blob: 3603cbe2d40ea2a2a76e14d69f403ee3a4997613 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Universal Interface for Intel High Definition Audio Codec
3 *
4 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
5 *
6 *
7 * This driver is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This driver is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
Takashi Iwai18478e82012-03-09 17:51:10 +010022#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/init.h>
24#include <linux/delay.h>
25#include <linux/slab.h>
26#include <linux/pci.h>
Ingo Molnar62932df2006-01-16 16:34:20 +010027#include <linux/mutex.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040028#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <sound/core.h>
30#include "hda_codec.h"
31#include <sound/asoundef.h>
Jaroslav Kysela302e9c52006-07-05 17:39:49 +020032#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <sound/initval.h>
Takashi Iwaicd372fb2011-03-03 14:40:14 +010034#include <sound/jack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "hda_local.h"
Jaroslav Kysela123c07a2009-10-21 14:48:23 +020036#include "hda_beep.h"
Takashi Iwai1835a0f2011-10-27 22:12:46 +020037#include "hda_jack.h"
Takashi Iwai28073142007-07-27 18:58:06 +020038#include <sound/hda_hwdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Takashi Iwaid66fee52011-08-02 15:39:31 +020040#define CREATE_TRACE_POINTS
41#include "hda_trace.h"
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/*
44 * vendor / preset table
45 */
46
47struct hda_vendor_id {
48 unsigned int id;
49 const char *name;
50};
51
52/* codec vendor labels */
53static struct hda_vendor_id hda_vendor_ids[] = {
Takashi Iwaic8cd1282008-02-13 16:59:29 +010054 { 0x1002, "ATI" },
Takashi Iwaie5f14242009-07-01 18:11:44 +020055 { 0x1013, "Cirrus Logic" },
Takashi Iwaia9226252006-09-17 22:05:54 +020056 { 0x1057, "Motorola" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010057 { 0x1095, "Silicon Image" },
Takashi Iwai31117b72008-12-16 14:43:21 +010058 { 0x10de, "Nvidia" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010059 { 0x10ec, "Realtek" },
Takashi Iwai4e01f542009-04-16 08:53:34 +020060 { 0x1102, "Creative" },
Joseph Chanc577b8a2006-11-29 15:29:40 +010061 { 0x1106, "VIA" },
Matthew Ranostay7f168592007-10-18 17:38:17 +020062 { 0x111d, "IDT" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010063 { 0x11c1, "LSI" },
Takashi Iwai54b903e2005-05-15 14:30:10 +020064 { 0x11d4, "Analog Devices" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 { 0x13f6, "C-Media" },
Takashi Iwaia9226252006-09-17 22:05:54 +020066 { 0x14f1, "Conexant" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010067 { 0x17e8, "Chrontel" },
68 { 0x1854, "LG" },
Mark Brown8199de32008-10-28 14:50:13 +000069 { 0x1aec, "Wolfson Microelectronics" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 { 0x434d, "C-Media" },
Takashi Iwai74c61132008-12-18 09:11:33 +010071 { 0x8086, "Intel" },
Matt2f2f4252005-04-13 14:45:30 +020072 { 0x8384, "SigmaTel" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 {} /* terminator */
74};
75
Takashi Iwai1289e9e2008-11-27 15:47:11 +010076static DEFINE_MUTEX(preset_mutex);
77static LIST_HEAD(hda_preset_tables);
78
79int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
80{
81 mutex_lock(&preset_mutex);
82 list_add_tail(&preset->list, &hda_preset_tables);
83 mutex_unlock(&preset_mutex);
84 return 0;
85}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010086EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset);
Takashi Iwai1289e9e2008-11-27 15:47:11 +010087
88int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
89{
90 mutex_lock(&preset_mutex);
91 list_del(&preset->list);
92 mutex_unlock(&preset_mutex);
93 return 0;
94}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010095EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Takashi Iwai83012a72012-08-24 18:38:08 +020097#ifdef CONFIG_PM
Takashi Iwaid846b172012-11-24 11:58:24 +010098#define codec_in_pm(codec) ((codec)->in_pm)
Takashi Iwaicb53c622007-08-10 17:21:45 +020099static void hda_power_work(struct work_struct *work);
100static void hda_keep_power_on(struct hda_codec *codec);
Takashi Iwaie581f3d2011-07-26 10:19:20 +0200101#define hda_codec_is_power_on(codec) ((codec)->power_on)
Takashi Iwai68467f52012-08-28 09:14:29 -0700102static inline void hda_call_pm_notify(struct hda_bus *bus, bool power_up)
103{
104 if (bus->ops.pm_notify)
105 bus->ops.pm_notify(bus, power_up);
106}
Takashi Iwaicb53c622007-08-10 17:21:45 +0200107#else
Takashi Iwaid846b172012-11-24 11:58:24 +0100108#define codec_in_pm(codec) 0
Takashi Iwaicb53c622007-08-10 17:21:45 +0200109static inline void hda_keep_power_on(struct hda_codec *codec) {}
Takashi Iwaie581f3d2011-07-26 10:19:20 +0200110#define hda_codec_is_power_on(codec) 1
Takashi Iwai68467f52012-08-28 09:14:29 -0700111#define hda_call_pm_notify(bus, state) {}
Takashi Iwaicb53c622007-08-10 17:21:45 +0200112#endif
113
Takashi Iwaid5191e52009-11-16 14:58:17 +0100114/**
115 * snd_hda_get_jack_location - Give a location string of the jack
116 * @cfg: pin default config value
117 *
118 * Parse the pin default config value and returns the string of the
119 * jack location, e.g. "Rear", "Front", etc.
120 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400121const char *snd_hda_get_jack_location(u32 cfg)
122{
123 static char *bases[7] = {
124 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
125 };
126 static unsigned char specials_idx[] = {
127 0x07, 0x08,
128 0x17, 0x18, 0x19,
129 0x37, 0x38
130 };
131 static char *specials[] = {
132 "Rear Panel", "Drive Bar",
133 "Riser", "HDMI", "ATAPI",
134 "Mobile-In", "Mobile-Out"
135 };
136 int i;
137 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
138 if ((cfg & 0x0f) < 7)
139 return bases[cfg & 0x0f];
140 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
141 if (cfg == specials_idx[i])
142 return specials[i];
143 }
144 return "UNKNOWN";
145}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100146EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400147
Takashi Iwaid5191e52009-11-16 14:58:17 +0100148/**
149 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
150 * @cfg: pin default config value
151 *
152 * Parse the pin default config value and returns the string of the
153 * jack connectivity, i.e. external or internal connection.
154 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400155const char *snd_hda_get_jack_connectivity(u32 cfg)
156{
157 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
158
159 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
160}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100161EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400162
Takashi Iwaid5191e52009-11-16 14:58:17 +0100163/**
164 * snd_hda_get_jack_type - Give a type string of the jack
165 * @cfg: pin default config value
166 *
167 * Parse the pin default config value and returns the string of the
168 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
169 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400170const char *snd_hda_get_jack_type(u32 cfg)
171{
172 static char *jack_types[16] = {
173 "Line Out", "Speaker", "HP Out", "CD",
174 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
175 "Line In", "Aux", "Mic", "Telephony",
David Henningssonaeb3a972013-04-04 11:47:13 +0200176 "SPDIF In", "Digital In", "Reserved", "Other"
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400177 };
178
179 return jack_types[(cfg & AC_DEFCFG_DEVICE)
180 >> AC_DEFCFG_DEVICE_SHIFT];
181}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100182EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400183
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100184/*
185 * Compose a 32bit command word to be sent to the HD-audio controller
186 */
187static inline unsigned int
188make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
189 unsigned int verb, unsigned int parm)
190{
191 u32 val;
192
Takashi Iwai82e1b802009-07-17 12:47:34 +0200193 if ((codec->addr & ~0xf) || (direct & ~1) || (nid & ~0x7f) ||
194 (verb & ~0xfff) || (parm & ~0xffff)) {
Wu Fengguang6430aee2009-07-17 16:49:19 +0800195 printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
196 codec->addr, direct, nid, verb, parm);
197 return ~0;
198 }
199
200 val = (u32)codec->addr << 28;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100201 val |= (u32)direct << 27;
202 val |= (u32)nid << 20;
203 val |= verb << 8;
204 val |= parm;
205 return val;
206}
207
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200208/*
209 * Send and receive a verb
210 */
211static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
212 unsigned int *res)
213{
214 struct hda_bus *bus = codec->bus;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200215 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200216
Wu Fengguang6430aee2009-07-17 16:49:19 +0800217 if (cmd == ~0)
218 return -1;
219
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200220 if (res)
221 *res = -1;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200222 again:
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200223 snd_hda_power_up(codec);
224 mutex_lock(&bus->cmd_mutex);
Takashi Iwai3bcce5c2012-12-20 11:17:17 +0100225 for (;;) {
226 trace_hda_send_cmd(codec, cmd);
227 err = bus->ops.command(bus, cmd);
228 if (err != -EAGAIN)
229 break;
230 /* process pending verbs */
231 bus->ops.get_response(bus, codec->addr);
232 }
Takashi Iwaid66fee52011-08-02 15:39:31 +0200233 if (!err && res) {
Wu Fengguangdeadff12009-08-01 18:45:16 +0800234 *res = bus->ops.get_response(bus, codec->addr);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200235 trace_hda_get_response(codec, *res);
236 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200237 mutex_unlock(&bus->cmd_mutex);
238 snd_hda_power_down(codec);
Takashi Iwaid846b172012-11-24 11:58:24 +0100239 if (!codec_in_pm(codec) && res && *res == -1 && bus->rirb_error) {
Takashi Iwai8dd78332009-06-02 01:16:07 +0200240 if (bus->response_reset) {
241 snd_printd("hda_codec: resetting BUS due to "
242 "fatal communication error\n");
Takashi Iwaid66fee52011-08-02 15:39:31 +0200243 trace_hda_bus_reset(bus);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200244 bus->ops.bus_reset(bus);
245 }
246 goto again;
247 }
248 /* clear reset-flag when the communication gets recovered */
Takashi Iwaid846b172012-11-24 11:58:24 +0100249 if (!err || codec_in_pm(codec))
Takashi Iwai8dd78332009-06-02 01:16:07 +0200250 bus->response_reset = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200251 return err;
252}
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254/**
255 * snd_hda_codec_read - send a command and get the response
256 * @codec: the HDA codec
257 * @nid: NID to send the command
258 * @direct: direct flag
259 * @verb: the verb to send
260 * @parm: the parameter for the verb
261 *
262 * Send a single command and read the corresponding response.
263 *
264 * Returns the obtained response value, or -1 for an error.
265 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200266unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
267 int direct,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 unsigned int verb, unsigned int parm)
269{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200270 unsigned cmd = make_codec_cmd(codec, nid, direct, verb, parm);
271 unsigned int res;
Greg Thelen9857edf2011-06-13 07:45:45 -0700272 if (codec_exec_verb(codec, cmd, &res))
273 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 return res;
275}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100276EXPORT_SYMBOL_HDA(snd_hda_codec_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278/**
279 * snd_hda_codec_write - send a single command without waiting for response
280 * @codec: the HDA codec
281 * @nid: NID to send the command
282 * @direct: direct flag
283 * @verb: the verb to send
284 * @parm: the parameter for the verb
285 *
286 * Send a single command without waiting for response.
287 *
288 * Returns 0 if successful, or a negative error code.
289 */
290int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
291 unsigned int verb, unsigned int parm)
292{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200293 unsigned int cmd = make_codec_cmd(codec, nid, direct, verb, parm);
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100294 unsigned int res;
Takashi Iwaib20f3b82009-06-02 01:20:22 +0200295 return codec_exec_verb(codec, cmd,
296 codec->bus->sync_write ? &res : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100298EXPORT_SYMBOL_HDA(snd_hda_codec_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300/**
301 * snd_hda_sequence_write - sequence writes
302 * @codec: the HDA codec
303 * @seq: VERB array to send
304 *
305 * Send the commands sequentially from the given array.
306 * The array must be terminated with NID=0.
307 */
308void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
309{
310 for (; seq->nid; seq++)
311 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
312}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100313EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315/**
316 * snd_hda_get_sub_nodes - get the range of sub nodes
317 * @codec: the HDA codec
318 * @nid: NID to parse
319 * @start_id: the pointer to store the start NID
320 *
321 * Parse the NID and store the start NID of its sub-nodes.
322 * Returns the number of sub-nodes.
323 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200324int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
325 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
327 unsigned int parm;
328
329 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
Danny Tholene8a7f132007-09-11 21:41:56 +0200330 if (parm == -1)
331 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 *start_id = (parm >> 16) & 0x7fff;
333 return (int)(parm & 0x7fff);
334}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100335EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100337/* connection list element */
338struct hda_conn_list {
339 struct list_head list;
340 int len;
341 hda_nid_t nid;
342 hda_nid_t conns[0];
343};
344
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200345/* look up the cached results */
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100346static struct hda_conn_list *
347lookup_conn_list(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200348{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100349 struct hda_conn_list *p;
350 list_for_each_entry(p, &codec->conn_list, list) {
351 if (p->nid == nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200352 return p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200353 }
354 return NULL;
355}
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200356
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100357static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
358 const hda_nid_t *list)
359{
360 struct hda_conn_list *p;
361
362 p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL);
363 if (!p)
364 return -ENOMEM;
365 p->len = len;
366 p->nid = nid;
367 memcpy(p->conns, list, len * sizeof(hda_nid_t));
368 list_add(&p->list, &codec->conn_list);
369 return 0;
370}
371
372static void remove_conn_list(struct hda_codec *codec)
373{
374 while (!list_empty(&codec->conn_list)) {
375 struct hda_conn_list *p;
376 p = list_first_entry(&codec->conn_list, typeof(*p), list);
377 list_del(&p->list);
378 kfree(p);
379 }
380}
381
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200382/* read the connection and add to the cache */
383static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200384{
Takashi Iwai4eea3092013-02-07 18:18:19 +0100385 hda_nid_t list[32];
386 hda_nid_t *result = list;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200387 int len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200388
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200389 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
Takashi Iwai4eea3092013-02-07 18:18:19 +0100390 if (len == -ENOSPC) {
391 len = snd_hda_get_num_raw_conns(codec, nid);
392 result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL);
393 if (!result)
394 return -ENOMEM;
395 len = snd_hda_get_raw_connections(codec, nid, result, len);
396 }
397 if (len >= 0)
398 len = snd_hda_override_conn_list(codec, nid, len, result);
399 if (result != list)
400 kfree(result);
401 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200402}
Takashi Iwaidce20792011-06-24 14:10:28 +0200403
404/**
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100405 * snd_hda_get_conn_list - get connection list
406 * @codec: the HDA codec
407 * @nid: NID to parse
408 * @len: number of connection list entries
409 * @listp: the pointer to store NID list
410 *
411 * Parses the connection list of the given widget and stores the pointer
412 * to the list of NIDs.
413 *
414 * Returns the number of connections, or a negative error code.
415 *
416 * Note that the returned pointer isn't protected against the list
417 * modification. If snd_hda_override_conn_list() might be called
418 * concurrently, protect with a mutex appropriately.
419 */
420int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
421 const hda_nid_t **listp)
422{
423 bool added = false;
424
425 for (;;) {
426 int err;
427 const struct hda_conn_list *p;
428
429 /* if the connection-list is already cached, read it */
430 p = lookup_conn_list(codec, nid);
431 if (p) {
432 if (listp)
433 *listp = p->conns;
434 return p->len;
435 }
436 if (snd_BUG_ON(added))
437 return -EINVAL;
438
439 err = read_and_add_raw_conns(codec, nid);
440 if (err < 0)
441 return err;
442 added = true;
443 }
444}
445EXPORT_SYMBOL_HDA(snd_hda_get_conn_list);
446
447/**
Takashi Iwaidce20792011-06-24 14:10:28 +0200448 * snd_hda_get_connections - copy connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 * @codec: the HDA codec
450 * @nid: NID to parse
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200451 * @conn_list: connection list array; when NULL, checks only the size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 * @max_conns: max. number of connections to store
453 *
454 * Parses the connection list of the given widget and stores the list
455 * of NIDs.
456 *
457 * Returns the number of connections, or a negative error code.
458 */
459int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200460 hda_nid_t *conn_list, int max_conns)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200461{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100462 const hda_nid_t *list;
463 int len = snd_hda_get_conn_list(codec, nid, &list);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200464
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100465 if (len > 0 && conn_list) {
466 if (len > max_conns) {
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200467 snd_printk(KERN_ERR "hda_codec: "
468 "Too many connections %d for NID 0x%x\n",
469 len, nid);
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200470 return -EINVAL;
471 }
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100472 memcpy(conn_list, list, len * sizeof(hda_nid_t));
Takashi Iwaidce20792011-06-24 14:10:28 +0200473 }
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200474
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100475 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200476}
477EXPORT_SYMBOL_HDA(snd_hda_get_connections);
478
Takashi Iwai4eea3092013-02-07 18:18:19 +0100479/* return CONNLIST_LEN parameter of the given widget */
480static unsigned int get_num_conns(struct hda_codec *codec, hda_nid_t nid)
481{
482 unsigned int wcaps = get_wcaps(codec, nid);
483 unsigned int parm;
484
485 if (!(wcaps & AC_WCAP_CONN_LIST) &&
486 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
487 return 0;
488
489 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
490 if (parm == -1)
491 parm = 0;
492 return parm;
493}
494
495int snd_hda_get_num_raw_conns(struct hda_codec *codec, hda_nid_t nid)
496{
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100497 return snd_hda_get_raw_connections(codec, nid, NULL, 0);
Takashi Iwai4eea3092013-02-07 18:18:19 +0100498}
499
Takashi Iwai9e7717c2011-07-11 15:42:52 +0200500/**
501 * snd_hda_get_raw_connections - copy connection list without cache
502 * @codec: the HDA codec
503 * @nid: NID to parse
504 * @conn_list: connection list array
505 * @max_conns: max. number of connections to store
506 *
507 * Like snd_hda_get_connections(), copy the connection list but without
508 * checking through the connection-list cache.
509 * Currently called only from hda_proc.c, so not exported.
510 */
511int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
512 hda_nid_t *conn_list, int max_conns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
514 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100515 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 unsigned int shift, num_elems, mask;
Takashi Iwai54d17402005-11-21 16:33:22 +0100517 hda_nid_t prev_nid;
Takashi Iwai5fdaecd2012-12-20 10:45:55 +0100518 int null_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Takashi Iwai4eea3092013-02-07 18:18:19 +0100520 parm = get_num_conns(codec, nid);
521 if (!parm)
Takashi Iwai8d087c72011-06-28 12:45:47 +0200522 return 0;
Jaroslav Kysela16a433d2009-07-22 16:20:40 +0200523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 if (parm & AC_CLIST_LONG) {
525 /* long form */
526 shift = 16;
527 num_elems = 2;
528 } else {
529 /* short form */
530 shift = 8;
531 num_elems = 4;
532 }
533 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 mask = (1 << (shift-1)) - 1;
535
Takashi Iwai0ba21762007-04-16 11:29:14 +0200536 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 return 0; /* no connection */
538
539 if (conn_len == 1) {
540 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200541 parm = snd_hda_codec_read(codec, nid, 0,
542 AC_VERB_GET_CONNECT_LIST, 0);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200543 if (parm == -1 && codec->bus->rirb_error)
544 return -EIO;
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100545 if (conn_list)
546 conn_list[0] = parm & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 return 1;
548 }
549
550 /* multi connection */
551 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100552 prev_nid = 0;
553 for (i = 0; i < conn_len; i++) {
554 int range_val;
555 hda_nid_t val, n;
556
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200557 if (i % num_elems == 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100558 parm = snd_hda_codec_read(codec, nid, 0,
559 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200560 if (parm == -1 && codec->bus->rirb_error)
561 return -EIO;
562 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200563 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100564 val = parm & mask;
Takashi Iwai5fdaecd2012-12-20 10:45:55 +0100565 if (val == 0 && null_count++) { /* no second chance */
Jaroslav Kysela2e9bf242009-07-18 11:48:19 +0200566 snd_printk(KERN_WARNING "hda_codec: "
567 "invalid CONNECT_LIST verb %x[%i]:%x\n",
568 nid, i, parm);
569 return 0;
570 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100571 parm >>= shift;
572 if (range_val) {
573 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200574 if (!prev_nid || prev_nid >= val) {
575 snd_printk(KERN_WARNING "hda_codec: "
576 "invalid dep_range_val %x:%x\n",
577 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100578 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100580 for (n = prev_nid + 1; n <= val; n++) {
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100581 if (conn_list) {
582 if (conns >= max_conns)
583 return -ENOSPC;
584 conn_list[conns] = n;
585 }
586 conns++;
Takashi Iwai54d17402005-11-21 16:33:22 +0100587 }
588 } else {
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100589 if (conn_list) {
590 if (conns >= max_conns)
591 return -ENOSPC;
592 conn_list[conns] = val;
593 }
594 conns++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100596 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 }
598 return conns;
599}
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200602 * snd_hda_override_conn_list - add/modify the connection-list to cache
603 * @codec: the HDA codec
604 * @nid: NID to parse
605 * @len: number of connection list entries
606 * @list: the list of connection entries
607 *
608 * Add or modify the given connection-list to the cache. If the corresponding
609 * cache already exists, invalidate it and append a new one.
610 *
611 * Returns zero or a negative error code.
612 */
613int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
614 const hda_nid_t *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100616 struct hda_conn_list *p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200617
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100618 p = lookup_conn_list(codec, nid);
619 if (p) {
620 list_del(&p->list);
621 kfree(p);
622 }
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200623
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100624 return add_conn_list(codec, nid, len, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625}
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200626EXPORT_SYMBOL_HDA(snd_hda_override_conn_list);
627
628/**
Takashi Iwai8d087c72011-06-28 12:45:47 +0200629 * snd_hda_get_conn_index - get the connection index of the given NID
630 * @codec: the HDA codec
631 * @mux: NID containing the list
632 * @nid: NID to select
633 * @recursive: 1 when searching NID recursively, otherwise 0
634 *
635 * Parses the connection list of the widget @mux and checks whether the
636 * widget @nid is present. If it is, return the connection index.
637 * Otherwise it returns -1.
638 */
639int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
640 hda_nid_t nid, int recursive)
641{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100642 const hda_nid_t *conn;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200643 int i, nums;
644
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100645 nums = snd_hda_get_conn_list(codec, mux, &conn);
Takashi Iwai8d087c72011-06-28 12:45:47 +0200646 for (i = 0; i < nums; i++)
647 if (conn[i] == nid)
648 return i;
649 if (!recursive)
650 return -1;
Takashi Iwaid94ddd82012-12-20 14:42:42 +0100651 if (recursive > 10) {
Takashi Iwai8d087c72011-06-28 12:45:47 +0200652 snd_printd("hda_codec: too deep connection for 0x%x\n", nid);
653 return -1;
654 }
655 recursive++;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200656 for (i = 0; i < nums; i++) {
657 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
658 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
659 continue;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200660 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
661 return i;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200662 }
Takashi Iwai8d087c72011-06-28 12:45:47 +0200663 return -1;
664}
665EXPORT_SYMBOL_HDA(snd_hda_get_conn_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667/**
668 * snd_hda_queue_unsol_event - add an unsolicited event to queue
669 * @bus: the BUS
670 * @res: unsolicited event (lower 32bit of RIRB entry)
671 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
672 *
673 * Adds the given event to the queue. The events are processed in
674 * the workqueue asynchronously. Call this function in the interrupt
675 * hanlder when RIRB receives an unsolicited event.
676 *
677 * Returns 0 if successful, or a negative error code.
678 */
679int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
680{
681 struct hda_bus_unsolicited *unsol;
682 unsigned int wp;
683
Takashi Iwaiecf726f2011-08-09 14:22:44 +0200684 trace_hda_unsol_event(bus, res, res_ex);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200685 unsol = bus->unsol;
686 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 return 0;
688
689 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
690 unsol->wp = wp;
691
692 wp <<= 1;
693 unsol->queue[wp] = res;
694 unsol->queue[wp + 1] = res_ex;
695
Takashi Iwai6acaed32009-01-12 10:09:24 +0100696 queue_work(bus->workq, &unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 return 0;
699}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100700EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800703 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 */
David Howellsc4028952006-11-22 14:57:56 +0000705static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
David Howellsc4028952006-11-22 14:57:56 +0000707 struct hda_bus_unsolicited *unsol =
708 container_of(work, struct hda_bus_unsolicited, work);
709 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 struct hda_codec *codec;
711 unsigned int rp, caddr, res;
712
713 while (unsol->rp != unsol->wp) {
714 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
715 unsol->rp = rp;
716 rp <<= 1;
717 res = unsol->queue[rp];
718 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200719 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 continue;
721 codec = bus->caddr_tbl[caddr & 0x0f];
722 if (codec && codec->patch_ops.unsol_event)
723 codec->patch_ops.unsol_event(codec, res);
724 }
725}
726
727/*
728 * initialize unsolicited queue
729 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200730static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
732 struct hda_bus_unsolicited *unsol;
733
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100734 if (bus->unsol) /* already initialized */
735 return 0;
736
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200737 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200738 if (!unsol) {
739 snd_printk(KERN_ERR "hda_codec: "
740 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 return -ENOMEM;
742 }
David Howellsc4028952006-11-22 14:57:56 +0000743 INIT_WORK(&unsol->work, process_unsol_events);
744 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 bus->unsol = unsol;
746 return 0;
747}
748
749/*
750 * destructor
751 */
752static void snd_hda_codec_free(struct hda_codec *codec);
753
754static int snd_hda_bus_free(struct hda_bus *bus)
755{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200756 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Takashi Iwai0ba21762007-04-16 11:29:14 +0200758 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 return 0;
Takashi Iwai6acaed32009-01-12 10:09:24 +0100760 if (bus->workq)
761 flush_workqueue(bus->workq);
762 if (bus->unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 kfree(bus->unsol);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200764 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 snd_hda_codec_free(codec);
766 }
767 if (bus->ops.private_free)
768 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100769 if (bus->workq)
770 destroy_workqueue(bus->workq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 kfree(bus);
772 return 0;
773}
774
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100775static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
777 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100778 bus->shutdown = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 return snd_hda_bus_free(bus);
780}
781
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200782#ifdef CONFIG_SND_HDA_HWDEP
783static int snd_hda_bus_dev_register(struct snd_device *device)
784{
785 struct hda_bus *bus = device->device_data;
786 struct hda_codec *codec;
787 list_for_each_entry(codec, &bus->codec_list, list) {
788 snd_hda_hwdep_add_sysfs(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +0100789 snd_hda_hwdep_add_power_sysfs(codec);
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200790 }
791 return 0;
792}
793#else
794#define snd_hda_bus_dev_register NULL
795#endif
796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797/**
798 * snd_hda_bus_new - create a HDA bus
799 * @card: the card entry
800 * @temp: the template for hda_bus information
801 * @busp: the pointer to store the created bus instance
802 *
803 * Returns 0 if successful, or a negative error code.
804 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +0100805int snd_hda_bus_new(struct snd_card *card,
Takashi Iwai756e2b02007-04-16 11:27:07 +0200806 const struct hda_bus_template *temp,
807 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
809 struct hda_bus *bus;
810 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100811 static struct snd_device_ops dev_ops = {
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200812 .dev_register = snd_hda_bus_dev_register,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 .dev_free = snd_hda_bus_dev_free,
814 };
815
Takashi Iwaida3cec32008-08-08 17:12:14 +0200816 if (snd_BUG_ON(!temp))
817 return -EINVAL;
818 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
819 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 if (busp)
822 *busp = NULL;
823
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200824 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 if (bus == NULL) {
826 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
827 return -ENOMEM;
828 }
829
830 bus->card = card;
831 bus->private_data = temp->private_data;
832 bus->pci = temp->pci;
833 bus->modelname = temp->modelname;
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100834 bus->power_save = temp->power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 bus->ops = temp->ops;
836
Ingo Molnar62932df2006-01-16 16:34:20 +0100837 mutex_init(&bus->cmd_mutex);
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200838 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 INIT_LIST_HEAD(&bus->codec_list);
840
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100841 snprintf(bus->workq_name, sizeof(bus->workq_name),
842 "hd-audio%d", card->number);
843 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100844 if (!bus->workq) {
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100845 snd_printk(KERN_ERR "cannot create workqueue %s\n",
846 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100847 kfree(bus);
848 return -ENOMEM;
849 }
850
Takashi Iwai0ba21762007-04-16 11:29:14 +0200851 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
852 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 snd_hda_bus_free(bus);
854 return err;
855 }
856 if (busp)
857 *busp = bus;
858 return 0;
859}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100860EXPORT_SYMBOL_HDA(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Takashi Iwai82467612007-07-27 19:15:54 +0200862#ifdef CONFIG_SND_HDA_GENERIC
863#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200864 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200865#else
866#define is_generic_config(codec) 0
867#endif
868
Takashi Iwai645f10c2008-11-28 15:07:37 +0100869#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100870#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
871#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100872#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100873#endif
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875/*
876 * find a matching codec preset
877 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200878static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200879find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100881 struct hda_codec_preset_list *tbl;
882 const struct hda_codec_preset *preset;
Takashi Iwai5d908ab2012-08-24 18:40:10 +0200883 unsigned int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Takashi Iwai82467612007-07-27 19:15:54 +0200885 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100886 return NULL; /* use the generic parser */
887
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100888 again:
889 mutex_lock(&preset_mutex);
890 list_for_each_entry(tbl, &hda_preset_tables, list) {
891 if (!try_module_get(tbl->owner)) {
892 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
893 continue;
894 }
895 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100897 if (preset->afg && preset->afg != codec->afg)
898 continue;
899 if (preset->mfg && preset->mfg != codec->mfg)
900 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200901 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200903 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200904 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100905 preset->rev == codec->revision_id)) {
906 mutex_unlock(&preset_mutex);
907 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100911 module_put(tbl->owner);
912 }
913 mutex_unlock(&preset_mutex);
914
915 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
916 char name[32];
917 if (!mod_requested)
918 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
919 codec->vendor_id);
920 else
921 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
922 (codec->vendor_id >> 16) & 0xffff);
923 request_module(name);
924 mod_requested++;
925 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 }
927 return NULL;
928}
929
930/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200931 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200933static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
935 const struct hda_vendor_id *c;
936 const char *vendor = NULL;
937 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200938 char tmp[16];
939
940 if (codec->vendor_name)
941 goto get_chip_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
943 for (c = hda_vendor_ids; c->id; c++) {
944 if (c->id == vendor_id) {
945 vendor = c->name;
946 break;
947 }
948 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200949 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 sprintf(tmp, "Generic %04x", vendor_id);
951 vendor = tmp;
952 }
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200953 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
954 if (!codec->vendor_name)
955 return -ENOMEM;
956
957 get_chip_name:
958 if (codec->chip_name)
959 return 0;
960
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 if (codec->preset && codec->preset->name)
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200962 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
963 else {
964 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
965 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
966 }
967 if (!codec->chip_name)
Takashi Iwaif44ac832008-07-30 15:01:45 +0200968 return -ENOMEM;
969 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970}
971
972/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200973 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +0100975static void setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200977 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 hda_nid_t nid;
979
980 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
981 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200982 function_id = snd_hda_param_read(codec, nid,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200983 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +0200984 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200985 case AC_GRP_AUDIO_FUNCTION:
986 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200987 codec->afg_function_id = function_id & 0xff;
988 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200989 break;
990 case AC_GRP_MODEM_FUNCTION:
991 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200992 codec->mfg_function_id = function_id & 0xff;
993 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200994 break;
995 default:
996 break;
997 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999}
1000
1001/*
Takashi Iwai54d17402005-11-21 16:33:22 +01001002 * read widget caps for each widget and store in cache
1003 */
1004static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
1005{
1006 int i;
1007 hda_nid_t nid;
1008
1009 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
1010 &codec->start_nid);
1011 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001012 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +01001013 return -ENOMEM;
1014 nid = codec->start_nid;
1015 for (i = 0; i < codec->num_nodes; i++, nid++)
1016 codec->wcaps[i] = snd_hda_param_read(codec, nid,
1017 AC_PAR_AUDIO_WIDGET_CAP);
1018 return 0;
1019}
1020
Takashi Iwai3be14142009-02-20 14:11:16 +01001021/* read all pin default configurations and save codec->init_pins */
1022static int read_pin_defaults(struct hda_codec *codec)
1023{
1024 int i;
1025 hda_nid_t nid = codec->start_nid;
1026
1027 for (i = 0; i < codec->num_nodes; i++, nid++) {
1028 struct hda_pincfg *pin;
1029 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +02001030 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +01001031 if (wid_type != AC_WID_PIN)
1032 continue;
1033 pin = snd_array_new(&codec->init_pins);
1034 if (!pin)
1035 return -ENOMEM;
1036 pin->nid = nid;
1037 pin->cfg = snd_hda_codec_read(codec, nid, 0,
1038 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001039 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
1040 AC_VERB_GET_PIN_WIDGET_CONTROL,
1041 0);
Takashi Iwai3be14142009-02-20 14:11:16 +01001042 }
1043 return 0;
1044}
1045
1046/* look up the given pin config list and return the item matching with NID */
1047static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
1048 struct snd_array *array,
1049 hda_nid_t nid)
1050{
1051 int i;
1052 for (i = 0; i < array->used; i++) {
1053 struct hda_pincfg *pin = snd_array_elem(array, i);
1054 if (pin->nid == nid)
1055 return pin;
1056 }
1057 return NULL;
1058}
1059
Takashi Iwai3be14142009-02-20 14:11:16 +01001060/* set the current pin config value for the given NID.
1061 * the value is cached, and read via snd_hda_codec_get_pincfg()
1062 */
1063int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
1064 hda_nid_t nid, unsigned int cfg)
1065{
1066 struct hda_pincfg *pin;
1067
Takashi Iwaib82855a2009-12-27 11:24:56 +01001068 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1069 return -EINVAL;
1070
Takashi Iwai3be14142009-02-20 14:11:16 +01001071 pin = look_up_pincfg(codec, list, nid);
1072 if (!pin) {
1073 pin = snd_array_new(list);
1074 if (!pin)
1075 return -ENOMEM;
1076 pin->nid = nid;
1077 }
1078 pin->cfg = cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001079 return 0;
1080}
1081
Takashi Iwaid5191e52009-11-16 14:58:17 +01001082/**
1083 * snd_hda_codec_set_pincfg - Override a pin default configuration
1084 * @codec: the HDA codec
1085 * @nid: NID to set the pin config
1086 * @cfg: the pin default config value
1087 *
1088 * Override a pin default configuration value in the cache.
1089 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1090 * priority than the real hardware value.
1091 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001092int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1093 hda_nid_t nid, unsigned int cfg)
1094{
Takashi Iwai346ff702009-02-23 09:42:57 +01001095 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001096}
1097EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
1098
Takashi Iwaid5191e52009-11-16 14:58:17 +01001099/**
1100 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1101 * @codec: the HDA codec
1102 * @nid: NID to get the pin config
1103 *
1104 * Get the current pin config value of the given pin NID.
1105 * If the pincfg value is cached or overridden via sysfs or driver,
1106 * returns the cached value.
1107 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001108unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1109{
1110 struct hda_pincfg *pin;
1111
Takashi Iwai3be14142009-02-20 14:11:16 +01001112#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai09b70e82013-01-10 18:21:56 +01001113 {
1114 unsigned int cfg = 0;
1115 mutex_lock(&codec->user_mutex);
1116 pin = look_up_pincfg(codec, &codec->user_pins, nid);
1117 if (pin)
1118 cfg = pin->cfg;
1119 mutex_unlock(&codec->user_mutex);
1120 if (cfg)
1121 return cfg;
1122 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001123#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001124 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1125 if (pin)
1126 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001127 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1128 if (pin)
1129 return pin->cfg;
1130 return 0;
1131}
1132EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
1133
Takashi Iwaid7fdc002013-01-10 08:38:04 +01001134/* remember the current pinctl target value */
1135int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
1136 unsigned int val)
1137{
1138 struct hda_pincfg *pin;
1139
1140 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1141 if (!pin)
1142 return -EINVAL;
1143 pin->target = val;
1144 return 0;
1145}
1146EXPORT_SYMBOL_HDA(snd_hda_codec_set_pin_target);
1147
1148/* return the current pinctl target value */
1149int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
1150{
1151 struct hda_pincfg *pin;
1152
1153 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1154 if (!pin)
1155 return 0;
1156 return pin->target;
1157}
1158EXPORT_SYMBOL_HDA(snd_hda_codec_get_pin_target);
1159
Takashi Iwai92ee6162009-12-27 11:18:59 +01001160/**
1161 * snd_hda_shutup_pins - Shut up all pins
1162 * @codec: the HDA codec
1163 *
1164 * Clear all pin controls to shup up before suspend for avoiding click noise.
1165 * The controls aren't cached so that they can be resumed properly.
1166 */
1167void snd_hda_shutup_pins(struct hda_codec *codec)
1168{
1169 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001170 /* don't shut up pins when unloading the driver; otherwise it breaks
1171 * the default pin setup at the next load of the driver
1172 */
1173 if (codec->bus->shutdown)
1174 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001175 for (i = 0; i < codec->init_pins.used; i++) {
1176 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1177 /* use read here for syncing after issuing each verb */
1178 snd_hda_codec_read(codec, pin->nid, 0,
1179 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1180 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001181 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001182}
1183EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
1184
Takashi Iwai2a439522011-07-26 09:52:50 +02001185#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001186/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1187static void restore_shutup_pins(struct hda_codec *codec)
1188{
1189 int i;
1190 if (!codec->pins_shutup)
1191 return;
1192 if (codec->bus->shutdown)
1193 return;
1194 for (i = 0; i < codec->init_pins.used; i++) {
1195 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1196 snd_hda_codec_write(codec, pin->nid, 0,
1197 AC_VERB_SET_PIN_WIDGET_CONTROL,
1198 pin->ctrl);
1199 }
1200 codec->pins_shutup = 0;
1201}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001202#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001203
David Henningsson26a6cb62012-10-09 15:04:21 +02001204static void hda_jackpoll_work(struct work_struct *work)
1205{
1206 struct hda_codec *codec =
1207 container_of(work, struct hda_codec, jackpoll_work.work);
1208 if (!codec->jackpoll_interval)
1209 return;
1210
1211 snd_hda_jack_set_dirty_all(codec);
1212 snd_hda_jack_poll_all(codec);
1213 queue_delayed_work(codec->bus->workq, &codec->jackpoll_work,
1214 codec->jackpoll_interval);
1215}
1216
Takashi Iwai01751f52007-08-10 16:59:39 +02001217static void init_hda_cache(struct hda_cache_rec *cache,
1218 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001219static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001220
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001221/* release all pincfg lists */
1222static void free_init_pincfgs(struct hda_codec *codec)
Takashi Iwai3be14142009-02-20 14:11:16 +01001223{
Takashi Iwai346ff702009-02-23 09:42:57 +01001224 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001225#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001226 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001227#endif
Takashi Iwai3be14142009-02-20 14:11:16 +01001228 snd_array_free(&codec->init_pins);
1229}
1230
Takashi Iwai54d17402005-11-21 16:33:22 +01001231/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001232 * audio-converter setup caches
1233 */
1234struct hda_cvt_setup {
1235 hda_nid_t nid;
1236 u8 stream_tag;
1237 u8 channel_id;
1238 u16 format_id;
1239 unsigned char active; /* cvt is currently used */
1240 unsigned char dirty; /* setups should be cleared */
1241};
1242
1243/* get or create a cache entry for the given audio converter NID */
1244static struct hda_cvt_setup *
1245get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1246{
1247 struct hda_cvt_setup *p;
1248 int i;
1249
1250 for (i = 0; i < codec->cvt_setups.used; i++) {
1251 p = snd_array_elem(&codec->cvt_setups, i);
1252 if (p->nid == nid)
1253 return p;
1254 }
1255 p = snd_array_new(&codec->cvt_setups);
1256 if (p)
1257 p->nid = nid;
1258 return p;
1259}
1260
1261/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 * codec destructor
1263 */
1264static void snd_hda_codec_free(struct hda_codec *codec)
1265{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001266 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 return;
David Henningsson26a6cb62012-10-09 15:04:21 +02001268 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai59cad162012-06-26 15:00:20 +02001269 snd_hda_jack_tbl_clear(codec);
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001270 free_init_pincfgs(codec);
Takashi Iwai83012a72012-08-24 18:38:08 +02001271#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02001272 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001273 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001274#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001276 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001277 snd_array_free(&codec->nids);
Takashi Iwai59cad162012-06-26 15:00:20 +02001278 snd_array_free(&codec->cvt_setups);
Stephen Warren7c935972011-06-01 11:14:17 -06001279 snd_array_free(&codec->spdif_out);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001280 remove_conn_list(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 codec->bus->caddr_tbl[codec->addr] = NULL;
1282 if (codec->patch_ops.free)
1283 codec->patch_ops.free(codec);
Takashi Iwai83012a72012-08-24 18:38:08 +02001284#ifdef CONFIG_PM
Takashi Iwai08fa20a2012-08-31 07:46:56 -07001285 if (!codec->pm_down_notified) /* cancel leftover refcounts */
Takashi Iwai68467f52012-08-28 09:14:29 -07001286 hda_call_pm_notify(codec->bus, false);
1287#endif
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001288 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +02001289 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001290 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001291 kfree(codec->vendor_name);
1292 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001293 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001294 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 kfree(codec);
1296}
1297
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001298static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec,
1299 hda_nid_t fg, unsigned int power_state);
1300
Takashi Iwaid8193872012-08-31 07:54:38 -07001301static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001302 unsigned int power_state);
1303
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304/**
1305 * snd_hda_codec_new - create a HDA codec
1306 * @bus: the bus to assign
1307 * @codec_addr: the codec address
1308 * @codecp: the pointer to store the generated codec
1309 *
1310 * Returns 0 if successful, or a negative error code.
1311 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001312int snd_hda_codec_new(struct hda_bus *bus,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001313 unsigned int codec_addr,
1314 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315{
1316 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001317 char component[31];
Takashi Iwaid8193872012-08-31 07:54:38 -07001318 hda_nid_t fg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 int err;
1320
Takashi Iwaida3cec32008-08-08 17:12:14 +02001321 if (snd_BUG_ON(!bus))
1322 return -EINVAL;
1323 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1324 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
1326 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001327 snd_printk(KERN_ERR "hda_codec: "
1328 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 return -EBUSY;
1330 }
1331
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001332 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 if (codec == NULL) {
1334 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1335 return -ENOMEM;
1336 }
1337
1338 codec->bus = bus;
1339 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001340 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001341 mutex_init(&codec->control_mutex);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001342 mutex_init(&codec->hash_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001343 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001344 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001345 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1346 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001347 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001348 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001349 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Stephen Warren7c935972011-06-01 11:14:17 -06001350 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai361dab32012-05-09 14:35:27 +02001351 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01001352 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001353 INIT_LIST_HEAD(&codec->conn_list);
1354
David Henningsson26a6cb62012-10-09 15:04:21 +02001355 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Takashi Iwai83012a72012-08-24 18:38:08 +02001357#ifdef CONFIG_PM
Takashi Iwai5536c6d2012-05-08 17:08:10 +02001358 spin_lock_init(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001359 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1360 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1361 * the caller has to power down appropriatley after initialization
1362 * phase.
1363 */
1364 hda_keep_power_on(codec);
Takashi Iwai68467f52012-08-28 09:14:29 -07001365 hda_call_pm_notify(bus, true);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001366#endif
1367
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001368 if (codec->bus->modelname) {
1369 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1370 if (!codec->modelname) {
1371 snd_hda_codec_free(codec);
1372 return -ENODEV;
1373 }
1374 }
1375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 list_add_tail(&codec->list, &bus->codec_list);
1377 bus->caddr_tbl[codec_addr] = codec;
1378
Takashi Iwai0ba21762007-04-16 11:29:14 +02001379 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1380 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001381 if (codec->vendor_id == -1)
1382 /* read again, hopefully the access method was corrected
1383 * in the last read...
1384 */
1385 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1386 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001387 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1388 AC_PAR_SUBSYSTEM_ID);
1389 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1390 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001392 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001393 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001394 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001395 err = -ENODEV;
1396 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 }
1398
Takashi Iwaid8193872012-08-31 07:54:38 -07001399 fg = codec->afg ? codec->afg : codec->mfg;
1400 err = read_widget_caps(codec, fg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001401 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001402 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001403 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001404 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001405 err = read_pin_defaults(codec);
1406 if (err < 0)
1407 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001408
Takashi Iwai0ba21762007-04-16 11:29:14 +02001409 if (!codec->subsystem_id) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001410 codec->subsystem_id =
Takashi Iwaid8193872012-08-31 07:54:38 -07001411 snd_hda_codec_read(codec, fg, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001412 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001413 }
1414
Takashi Iwai83012a72012-08-24 18:38:08 +02001415#ifdef CONFIG_PM
Takashi Iwaid8193872012-08-31 07:54:38 -07001416 codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg,
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001417 AC_PWRST_CLKSTOP);
1418 if (!codec->d3_stop_clk)
1419 bus->power_keep_link_on = 1;
Mengdong Lin5d6147f2012-08-24 12:06:30 +08001420#endif
Takashi Iwaid8193872012-08-31 07:54:38 -07001421 codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
Takashi Iwai983f6b92012-08-28 09:18:01 -07001422 AC_PWRST_EPSS);
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001423
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001424 /* power-up all before initialization */
Takashi Iwaid8193872012-08-31 07:54:38 -07001425 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001426
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001427 snd_hda_codec_proc_new(codec);
1428
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001429 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001430
1431 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1432 codec->subsystem_id, codec->revision_id);
1433 snd_component_add(codec->bus->card, component);
1434
1435 if (codecp)
1436 *codecp = codec;
1437 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001438
1439 error:
1440 snd_hda_codec_free(codec);
1441 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001442}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001443EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001444
Mengdong Lina15d05d2013-02-08 17:09:31 -05001445int snd_hda_codec_update_widgets(struct hda_codec *codec)
1446{
1447 hda_nid_t fg;
1448 int err;
1449
1450 /* Assume the function group node does not change,
1451 * only the widget nodes may change.
1452 */
1453 kfree(codec->wcaps);
1454 fg = codec->afg ? codec->afg : codec->mfg;
1455 err = read_widget_caps(codec, fg);
1456 if (err < 0) {
1457 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
1458 return err;
1459 }
1460
1461 snd_array_free(&codec->init_pins);
1462 err = read_pin_defaults(codec);
1463
1464 return err;
1465}
1466EXPORT_SYMBOL_HDA(snd_hda_codec_update_widgets);
1467
1468
Takashi Iwaid5191e52009-11-16 14:58:17 +01001469/**
1470 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1471 * @codec: the HDA codec
1472 *
1473 * Start parsing of the given codec tree and (re-)initialize the whole
1474 * patch instance.
1475 *
1476 * Returns 0 if successful or a negative error code.
1477 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001478int snd_hda_codec_configure(struct hda_codec *codec)
1479{
1480 int err;
1481
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001482 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001483 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001484 err = get_codec_name(codec);
1485 if (err < 0)
1486 return err;
1487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Takashi Iwai82467612007-07-27 19:15:54 +02001489 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001491 goto patched;
1492 }
Takashi Iwai82467612007-07-27 19:15:54 +02001493 if (codec->preset && codec->preset->patch) {
1494 err = codec->preset->patch(codec);
1495 goto patched;
1496 }
1497
1498 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001499 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001500 if (err < 0)
1501 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001502
1503 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001504 if (!err && codec->patch_ops.unsol_event)
1505 err = init_unsol_queue(codec->bus);
Takashi Iwaif62faed2009-12-23 09:27:51 +01001506 /* audio codec should override the mixer name */
1507 if (!err && (codec->afg || !*codec->bus->card->mixername))
1508 snprintf(codec->bus->card->mixername,
1509 sizeof(codec->bus->card->mixername),
1510 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001511 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001513EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
Takashi Iwaied360812012-08-08 17:12:52 +02001515/* update the stream-id if changed */
1516static void update_pcm_stream_id(struct hda_codec *codec,
1517 struct hda_cvt_setup *p, hda_nid_t nid,
1518 u32 stream_tag, int channel_id)
1519{
1520 unsigned int oldval, newval;
1521
1522 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1523 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1524 newval = (stream_tag << 4) | channel_id;
1525 if (oldval != newval)
1526 snd_hda_codec_write(codec, nid, 0,
1527 AC_VERB_SET_CHANNEL_STREAMID,
1528 newval);
1529 p->stream_tag = stream_tag;
1530 p->channel_id = channel_id;
1531 }
1532}
1533
1534/* update the format-id if changed */
1535static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1536 hda_nid_t nid, int format)
1537{
1538 unsigned int oldval;
1539
1540 if (p->format_id != format) {
1541 oldval = snd_hda_codec_read(codec, nid, 0,
1542 AC_VERB_GET_STREAM_FORMAT, 0);
1543 if (oldval != format) {
1544 msleep(1);
1545 snd_hda_codec_write(codec, nid, 0,
1546 AC_VERB_SET_STREAM_FORMAT,
1547 format);
1548 }
1549 p->format_id = format;
1550 }
1551}
1552
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553/**
1554 * snd_hda_codec_setup_stream - set up the codec for streaming
1555 * @codec: the CODEC to set up
1556 * @nid: the NID to set up
1557 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1558 * @channel_id: channel id to pass, zero based.
1559 * @format: stream format.
1560 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001561void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1562 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 int channel_id, int format)
1564{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001565 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001566 struct hda_cvt_setup *p;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001567 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001568 int i;
1569
Takashi Iwai0ba21762007-04-16 11:29:14 +02001570 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001571 return;
1572
Takashi Iwai0ba21762007-04-16 11:29:14 +02001573 snd_printdd("hda_codec_setup_stream: "
1574 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001576 p = get_hda_cvt_setup(codec, nid);
Takashi Iwaiaffdb622013-01-07 15:36:45 +01001577 if (!p || p->active)
Takashi Iwaieb541332010-08-06 13:48:11 +02001578 return;
Takashi Iwaied360812012-08-08 17:12:52 +02001579
1580 if (codec->pcm_format_first)
1581 update_pcm_format(codec, p, nid, format);
1582 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1583 if (!codec->pcm_format_first)
1584 update_pcm_format(codec, p, nid, format);
1585
Takashi Iwaieb541332010-08-06 13:48:11 +02001586 p->active = 1;
1587 p->dirty = 0;
1588
1589 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001590 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001591 list_for_each_entry(c, &codec->bus->codec_list, list) {
1592 for (i = 0; i < c->cvt_setups.used; i++) {
1593 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001594 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001595 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001596 p->dirty = 1;
1597 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001598 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001600EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
Takashi Iwaif0cea792010-08-13 11:56:53 +02001602static void really_cleanup_stream(struct hda_codec *codec,
1603 struct hda_cvt_setup *q);
1604
Takashi Iwaid5191e52009-11-16 14:58:17 +01001605/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001606 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001607 * @codec: the CODEC to clean up
1608 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001609 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001610 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001611void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1612 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001613{
Takashi Iwaieb541332010-08-06 13:48:11 +02001614 struct hda_cvt_setup *p;
1615
Takashi Iwai888afa12008-03-18 09:57:50 +01001616 if (!nid)
1617 return;
1618
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001619 if (codec->no_sticky_stream)
1620 do_now = 1;
1621
Takashi Iwai888afa12008-03-18 09:57:50 +01001622 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001623 p = get_hda_cvt_setup(codec, nid);
Takashi Iwaiaffdb622013-01-07 15:36:45 +01001624 if (p && p->active) {
Takashi Iwaif0cea792010-08-13 11:56:53 +02001625 /* here we just clear the active flag when do_now isn't set;
1626 * actual clean-ups will be done later in
1627 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1628 */
1629 if (do_now)
1630 really_cleanup_stream(codec, p);
1631 else
1632 p->active = 0;
1633 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001634}
Takashi Iwaif0cea792010-08-13 11:56:53 +02001635EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001636
Takashi Iwaieb541332010-08-06 13:48:11 +02001637static void really_cleanup_stream(struct hda_codec *codec,
1638 struct hda_cvt_setup *q)
1639{
1640 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001641 if (q->stream_tag || q->channel_id)
1642 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1643 if (q->format_id)
1644 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1645);
Takashi Iwaieb541332010-08-06 13:48:11 +02001646 memset(q, 0, sizeof(*q));
1647 q->nid = nid;
1648}
1649
1650/* clean up the all conflicting obsolete streams */
1651static void purify_inactive_streams(struct hda_codec *codec)
1652{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001653 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001654 int i;
1655
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001656 list_for_each_entry(c, &codec->bus->codec_list, list) {
1657 for (i = 0; i < c->cvt_setups.used; i++) {
1658 struct hda_cvt_setup *p;
1659 p = snd_array_elem(&c->cvt_setups, i);
1660 if (p->dirty)
1661 really_cleanup_stream(c, p);
1662 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001663 }
1664}
1665
Takashi Iwai2a439522011-07-26 09:52:50 +02001666#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001667/* clean up all streams; called from suspend */
1668static void hda_cleanup_all_streams(struct hda_codec *codec)
1669{
1670 int i;
1671
1672 for (i = 0; i < codec->cvt_setups.used; i++) {
1673 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1674 if (p->stream_tag)
1675 really_cleanup_stream(codec, p);
1676 }
1677}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001678#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001679
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680/*
1681 * amp access functions
1682 */
1683
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001684/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001685#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001686#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001687#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1688#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001690#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
1692/* initialize the hash table */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001693static void init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001694 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695{
Takashi Iwai01751f52007-08-10 16:59:39 +02001696 memset(cache, 0, sizeof(*cache));
1697 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001698 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001699}
1700
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001701static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001702{
Takashi Iwai603c4012008-07-30 15:01:44 +02001703 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704}
1705
1706/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001707static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708{
Takashi Iwai01751f52007-08-10 16:59:39 +02001709 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1710 u16 cur = cache->hash[idx];
1711 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
1713 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001714 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 if (info->key == key)
1716 return info;
1717 cur = info->next;
1718 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001719 return NULL;
1720}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001722/* query the hash. allocate an entry if not found. */
1723static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1724 u32 key)
1725{
1726 struct hda_cache_head *info = get_hash(cache, key);
1727 if (!info) {
1728 u16 idx, cur;
1729 /* add a new hash entry */
1730 info = snd_array_new(&cache->buf);
1731 if (!info)
1732 return NULL;
1733 cur = snd_array_index(&cache->buf, info);
1734 info->key = key;
1735 info->val = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001736 info->dirty = 0;
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001737 idx = key % (u16)ARRAY_SIZE(cache->hash);
1738 info->next = cache->hash[idx];
1739 cache->hash[idx] = cur;
1740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 return info;
1742}
1743
Takashi Iwai01751f52007-08-10 16:59:39 +02001744/* query and allocate an amp hash entry */
1745static inline struct hda_amp_info *
1746get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1747{
1748 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1749}
1750
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001751/* overwrite the value with the key in the caps hash */
1752static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1753{
1754 struct hda_amp_info *info;
1755
1756 mutex_lock(&codec->hash_mutex);
1757 info = get_alloc_amp_hash(codec, key);
1758 if (!info) {
1759 mutex_unlock(&codec->hash_mutex);
1760 return -EINVAL;
1761 }
1762 info->amp_caps = val;
1763 info->head.val |= INFO_AMP_CAPS;
1764 mutex_unlock(&codec->hash_mutex);
1765 return 0;
1766}
1767
1768/* query the value from the caps hash; if not found, fetch the current
1769 * value from the given function and store in the hash
1770 */
1771static unsigned int
1772query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1773 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1774{
1775 struct hda_amp_info *info;
1776 unsigned int val;
1777
1778 mutex_lock(&codec->hash_mutex);
1779 info = get_alloc_amp_hash(codec, key);
1780 if (!info) {
1781 mutex_unlock(&codec->hash_mutex);
1782 return 0;
1783 }
1784 if (!(info->head.val & INFO_AMP_CAPS)) {
1785 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1786 val = func(codec, nid, dir);
1787 write_caps_hash(codec, key, val);
1788 } else {
1789 val = info->amp_caps;
1790 mutex_unlock(&codec->hash_mutex);
1791 }
1792 return val;
1793}
1794
1795static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1796 int direction)
1797{
1798 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1799 nid = codec->afg;
1800 return snd_hda_param_read(codec, nid,
1801 direction == HDA_OUTPUT ?
1802 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1803}
1804
Takashi Iwaid5191e52009-11-16 14:58:17 +01001805/**
1806 * query_amp_caps - query AMP capabilities
1807 * @codec: the HD-auio codec
1808 * @nid: the NID to query
1809 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1810 *
1811 * Query AMP capabilities for the given widget and direction.
1812 * Returns the obtained capability bits.
1813 *
1814 * When cap bits have been already read, this doesn't read again but
1815 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001817u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001819 return query_caps_hash(codec, nid, direction,
1820 HDA_HASH_KEY(nid, direction, 0),
1821 read_amp_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001823EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
Takashi Iwaid5191e52009-11-16 14:58:17 +01001825/**
1826 * snd_hda_override_amp_caps - Override the AMP capabilities
1827 * @codec: the CODEC to clean up
1828 * @nid: the NID to clean up
1829 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1830 * @caps: the capability bits to set
1831 *
1832 * Override the cached AMP caps bits value by the given one.
1833 * This function is useful if the driver needs to adjust the AMP ranges,
1834 * e.g. limit to 0dB, etc.
1835 *
1836 * Returns zero if successful or a negative error code.
1837 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001838int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1839 unsigned int caps)
1840{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001841 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001842}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001843EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001844
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001845static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
1846 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001847{
1848 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1849}
1850
Takashi Iwaid5191e52009-11-16 14:58:17 +01001851/**
1852 * snd_hda_query_pin_caps - Query PIN capabilities
1853 * @codec: the HD-auio codec
1854 * @nid: the NID to query
1855 *
1856 * Query PIN capabilities for the given widget.
1857 * Returns the obtained capability bits.
1858 *
1859 * When cap bits have been already read, this doesn't read again but
1860 * returns the cached value.
1861 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001862u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1863{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001864 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001865 read_pin_cap);
1866}
Takashi Iwai1327a322009-03-23 13:07:47 +01001867EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1868
Wu Fengguang864f92b2009-11-18 12:38:02 +08001869/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02001870 * snd_hda_override_pin_caps - Override the pin capabilities
1871 * @codec: the CODEC
1872 * @nid: the NID to override
1873 * @caps: the capability bits to set
1874 *
1875 * Override the cached PIN capabilitiy bits value by the given one.
1876 *
1877 * Returns zero if successful or a negative error code.
1878 */
1879int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1880 unsigned int caps)
1881{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001882 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02001883}
1884EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps);
1885
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001886/* read or sync the hash value with the current value;
1887 * call within hash_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 */
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001889static struct hda_amp_info *
1890update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
Takashi Iwai280e57d2012-12-14 10:32:21 +01001891 int direction, int index, bool init_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001893 struct hda_amp_info *info;
1894 unsigned int parm, val = 0;
1895 bool val_read = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001897 retry:
1898 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1899 if (!info)
1900 return NULL;
1901 if (!(info->head.val & INFO_AMP_VOL(ch))) {
1902 if (!val_read) {
1903 mutex_unlock(&codec->hash_mutex);
1904 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1905 parm |= direction == HDA_OUTPUT ?
1906 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1907 parm |= index;
1908 val = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001909 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001910 val &= 0xff;
1911 val_read = true;
1912 mutex_lock(&codec->hash_mutex);
1913 goto retry;
1914 }
1915 info->vol[ch] = val;
1916 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001917 } else if (init_only)
1918 return NULL;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001919 return info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920}
1921
1922/*
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001923 * write the current volume in info to the h/w
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 */
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001925static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001926 hda_nid_t nid, int ch, int direction, int index,
1927 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928{
1929 u32 parm;
1930
1931 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1932 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1933 parm |= index << AC_AMP_SET_INDEX_SHIFT;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001934 if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) &&
1935 (amp_caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwai38681372012-02-27 15:00:58 +01001936 ; /* set the zero value as a fake mute */
1937 else
1938 parm |= val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1940}
1941
Takashi Iwaid5191e52009-11-16 14:58:17 +01001942/**
1943 * snd_hda_codec_amp_read - Read AMP value
1944 * @codec: HD-audio codec
1945 * @nid: NID to read the AMP value
1946 * @ch: channel (left=0 or right=1)
1947 * @direction: #HDA_INPUT or #HDA_OUTPUT
1948 * @index: the index value (only for input direction)
1949 *
1950 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 */
Takashi Iwai834be882006-03-01 14:16:17 +01001952int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1953 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001955 struct hda_amp_info *info;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001956 unsigned int val = 0;
1957
1958 mutex_lock(&codec->hash_mutex);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001959 info = update_amp_hash(codec, nid, ch, direction, index, false);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001960 if (info)
1961 val = info->vol[ch];
1962 mutex_unlock(&codec->hash_mutex);
1963 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001965EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
Takashi Iwai280e57d2012-12-14 10:32:21 +01001967static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1968 int direction, int idx, int mask, int val,
1969 bool init_only)
1970{
1971 struct hda_amp_info *info;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001972 unsigned int caps;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01001973 unsigned int cache_only;
Takashi Iwai280e57d2012-12-14 10:32:21 +01001974
1975 if (snd_BUG_ON(mask & ~0xff))
1976 mask &= 0xff;
1977 val &= mask;
1978
1979 mutex_lock(&codec->hash_mutex);
1980 info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
1981 if (!info) {
1982 mutex_unlock(&codec->hash_mutex);
1983 return 0;
1984 }
1985 val |= info->vol[ch] & ~mask;
1986 if (info->vol[ch] == val) {
1987 mutex_unlock(&codec->hash_mutex);
1988 return 0;
1989 }
1990 info->vol[ch] = val;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01001991 cache_only = info->head.dirty = codec->cached_write;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001992 caps = info->amp_caps;
Takashi Iwai280e57d2012-12-14 10:32:21 +01001993 mutex_unlock(&codec->hash_mutex);
Takashi Iwaide1e37b2012-12-20 11:00:21 +01001994 if (!cache_only)
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001995 put_vol_mute(codec, caps, nid, ch, direction, idx, val);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001996 return 1;
1997}
1998
Takashi Iwaid5191e52009-11-16 14:58:17 +01001999/**
2000 * snd_hda_codec_amp_update - update the AMP value
2001 * @codec: HD-audio codec
2002 * @nid: NID to read the AMP value
2003 * @ch: channel (left=0 or right=1)
2004 * @direction: #HDA_INPUT or #HDA_OUTPUT
2005 * @idx: the index value (only for input direction)
2006 * @mask: bit mask to set
2007 * @val: the bits value to set
2008 *
2009 * Update the AMP value with a bit mask.
2010 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002011 */
Takashi Iwai834be882006-03-01 14:16:17 +01002012int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
2013 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014{
Takashi Iwai280e57d2012-12-14 10:32:21 +01002015 return codec_amp_update(codec, nid, ch, direction, idx, mask, val, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002017EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
Takashi Iwaid5191e52009-11-16 14:58:17 +01002019/**
2020 * snd_hda_codec_amp_stereo - update the AMP stereo values
2021 * @codec: HD-audio codec
2022 * @nid: NID to read the AMP value
2023 * @direction: #HDA_INPUT or #HDA_OUTPUT
2024 * @idx: the index value (only for input direction)
2025 * @mask: bit mask to set
2026 * @val: the bits value to set
2027 *
2028 * Update the AMP values like snd_hda_codec_amp_update(), but for a
2029 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02002030 */
2031int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
2032 int direction, int idx, int mask, int val)
2033{
2034 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02002035
2036 if (snd_BUG_ON(mask & ~0xff))
2037 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02002038 for (ch = 0; ch < 2; ch++)
2039 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
2040 idx, mask, val);
2041 return ret;
2042}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002043EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02002044
Takashi Iwai280e57d2012-12-14 10:32:21 +01002045/* Works like snd_hda_codec_amp_update() but it writes the value only at
2046 * the first access. If the amp was already initialized / updated beforehand,
2047 * this does nothing.
2048 */
2049int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
2050 int dir, int idx, int mask, int val)
2051{
2052 return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true);
2053}
2054EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init);
2055
2056int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
2057 int dir, int idx, int mask, int val)
2058{
2059 int ch, ret = 0;
2060
2061 if (snd_BUG_ON(mask & ~0xff))
2062 mask &= 0xff;
2063 for (ch = 0; ch < 2; ch++)
2064 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
2065 idx, mask, val);
2066 return ret;
2067}
2068EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init_stereo);
2069
Takashi Iwaid5191e52009-11-16 14:58:17 +01002070/**
2071 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
2072 * @codec: HD-audio codec
2073 *
2074 * Resume the all amp commands from the cache.
2075 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002076void snd_hda_codec_resume_amp(struct hda_codec *codec)
2077{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002078 int i;
2079
Takashi Iwaic370dd62012-12-13 18:30:04 +01002080 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01002081 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002082 for (i = 0; i < codec->amp_cache.buf.used; i++) {
2083 struct hda_amp_info *buffer;
2084 u32 key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002085 hda_nid_t nid;
2086 unsigned int idx, dir, ch;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002087 struct hda_amp_info info;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002088
2089 buffer = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwai8565f052012-12-20 12:54:18 +01002090 if (!buffer->head.dirty)
2091 continue;
2092 buffer->head.dirty = 0;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002093 info = *buffer;
2094 key = info.head.key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002095 if (!key)
2096 continue;
2097 nid = key & 0xff;
2098 idx = (key >> 16) & 0xff;
2099 dir = (key >> 24) & 0xff;
2100 for (ch = 0; ch < 2; ch++) {
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002101 if (!(info.head.val & INFO_AMP_VOL(ch)))
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002102 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002103 mutex_unlock(&codec->hash_mutex);
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002104 put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx,
2105 info.vol[ch]);
Takashi Iwaic370dd62012-12-13 18:30:04 +01002106 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002107 }
2108 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01002109 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002110}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002111EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002113static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
2114 unsigned int ofs)
2115{
2116 u32 caps = query_amp_caps(codec, nid, dir);
2117 /* get num steps */
2118 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2119 if (ofs < caps)
2120 caps -= ofs;
2121 return caps;
2122}
2123
Takashi Iwaid5191e52009-11-16 14:58:17 +01002124/**
2125 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
2126 *
2127 * The control element is supposed to have the private_value field
2128 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2129 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002130int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
2131 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132{
2133 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2134 u16 nid = get_amp_nid(kcontrol);
2135 u8 chs = get_amp_channels(kcontrol);
2136 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002137 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002139 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2140 uinfo->count = chs == 3 ? 2 : 1;
2141 uinfo->value.integer.min = 0;
2142 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
2143 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002144 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01002145 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
2146 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 return -EINVAL;
2148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 return 0;
2150}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002151EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002153
2154static inline unsigned int
2155read_amp_value(struct hda_codec *codec, hda_nid_t nid,
2156 int ch, int dir, int idx, unsigned int ofs)
2157{
2158 unsigned int val;
2159 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
2160 val &= HDA_AMP_VOLMASK;
2161 if (val >= ofs)
2162 val -= ofs;
2163 else
2164 val = 0;
2165 return val;
2166}
2167
2168static inline int
2169update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2170 int ch, int dir, int idx, unsigned int ofs,
2171 unsigned int val)
2172{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002173 unsigned int maxval;
2174
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002175 if (val > 0)
2176 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02002177 /* ofs = 0: raw max value */
2178 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002179 if (val > maxval)
2180 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002181 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
2182 HDA_AMP_VOLMASK, val);
2183}
2184
Takashi Iwaid5191e52009-11-16 14:58:17 +01002185/**
2186 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
2187 *
2188 * The control element is supposed to have the private_value field
2189 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2190 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002191int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2192 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193{
2194 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2195 hda_nid_t nid = get_amp_nid(kcontrol);
2196 int chs = get_amp_channels(kcontrol);
2197 int dir = get_amp_direction(kcontrol);
2198 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002199 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 long *valp = ucontrol->value.integer.value;
2201
2202 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002203 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002205 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 return 0;
2207}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002208EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
Takashi Iwaid5191e52009-11-16 14:58:17 +01002210/**
2211 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2212 *
2213 * The control element is supposed to have the private_value field
2214 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2215 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002216int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2217 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218{
2219 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2220 hda_nid_t nid = get_amp_nid(kcontrol);
2221 int chs = get_amp_channels(kcontrol);
2222 int dir = get_amp_direction(kcontrol);
2223 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002224 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 long *valp = ucontrol->value.integer.value;
2226 int change = 0;
2227
Takashi Iwaicb53c622007-08-10 17:21:45 +02002228 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002229 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002230 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002231 valp++;
2232 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002233 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002234 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002235 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 return change;
2237}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002238EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
Takashi Iwaid5191e52009-11-16 14:58:17 +01002240/**
2241 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2242 *
2243 * The control element is supposed to have the private_value field
2244 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2245 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002246int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2247 unsigned int size, unsigned int __user *_tlv)
2248{
2249 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2250 hda_nid_t nid = get_amp_nid(kcontrol);
2251 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002252 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002253 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002254 u32 caps, val1, val2;
2255
2256 if (size < 4 * sizeof(unsigned int))
2257 return -ENOMEM;
2258 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002259 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2260 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002261 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002262 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002263 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01002264 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02002265 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002266 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2267 return -EFAULT;
2268 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2269 return -EFAULT;
2270 if (put_user(val1, _tlv + 2))
2271 return -EFAULT;
2272 if (put_user(val2, _tlv + 3))
2273 return -EFAULT;
2274 return 0;
2275}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002276EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002277
Takashi Iwaid5191e52009-11-16 14:58:17 +01002278/**
2279 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2280 * @codec: HD-audio codec
2281 * @nid: NID of a reference widget
2282 * @dir: #HDA_INPUT or #HDA_OUTPUT
2283 * @tlv: TLV data to be stored, at least 4 elements
2284 *
2285 * Set (static) TLV data for a virtual master volume using the AMP caps
2286 * obtained from the reference NID.
2287 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002288 */
2289void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2290 unsigned int *tlv)
2291{
2292 u32 caps;
2293 int nums, step;
2294
2295 caps = query_amp_caps(codec, nid, dir);
2296 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2297 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2298 step = (step + 1) * 25;
2299 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2300 tlv[1] = 2 * sizeof(unsigned int);
2301 tlv[2] = -nums * step;
2302 tlv[3] = step;
2303}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002304EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002305
2306/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002307static struct snd_kcontrol *
Takashi Iwaidcda5802012-10-12 17:24:51 +02002308find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002309{
2310 struct snd_ctl_elem_id id;
2311 memset(&id, 0, sizeof(id));
2312 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002313 id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01002314 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002315 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2316 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002317 strcpy(id.name, name);
2318 return snd_ctl_find_id(codec->bus->card, &id);
2319}
2320
Takashi Iwaid5191e52009-11-16 14:58:17 +01002321/**
2322 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2323 * @codec: HD-audio codec
2324 * @name: ctl id name string
2325 *
2326 * Get the control element with the given id string and IFACE_MIXER.
2327 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002328struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2329 const char *name)
2330{
Takashi Iwaidcda5802012-10-12 17:24:51 +02002331 return find_mixer_ctl(codec, name, 0, 0);
Takashi Iwai09f99702008-02-04 12:31:13 +01002332}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002333EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002334
Takashi Iwaidcda5802012-10-12 17:24:51 +02002335static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002336 int start_idx)
Takashi Iwai1afe2062010-12-23 10:17:52 +01002337{
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002338 int i, idx;
2339 /* 16 ctlrs should be large enough */
2340 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
2341 if (!find_mixer_ctl(codec, name, 0, idx))
Takashi Iwai1afe2062010-12-23 10:17:52 +01002342 return idx;
2343 }
2344 return -EBUSY;
2345}
2346
Takashi Iwaid5191e52009-11-16 14:58:17 +01002347/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002348 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002349 * @codec: HD-audio codec
2350 * @nid: corresponding NID (optional)
2351 * @kctl: the control element to assign
2352 *
2353 * Add the given control element to an array inside the codec instance.
2354 * All control elements belonging to a codec are supposed to be added
2355 * by this function so that a proper clean-up works at the free or
2356 * reconfiguration time.
2357 *
2358 * If non-zero @nid is passed, the NID is assigned to the control element.
2359 * The assignment is shown in the codec proc file.
2360 *
2361 * snd_hda_ctl_add() checks the control subdev id field whether
2362 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002363 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2364 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002365 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002366int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2367 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002368{
2369 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002370 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002371 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002372
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002373 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002374 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002375 if (nid == 0)
2376 nid = get_amp_nid_(kctl->private_value);
2377 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002378 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2379 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002380 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002381 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002382 err = snd_ctl_add(codec->bus->card, kctl);
2383 if (err < 0)
2384 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002385 item = snd_array_new(&codec->mixers);
2386 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002387 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002388 item->kctl = kctl;
2389 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002390 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002391 return 0;
2392}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002393EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002394
Takashi Iwaid5191e52009-11-16 14:58:17 +01002395/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002396 * snd_hda_add_nid - Assign a NID to a control element
2397 * @codec: HD-audio codec
2398 * @nid: corresponding NID (optional)
2399 * @kctl: the control element to assign
2400 * @index: index to kctl
2401 *
2402 * Add the given control element to an array inside the codec instance.
2403 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2404 * NID:KCTL mapping - for example "Capture Source" selector.
2405 */
2406int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2407 unsigned int index, hda_nid_t nid)
2408{
2409 struct hda_nid_item *item;
2410
2411 if (nid > 0) {
2412 item = snd_array_new(&codec->nids);
2413 if (!item)
2414 return -ENOMEM;
2415 item->kctl = kctl;
2416 item->index = index;
2417 item->nid = nid;
2418 return 0;
2419 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01002420 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2421 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002422 return -EINVAL;
2423}
2424EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2425
2426/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002427 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2428 * @codec: HD-audio codec
2429 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002430void snd_hda_ctls_clear(struct hda_codec *codec)
2431{
2432 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002433 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002434 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002435 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002436 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002437 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002438}
2439
Takashi Iwaia65d6292009-02-23 16:57:04 +01002440/* pseudo device locking
2441 * toggle card->shutdown to allow/disallow the device access (as a hack)
2442 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002443int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002444{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002445 struct snd_card *card = bus->card;
2446 struct hda_codec *codec;
2447
Takashi Iwaia65d6292009-02-23 16:57:04 +01002448 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002449 if (card->shutdown)
2450 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002451 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002452 if (!list_empty(&card->ctl_files))
2453 goto err_clear;
2454
2455 list_for_each_entry(codec, &bus->codec_list, list) {
2456 int pcm;
2457 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2458 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2459 if (!cpcm->pcm)
2460 continue;
2461 if (cpcm->pcm->streams[0].substream_opened ||
2462 cpcm->pcm->streams[1].substream_opened)
2463 goto err_clear;
2464 }
2465 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01002466 spin_unlock(&card->files_lock);
2467 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002468
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002469 err_clear:
2470 card->shutdown = 0;
2471 err_unlock:
2472 spin_unlock(&card->files_lock);
2473 return -EINVAL;
2474}
2475EXPORT_SYMBOL_HDA(snd_hda_lock_devices);
2476
2477void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002478{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002479 struct snd_card *card = bus->card;
2480
2481 card = bus->card;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002482 spin_lock(&card->files_lock);
2483 card->shutdown = 0;
2484 spin_unlock(&card->files_lock);
2485}
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002486EXPORT_SYMBOL_HDA(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002487
Takashi Iwaid5191e52009-11-16 14:58:17 +01002488/**
2489 * snd_hda_codec_reset - Clear all objects assigned to the codec
2490 * @codec: HD-audio codec
2491 *
2492 * This frees the all PCM and control elements assigned to the codec, and
2493 * clears the caches and restores the pin default configurations.
2494 *
2495 * When a device is being used, it returns -EBSY. If successfully freed,
2496 * returns zero.
2497 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002498int snd_hda_codec_reset(struct hda_codec *codec)
2499{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002500 struct hda_bus *bus = codec->bus;
2501 struct snd_card *card = bus->card;
2502 int i;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002503
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002504 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002505 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002506
2507 /* OK, let it free */
David Henningsson26a6cb62012-10-09 15:04:21 +02002508 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai83012a72012-08-24 18:38:08 +02002509#ifdef CONFIG_PM
Takashi Iwaia2d96e72012-05-09 12:36:22 +02002510 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwai339876d2012-05-08 16:57:12 +02002511 codec->power_on = 0;
2512 codec->power_transition = 0;
2513 codec->power_jiffies = jiffies;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002514 flush_workqueue(bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002515#endif
2516 snd_hda_ctls_clear(codec);
2517 /* relase PCMs */
2518 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002519 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002520 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002521 clear_bit(codec->pcm_info[i].device,
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002522 bus->pcm_dev_bits);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002523 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002524 }
2525 if (codec->patch_ops.free)
2526 codec->patch_ops.free(codec);
Takashi Iwai07dc59f2012-09-10 09:39:31 +02002527 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002528 snd_hda_jack_tbl_clear(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002529 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002530 codec->spec = NULL;
2531 free_hda_cache(&codec->amp_cache);
2532 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002533 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2534 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002535 /* free only driver_pins so that init_pins + user_pins are restored */
2536 snd_array_free(&codec->driver_pins);
Takashi Iwai09a60712012-06-26 15:01:33 +02002537 snd_array_free(&codec->cvt_setups);
2538 snd_array_free(&codec->spdif_out);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01002539 snd_array_free(&codec->verbs);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002540 codec->num_pcms = 0;
2541 codec->pcm_info = NULL;
2542 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002543 codec->slave_dig_outs = NULL;
2544 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002545 module_put(codec->owner);
2546 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002547
2548 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002549 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002550 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002551}
2552
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002553typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2554
2555/* apply the function to all matching slave ctls in the mixer list */
2556static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002557 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002558{
2559 struct hda_nid_item *items;
2560 const char * const *s;
2561 int i, err;
2562
2563 items = codec->mixers.list;
2564 for (i = 0; i < codec->mixers.used; i++) {
2565 struct snd_kcontrol *sctl = items[i].kctl;
2566 if (!sctl || !sctl->id.name ||
2567 sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2568 continue;
2569 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002570 char tmpname[sizeof(sctl->id.name)];
2571 const char *name = *s;
2572 if (suffix) {
2573 snprintf(tmpname, sizeof(tmpname), "%s %s",
2574 name, suffix);
2575 name = tmpname;
2576 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002577 if (!strcmp(sctl->id.name, name)) {
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002578 err = func(data, sctl);
2579 if (err)
2580 return err;
2581 break;
2582 }
2583 }
2584 }
2585 return 0;
2586}
2587
2588static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2589{
2590 return 1;
2591}
2592
Takashi Iwai18478e82012-03-09 17:51:10 +01002593/* guess the value corresponding to 0dB */
2594static int get_kctl_0dB_offset(struct snd_kcontrol *kctl)
2595{
2596 int _tlv[4];
2597 const int *tlv = NULL;
2598 int val = -1;
2599
2600 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2601 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2602 mm_segment_t fs = get_fs();
2603 set_fs(get_ds());
2604 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2605 tlv = _tlv;
2606 set_fs(fs);
2607 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2608 tlv = kctl->tlv.p;
2609 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE)
2610 val = -tlv[2] / tlv[3];
2611 return val;
2612}
2613
2614/* call kctl->put with the given value(s) */
2615static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2616{
2617 struct snd_ctl_elem_value *ucontrol;
2618 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2619 if (!ucontrol)
2620 return -ENOMEM;
2621 ucontrol->value.integer.value[0] = val;
2622 ucontrol->value.integer.value[1] = val;
2623 kctl->put(kctl, ucontrol);
2624 kfree(ucontrol);
2625 return 0;
2626}
2627
2628/* initialize the slave volume with 0dB */
2629static int init_slave_0dB(void *data, struct snd_kcontrol *slave)
2630{
2631 int offset = get_kctl_0dB_offset(slave);
2632 if (offset > 0)
2633 put_kctl_with_value(slave, offset);
2634 return 0;
2635}
2636
2637/* unmute the slave */
2638static int init_slave_unmute(void *data, struct snd_kcontrol *slave)
2639{
2640 return put_kctl_with_value(slave, 1);
2641}
2642
Takashi Iwaid5191e52009-11-16 14:58:17 +01002643/**
2644 * snd_hda_add_vmaster - create a virtual master control and add slaves
2645 * @codec: HD-audio codec
2646 * @name: vmaster control name
2647 * @tlv: TLV data (optional)
2648 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002649 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002650 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002651 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002652 *
2653 * Create a virtual master control with the given name. The TLV data
2654 * must be either NULL or a valid data.
2655 *
2656 * @slaves is a NULL-terminated array of strings, each of which is a
2657 * slave control name. All controls with these names are assigned to
2658 * the new virtual master control.
2659 *
2660 * This function returns zero if successful or a negative error code.
2661 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002662int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002663 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002664 const char *suffix, bool init_slave_vol,
2665 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002666{
2667 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002668 int err;
2669
Takashi Iwai29e58532012-03-12 12:25:03 +01002670 if (ctl_ret)
2671 *ctl_ret = NULL;
2672
Takashi Iwai9322ca52012-02-03 14:28:01 +01002673 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002674 if (err != 1) {
Takashi Iwai2f085542008-02-22 18:43:50 +01002675 snd_printdd("No slave found for %s\n", name);
2676 return 0;
2677 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002678 kctl = snd_ctl_make_virtual_master(name, tlv);
2679 if (!kctl)
2680 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002681 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002682 if (err < 0)
2683 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002684
Takashi Iwai9322ca52012-02-03 14:28:01 +01002685 err = map_slaves(codec, slaves, suffix,
2686 (map_slave_func_t)snd_ctl_add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002687 if (err < 0)
2688 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002689
2690 /* init with master mute & zero volume */
2691 put_kctl_with_value(kctl, 0);
2692 if (init_slave_vol)
2693 map_slaves(codec, slaves, suffix,
2694 tlv ? init_slave_0dB : init_slave_unmute, kctl);
2695
Takashi Iwai29e58532012-03-12 12:25:03 +01002696 if (ctl_ret)
2697 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002698 return 0;
2699}
Takashi Iwai18478e82012-03-09 17:51:10 +01002700EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002701
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002702/*
2703 * mute-LED control using vmaster
2704 */
2705static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2706 struct snd_ctl_elem_info *uinfo)
2707{
2708 static const char * const texts[] = {
David Henningssonc86c2d42013-01-03 14:12:29 +01002709 "On", "Off", "Follow Master"
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002710 };
2711 unsigned int index;
2712
2713 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2714 uinfo->count = 1;
2715 uinfo->value.enumerated.items = 3;
2716 index = uinfo->value.enumerated.item;
2717 if (index >= 3)
2718 index = 2;
2719 strcpy(uinfo->value.enumerated.name, texts[index]);
2720 return 0;
2721}
2722
2723static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2724 struct snd_ctl_elem_value *ucontrol)
2725{
2726 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2727 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2728 return 0;
2729}
2730
2731static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2732 struct snd_ctl_elem_value *ucontrol)
2733{
2734 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2735 unsigned int old_mode = hook->mute_mode;
2736
2737 hook->mute_mode = ucontrol->value.enumerated.item[0];
2738 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2739 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2740 if (old_mode == hook->mute_mode)
2741 return 0;
2742 snd_hda_sync_vmaster_hook(hook);
2743 return 1;
2744}
2745
2746static struct snd_kcontrol_new vmaster_mute_mode = {
2747 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2748 .name = "Mute-LED Mode",
2749 .info = vmaster_mute_mode_info,
2750 .get = vmaster_mute_mode_get,
2751 .put = vmaster_mute_mode_put,
2752};
2753
2754/*
2755 * Add a mute-LED hook with the given vmaster switch kctl
2756 * "Mute-LED Mode" control is automatically created and associated with
2757 * the given hook.
2758 */
2759int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735c2012-03-13 07:55:10 +01002760 struct hda_vmaster_mute_hook *hook,
2761 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002762{
2763 struct snd_kcontrol *kctl;
2764
2765 if (!hook->hook || !hook->sw_kctl)
2766 return 0;
2767 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2768 hook->codec = codec;
2769 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735c2012-03-13 07:55:10 +01002770 if (!expose_enum_ctl)
2771 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002772 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2773 if (!kctl)
2774 return -ENOMEM;
2775 return snd_hda_ctl_add(codec, 0, kctl);
2776}
2777EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook);
2778
2779/*
2780 * Call the hook with the current value for synchronization
2781 * Should be called in init callback
2782 */
2783void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2784{
2785 if (!hook->hook || !hook->codec)
2786 return;
Takashi Iwai594813f2013-04-17 18:16:05 +02002787 /* don't call vmaster hook in the destructor since it might have
2788 * been already destroyed
2789 */
2790 if (hook->codec->bus->shutdown)
2791 return;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002792 switch (hook->mute_mode) {
2793 case HDA_VMUTE_FOLLOW_MASTER:
2794 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2795 break;
2796 default:
2797 hook->hook(hook->codec, hook->mute_mode);
2798 break;
2799 }
2800}
2801EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook);
2802
2803
Takashi Iwaid5191e52009-11-16 14:58:17 +01002804/**
2805 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2806 *
2807 * The control element is supposed to have the private_value field
2808 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2809 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002810int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2811 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812{
2813 int chs = get_amp_channels(kcontrol);
2814
2815 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2816 uinfo->count = chs == 3 ? 2 : 1;
2817 uinfo->value.integer.min = 0;
2818 uinfo->value.integer.max = 1;
2819 return 0;
2820}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002821EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822
Takashi Iwaid5191e52009-11-16 14:58:17 +01002823/**
2824 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2825 *
2826 * The control element is supposed to have the private_value field
2827 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2828 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002829int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2830 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831{
2832 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2833 hda_nid_t nid = get_amp_nid(kcontrol);
2834 int chs = get_amp_channels(kcontrol);
2835 int dir = get_amp_direction(kcontrol);
2836 int idx = get_amp_index(kcontrol);
2837 long *valp = ucontrol->value.integer.value;
2838
2839 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002840 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002841 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002843 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002844 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 return 0;
2846}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002847EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848
Takashi Iwaid5191e52009-11-16 14:58:17 +01002849/**
2850 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2851 *
2852 * The control element is supposed to have the private_value field
2853 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2854 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002855int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2856 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857{
2858 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2859 hda_nid_t nid = get_amp_nid(kcontrol);
2860 int chs = get_amp_channels(kcontrol);
2861 int dir = get_amp_direction(kcontrol);
2862 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 long *valp = ucontrol->value.integer.value;
2864 int change = 0;
2865
Takashi Iwaicb53c622007-08-10 17:21:45 +02002866 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002867 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002868 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002869 HDA_AMP_MUTE,
2870 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002871 valp++;
2872 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002873 if (chs & 2)
2874 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002875 HDA_AMP_MUTE,
2876 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002877 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002878 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 return change;
2880}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002881EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882
2883/*
Takashi Iwai985be542005-11-02 18:26:49 +01002884 * bound volume controls
2885 *
2886 * bind multiple volumes (# indices, from 0)
2887 */
2888
2889#define AMP_VAL_IDX_SHIFT 19
2890#define AMP_VAL_IDX_MASK (0x0f<<19)
2891
Takashi Iwaid5191e52009-11-16 14:58:17 +01002892/**
2893 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2894 *
2895 * The control element is supposed to have the private_value field
2896 * set up via HDA_BIND_MUTE*() macros.
2897 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002898int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2899 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002900{
2901 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2902 unsigned long pval;
2903 int err;
2904
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002905 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002906 pval = kcontrol->private_value;
2907 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2908 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2909 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002910 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002911 return err;
2912}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002913EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002914
Takashi Iwaid5191e52009-11-16 14:58:17 +01002915/**
2916 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2917 *
2918 * The control element is supposed to have the private_value field
2919 * set up via HDA_BIND_MUTE*() macros.
2920 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002921int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2922 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002923{
2924 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2925 unsigned long pval;
2926 int i, indices, err = 0, change = 0;
2927
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002928 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002929 pval = kcontrol->private_value;
2930 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2931 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002932 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2933 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002934 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2935 if (err < 0)
2936 break;
2937 change |= err;
2938 }
2939 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002940 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002941 return err < 0 ? err : change;
2942}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002943EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002944
Takashi Iwaid5191e52009-11-16 14:58:17 +01002945/**
2946 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2947 *
2948 * The control element is supposed to have the private_value field
2949 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002950 */
2951int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2952 struct snd_ctl_elem_info *uinfo)
2953{
2954 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2955 struct hda_bind_ctls *c;
2956 int err;
2957
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002958 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002959 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002960 kcontrol->private_value = *c->values;
2961 err = c->ops->info(kcontrol, uinfo);
2962 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002963 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002964 return err;
2965}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002966EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002967
Takashi Iwaid5191e52009-11-16 14:58:17 +01002968/**
2969 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2970 *
2971 * The control element is supposed to have the private_value field
2972 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2973 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002974int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2975 struct snd_ctl_elem_value *ucontrol)
2976{
2977 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2978 struct hda_bind_ctls *c;
2979 int err;
2980
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002981 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002982 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002983 kcontrol->private_value = *c->values;
2984 err = c->ops->get(kcontrol, ucontrol);
2985 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002986 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002987 return err;
2988}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002989EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002990
Takashi Iwaid5191e52009-11-16 14:58:17 +01002991/**
2992 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2993 *
2994 * The control element is supposed to have the private_value field
2995 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2996 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002997int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2998 struct snd_ctl_elem_value *ucontrol)
2999{
3000 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3001 struct hda_bind_ctls *c;
3002 unsigned long *vals;
3003 int err = 0, change = 0;
3004
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003005 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003006 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003007 for (vals = c->values; *vals; vals++) {
3008 kcontrol->private_value = *vals;
3009 err = c->ops->put(kcontrol, ucontrol);
3010 if (err < 0)
3011 break;
3012 change |= err;
3013 }
3014 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003015 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003016 return err < 0 ? err : change;
3017}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003018EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02003019
Takashi Iwaid5191e52009-11-16 14:58:17 +01003020/**
3021 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
3022 *
3023 * The control element is supposed to have the private_value field
3024 * set up via HDA_BIND_VOL() macro.
3025 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003026int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
3027 unsigned int size, unsigned int __user *tlv)
3028{
3029 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3030 struct hda_bind_ctls *c;
3031 int err;
3032
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003033 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003034 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003035 kcontrol->private_value = *c->values;
3036 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
3037 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003038 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003039 return err;
3040}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003041EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02003042
3043struct hda_ctl_ops snd_hda_bind_vol = {
3044 .info = snd_hda_mixer_amp_volume_info,
3045 .get = snd_hda_mixer_amp_volume_get,
3046 .put = snd_hda_mixer_amp_volume_put,
3047 .tlv = snd_hda_mixer_amp_tlv
3048};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003049EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02003050
3051struct hda_ctl_ops snd_hda_bind_sw = {
3052 .info = snd_hda_mixer_amp_switch_info,
3053 .get = snd_hda_mixer_amp_switch_get,
3054 .put = snd_hda_mixer_amp_switch_put,
3055 .tlv = snd_hda_mixer_amp_tlv
3056};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003057EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02003058
3059/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 * SPDIF out controls
3061 */
3062
Takashi Iwai0ba21762007-04-16 11:29:14 +02003063static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
3064 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065{
3066 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
3067 uinfo->count = 1;
3068 return 0;
3069}
3070
Takashi Iwai0ba21762007-04-16 11:29:14 +02003071static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
3072 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073{
3074 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3075 IEC958_AES0_NONAUDIO |
3076 IEC958_AES0_CON_EMPHASIS_5015 |
3077 IEC958_AES0_CON_NOT_COPYRIGHT;
3078 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
3079 IEC958_AES1_CON_ORIGINAL;
3080 return 0;
3081}
3082
Takashi Iwai0ba21762007-04-16 11:29:14 +02003083static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
3084 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085{
3086 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3087 IEC958_AES0_NONAUDIO |
3088 IEC958_AES0_PRO_EMPHASIS_5015;
3089 return 0;
3090}
3091
Takashi Iwai0ba21762007-04-16 11:29:14 +02003092static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
3093 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094{
3095 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003096 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003097 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003099 mutex_lock(&codec->spdif_mutex);
3100 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003101 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
3102 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
3103 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
3104 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003105 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106
3107 return 0;
3108}
3109
3110/* convert from SPDIF status bits to HDA SPDIF bits
3111 * bit 0 (DigEn) is always set zero (to be filled later)
3112 */
3113static unsigned short convert_from_spdif_status(unsigned int sbits)
3114{
3115 unsigned short val = 0;
3116
3117 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003118 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003120 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003122 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
3123 IEC958_AES0_PRO_EMPHASIS_5015)
3124 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003126 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
3127 IEC958_AES0_CON_EMPHASIS_5015)
3128 val |= AC_DIG1_EMPHASIS;
3129 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
3130 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02003132 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
3134 }
3135 return val;
3136}
3137
3138/* convert to SPDIF status bits from HDA SPDIF bits
3139 */
3140static unsigned int convert_to_spdif_status(unsigned short val)
3141{
3142 unsigned int sbits = 0;
3143
Takashi Iwai0ba21762007-04-16 11:29:14 +02003144 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003146 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 sbits |= IEC958_AES0_PROFESSIONAL;
3148 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwaia686fd12013-03-20 15:42:00 +01003149 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
3151 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003152 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003154 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003156 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3158 sbits |= val & (0x7f << 8);
3159 }
3160 return sbits;
3161}
3162
Takashi Iwai2f728532008-09-25 16:32:41 +02003163/* set digital convert verbs both for the given NID and its slaves */
3164static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3165 int verb, int val)
3166{
Takashi Iwaidda14412011-05-02 11:29:30 +02003167 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02003168
Takashi Iwai9e976972008-11-25 08:17:20 +01003169 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003170 d = codec->slave_dig_outs;
3171 if (!d)
3172 return;
3173 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01003174 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003175}
3176
3177static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3178 int dig1, int dig2)
3179{
3180 if (dig1 != -1)
3181 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3182 if (dig2 != -1)
3183 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3184}
3185
Takashi Iwai0ba21762007-04-16 11:29:14 +02003186static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3187 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188{
3189 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003190 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003191 struct hda_spdif_out *spdif;
3192 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 unsigned short val;
3194 int change;
3195
Ingo Molnar62932df2006-01-16 16:34:20 +01003196 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003197 spdif = snd_array_elem(&codec->spdif_out, idx);
3198 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003199 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3201 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3202 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c935972011-06-01 11:14:17 -06003203 val = convert_from_spdif_status(spdif->status);
3204 val |= spdif->ctls & 1;
3205 change = spdif->ctls != val;
3206 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003207 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02003208 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01003209 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 return change;
3211}
3212
Takashi Iwaia5ce8892007-07-23 15:42:26 +02003213#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214
Takashi Iwai0ba21762007-04-16 11:29:14 +02003215static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3216 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217{
3218 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003219 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003220 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003222 mutex_lock(&codec->spdif_mutex);
3223 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003224 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003225 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 return 0;
3227}
3228
Stephen Warren74b654c2011-06-01 11:14:18 -06003229static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3230 int dig1, int dig2)
3231{
3232 set_dig_out_convert(codec, nid, dig1, dig2);
3233 /* unmute amp switch (if any) */
3234 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3235 (dig1 & AC_DIG1_ENABLE))
3236 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3237 HDA_AMP_MUTE, 0);
3238}
3239
Takashi Iwai0ba21762007-04-16 11:29:14 +02003240static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3241 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242{
3243 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003244 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003245 struct hda_spdif_out *spdif;
3246 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 unsigned short val;
3248 int change;
3249
Ingo Molnar62932df2006-01-16 16:34:20 +01003250 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003251 spdif = snd_array_elem(&codec->spdif_out, idx);
3252 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003253 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003255 val |= AC_DIG1_ENABLE;
Stephen Warren7c935972011-06-01 11:14:17 -06003256 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003257 spdif->ctls = val;
3258 if (change && nid != (u16)-1)
3259 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003260 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 return change;
3262}
3263
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003264static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 {
3266 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3267 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003268 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 .info = snd_hda_spdif_mask_info,
3270 .get = snd_hda_spdif_cmask_get,
3271 },
3272 {
3273 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3274 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003275 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 .info = snd_hda_spdif_mask_info,
3277 .get = snd_hda_spdif_pmask_get,
3278 },
3279 {
3280 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003281 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 .info = snd_hda_spdif_mask_info,
3283 .get = snd_hda_spdif_default_get,
3284 .put = snd_hda_spdif_default_put,
3285 },
3286 {
3287 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003288 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 .info = snd_hda_spdif_out_switch_info,
3290 .get = snd_hda_spdif_out_switch_get,
3291 .put = snd_hda_spdif_out_switch_put,
3292 },
3293 { } /* end */
3294};
3295
3296/**
Takashi Iwaidcda5802012-10-12 17:24:51 +02003297 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 * @codec: the HDA codec
Takashi Iwaidcda5802012-10-12 17:24:51 +02003299 * @associated_nid: NID that new ctls associated with
3300 * @cvt_nid: converter NID
3301 * @type: HDA_PCM_TYPE_*
3302 * Creates controls related with the digital output.
3303 * Called from each patch supporting the digital out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 *
3305 * Returns 0 if successful, or a negative error code.
3306 */
Takashi Iwaidcda5802012-10-12 17:24:51 +02003307int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3308 hda_nid_t associated_nid,
3309 hda_nid_t cvt_nid,
3310 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311{
3312 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003313 struct snd_kcontrol *kctl;
3314 struct snd_kcontrol_new *dig_mix;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003315 int idx = 0;
3316 const int spdif_index = 16;
Stephen Warren7c935972011-06-01 11:14:17 -06003317 struct hda_spdif_out *spdif;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003318 struct hda_bus *bus = codec->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003320 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003321 type == HDA_PCM_TYPE_SPDIF) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003322 idx = spdif_index;
3323 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003324 type == HDA_PCM_TYPE_HDMI) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003325 /* suppose a single SPDIF device */
3326 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3327 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
3328 if (!kctl)
3329 break;
3330 kctl->id.index = spdif_index;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003331 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003332 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003333 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003334 if (!bus->primary_dig_out_type)
3335 bus->primary_dig_out_type = type;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003336
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003337 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003338 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003339 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
3340 return -EBUSY;
3341 }
Stephen Warren7c935972011-06-01 11:14:17 -06003342 spdif = snd_array_new(&codec->spdif_out);
Mengdong Lin25336e82013-03-07 14:10:25 -05003343 if (!spdif)
3344 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3346 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003347 if (!kctl)
3348 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01003349 kctl->id.index = idx;
Stephen Warren7c935972011-06-01 11:14:17 -06003350 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003351 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003352 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 return err;
3354 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003355 spdif->nid = cvt_nid;
3356 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c935972011-06-01 11:14:17 -06003357 AC_VERB_GET_DIGI_CONVERT_1, 0);
3358 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 return 0;
3360}
Takashi Iwaidcda5802012-10-12 17:24:51 +02003361EXPORT_SYMBOL_HDA(snd_hda_create_dig_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003363/* get the hda_spdif_out entry from the given NID
3364 * call within spdif_mutex lock
3365 */
Stephen Warren7c935972011-06-01 11:14:17 -06003366struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3367 hda_nid_t nid)
3368{
3369 int i;
3370 for (i = 0; i < codec->spdif_out.used; i++) {
3371 struct hda_spdif_out *spdif =
3372 snd_array_elem(&codec->spdif_out, i);
3373 if (spdif->nid == nid)
3374 return spdif;
3375 }
3376 return NULL;
3377}
3378EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
3379
Stephen Warren74b654c2011-06-01 11:14:18 -06003380void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3381{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003382 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003383
3384 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003385 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003386 spdif->nid = (u16)-1;
3387 mutex_unlock(&codec->spdif_mutex);
3388}
3389EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
3390
3391void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3392{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003393 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003394 unsigned short val;
3395
3396 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003397 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003398 if (spdif->nid != nid) {
3399 spdif->nid = nid;
3400 val = spdif->ctls;
3401 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3402 }
3403 mutex_unlock(&codec->spdif_mutex);
3404}
3405EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
3406
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003408 * SPDIF sharing with analog output
3409 */
3410static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3411 struct snd_ctl_elem_value *ucontrol)
3412{
3413 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3414 ucontrol->value.integer.value[0] = mout->share_spdif;
3415 return 0;
3416}
3417
3418static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3419 struct snd_ctl_elem_value *ucontrol)
3420{
3421 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3422 mout->share_spdif = !!ucontrol->value.integer.value[0];
3423 return 0;
3424}
3425
3426static struct snd_kcontrol_new spdif_share_sw = {
3427 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3428 .name = "IEC958 Default PCM Playback Switch",
3429 .info = snd_ctl_boolean_mono_info,
3430 .get = spdif_share_sw_get,
3431 .put = spdif_share_sw_put,
3432};
3433
Takashi Iwaid5191e52009-11-16 14:58:17 +01003434/**
3435 * snd_hda_create_spdif_share_sw - create Default PCM switch
3436 * @codec: the HDA codec
3437 * @mout: multi-out instance
3438 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003439int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3440 struct hda_multi_out *mout)
3441{
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003442 struct snd_kcontrol *kctl;
3443
Takashi Iwai9a081602008-02-12 18:37:26 +01003444 if (!mout->dig_out_nid)
3445 return 0;
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003446
3447 kctl = snd_ctl_new1(&spdif_share_sw, mout);
3448 if (!kctl)
3449 return -ENOMEM;
Takashi Iwai9a081602008-02-12 18:37:26 +01003450 /* ATTENTION: here mout is passed as private_data, instead of codec */
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003451 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
Takashi Iwai9a081602008-02-12 18:37:26 +01003452}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003453EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003454
3455/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 * SPDIF input
3457 */
3458
3459#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3460
Takashi Iwai0ba21762007-04-16 11:29:14 +02003461static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3462 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463{
3464 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3465
3466 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3467 return 0;
3468}
3469
Takashi Iwai0ba21762007-04-16 11:29:14 +02003470static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3471 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472{
3473 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3474 hda_nid_t nid = kcontrol->private_value;
3475 unsigned int val = !!ucontrol->value.integer.value[0];
3476 int change;
3477
Ingo Molnar62932df2006-01-16 16:34:20 +01003478 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003480 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003482 snd_hda_codec_write_cache(codec, nid, 0,
3483 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003485 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 return change;
3487}
3488
Takashi Iwai0ba21762007-04-16 11:29:14 +02003489static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3490 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491{
3492 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3493 hda_nid_t nid = kcontrol->private_value;
3494 unsigned short val;
3495 unsigned int sbits;
3496
Andrew Paprocki3982d172007-12-19 12:13:44 +01003497 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 sbits = convert_to_spdif_status(val);
3499 ucontrol->value.iec958.status[0] = sbits;
3500 ucontrol->value.iec958.status[1] = sbits >> 8;
3501 ucontrol->value.iec958.status[2] = sbits >> 16;
3502 ucontrol->value.iec958.status[3] = sbits >> 24;
3503 return 0;
3504}
3505
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003506static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 {
3508 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003509 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 .info = snd_hda_spdif_in_switch_info,
3511 .get = snd_hda_spdif_in_switch_get,
3512 .put = snd_hda_spdif_in_switch_put,
3513 },
3514 {
3515 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3516 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003517 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 .info = snd_hda_spdif_mask_info,
3519 .get = snd_hda_spdif_in_status_get,
3520 },
3521 { } /* end */
3522};
3523
3524/**
3525 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3526 * @codec: the HDA codec
3527 * @nid: audio in widget NID
3528 *
3529 * Creates controls related with the SPDIF input.
3530 * Called from each patch supporting the SPDIF in.
3531 *
3532 * Returns 0 if successful, or a negative error code.
3533 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003534int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535{
3536 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003537 struct snd_kcontrol *kctl;
3538 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003539 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540
Takashi Iwaidcda5802012-10-12 17:24:51 +02003541 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003542 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003543 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3544 return -EBUSY;
3545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3547 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003548 if (!kctl)
3549 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003551 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003552 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 return err;
3554 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003555 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003556 snd_hda_codec_read(codec, nid, 0,
3557 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003558 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 return 0;
3560}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003561EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003563/*
3564 * command cache
3565 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566
Takashi Iwaic370dd62012-12-13 18:30:04 +01003567/* build a 31bit cache key with the widget id and the command parameter */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003568#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3569#define get_cmd_cache_nid(key) ((key) & 0xff)
3570#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3571
3572/**
3573 * snd_hda_codec_write_cache - send a single command with caching
3574 * @codec: the HDA codec
3575 * @nid: NID to send the command
3576 * @direct: direct flag
3577 * @verb: the verb to send
3578 * @parm: the parameter for the verb
3579 *
3580 * Send a single command without waiting for response.
3581 *
3582 * Returns 0 if successful, or a negative error code.
3583 */
3584int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3585 int direct, unsigned int verb, unsigned int parm)
3586{
Takashi Iwaic370dd62012-12-13 18:30:04 +01003587 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003588 struct hda_cache_head *c;
3589 u32 key;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003590 unsigned int cache_only;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003591
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003592 cache_only = codec->cached_write;
3593 if (!cache_only) {
Takashi Iwaic370dd62012-12-13 18:30:04 +01003594 err = snd_hda_codec_write(codec, nid, direct, verb, parm);
3595 if (err < 0)
3596 return err;
3597 }
3598
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003599 /* parm may contain the verb stuff for get/set amp */
3600 verb = verb | (parm >> 8);
3601 parm &= 0xff;
3602 key = build_cmd_cache_key(nid, verb);
3603 mutex_lock(&codec->bus->cmd_mutex);
3604 c = get_alloc_hash(&codec->cmd_cache, key);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003605 if (c) {
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003606 c->val = parm;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003607 c->dirty = cache_only;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003608 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003609 mutex_unlock(&codec->bus->cmd_mutex);
3610 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003611}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003612EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003613
Takashi Iwaid5191e52009-11-16 14:58:17 +01003614/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003615 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3616 * @codec: the HDA codec
3617 * @nid: NID to send the command
3618 * @direct: direct flag
3619 * @verb: the verb to send
3620 * @parm: the parameter for the verb
3621 *
3622 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3623 * command if the parameter is already identical with the cached value.
3624 * If not, it sends the command and refreshes the cache.
3625 *
3626 * Returns 0 if successful, or a negative error code.
3627 */
3628int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3629 int direct, unsigned int verb, unsigned int parm)
3630{
3631 struct hda_cache_head *c;
3632 u32 key;
3633
3634 /* parm may contain the verb stuff for get/set amp */
3635 verb = verb | (parm >> 8);
3636 parm &= 0xff;
3637 key = build_cmd_cache_key(nid, verb);
3638 mutex_lock(&codec->bus->cmd_mutex);
3639 c = get_hash(&codec->cmd_cache, key);
3640 if (c && c->val == parm) {
3641 mutex_unlock(&codec->bus->cmd_mutex);
3642 return 0;
3643 }
3644 mutex_unlock(&codec->bus->cmd_mutex);
3645 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3646}
3647EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3648
3649/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003650 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3651 * @codec: HD-audio codec
3652 *
3653 * Execute all verbs recorded in the command caches to resume.
3654 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003655void snd_hda_codec_resume_cache(struct hda_codec *codec)
3656{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003657 int i;
3658
Takashi Iwaic370dd62012-12-13 18:30:04 +01003659 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01003660 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003661 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3662 struct hda_cache_head *buffer;
3663 u32 key;
3664
3665 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3666 key = buffer->key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003667 if (!key)
3668 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003669 if (!buffer->dirty)
3670 continue;
3671 buffer->dirty = 0;
3672 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003673 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3674 get_cmd_cache_cmd(key), buffer->val);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003675 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003676 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01003677 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003678}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003679EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003680
3681/**
3682 * snd_hda_sequence_write_cache - sequence writes with caching
3683 * @codec: the HDA codec
3684 * @seq: VERB array to send
3685 *
3686 * Send the commands sequentially from the given array.
3687 * Thte commands are recorded on cache for power-save and resume.
3688 * The array must be terminated with NID=0.
3689 */
3690void snd_hda_sequence_write_cache(struct hda_codec *codec,
3691 const struct hda_verb *seq)
3692{
3693 for (; seq->nid; seq++)
3694 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3695 seq->param);
3696}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003697EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003698
Takashi Iwaidc870f32013-01-22 15:24:30 +01003699/**
3700 * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
3701 * @codec: HD-audio codec
3702 */
3703void snd_hda_codec_flush_cache(struct hda_codec *codec)
3704{
3705 snd_hda_codec_resume_amp(codec);
3706 snd_hda_codec_resume_cache(codec);
3707}
3708EXPORT_SYMBOL_HDA(snd_hda_codec_flush_cache);
3709
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003710void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003711 unsigned int power_state)
Takashi Iwai54d17402005-11-21 16:33:22 +01003712{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003713 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003714 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003715
Takashi Iwaicb53c622007-08-10 17:21:45 +02003716 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003717 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003718 unsigned int state = power_state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003719 if (!(wcaps & AC_WCAP_POWER))
3720 continue;
Takashi Iwai9419ab62013-01-24 17:23:35 +01003721 if (codec->power_filter) {
3722 state = codec->power_filter(codec, nid, power_state);
3723 if (state != power_state && power_state == AC_PWRST_D3)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003724 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003725 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003726 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003727 state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003728 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003729}
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003730EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3731
3732/*
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003733 * supported power states check
3734 */
3735static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
3736 unsigned int power_state)
3737{
3738 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
3739
Mengdong Line037cb42012-08-10 14:11:58 +02003740 if (sup == -1)
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003741 return false;
3742 if (sup & power_state)
3743 return true;
3744 else
3745 return false;
3746}
3747
3748/*
Takashi Iwai432c6412012-08-28 09:59:20 -07003749 * wait until the state is reached, returns the current state
3750 */
3751static unsigned int hda_sync_power_state(struct hda_codec *codec,
3752 hda_nid_t fg,
3753 unsigned int power_state)
3754{
3755 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3756 unsigned int state, actual_state;
3757
3758 for (;;) {
3759 state = snd_hda_codec_read(codec, fg, 0,
3760 AC_VERB_GET_POWER_STATE, 0);
3761 if (state & AC_PWRST_ERROR)
3762 break;
3763 actual_state = (state >> 4) & 0x0f;
3764 if (actual_state == power_state)
3765 break;
3766 if (time_after_eq(jiffies, end_time))
3767 break;
3768 /* wait until the codec reachs to the target state */
3769 msleep(1);
3770 }
3771 return state;
3772}
3773
Takashi Iwai9419ab62013-01-24 17:23:35 +01003774/* don't power down the widget if it controls eapd and EAPD_BTLENABLE is set */
Takashi Iwaiba615b82013-03-13 14:47:21 +01003775unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
3776 hda_nid_t nid,
3777 unsigned int power_state)
Takashi Iwai9419ab62013-01-24 17:23:35 +01003778{
3779 if (power_state == AC_PWRST_D3 &&
3780 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
3781 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3782 int eapd = snd_hda_codec_read(codec, nid, 0,
3783 AC_VERB_GET_EAPD_BTLENABLE, 0);
3784 if (eapd & 0x02)
3785 return AC_PWRST_D0;
3786 }
3787 return power_state;
3788}
Takashi Iwaiba615b82013-03-13 14:47:21 +01003789EXPORT_SYMBOL_HDA(snd_hda_codec_eapd_power_filter);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003790
Takashi Iwai432c6412012-08-28 09:59:20 -07003791/*
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003792 * set power state of the codec, and return the power state
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003793 */
Takashi Iwaid8193872012-08-31 07:54:38 -07003794static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003795 unsigned int power_state)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003796{
Takashi Iwaid8193872012-08-31 07:54:38 -07003797 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003798 int count;
3799 unsigned int state;
3800
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003801 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003802 if (power_state == AC_PWRST_D3) {
3803 /* transition time less than 10ms for power down */
Takashi Iwai983f6b92012-08-28 09:18:01 -07003804 msleep(codec->epss ? 10 : 100);
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003805 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08003806
3807 /* repeat power states setting at most 10 times*/
3808 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07003809 if (codec->patch_ops.set_power_state)
3810 codec->patch_ops.set_power_state(codec, fg,
3811 power_state);
3812 else {
3813 snd_hda_codec_read(codec, fg, 0,
3814 AC_VERB_SET_POWER_STATE,
3815 power_state);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003816 snd_hda_codec_set_power_to_all(codec, fg, power_state);
Takashi Iwai432c6412012-08-28 09:59:20 -07003817 }
3818 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08003819 if (!(state & AC_PWRST_ERROR))
3820 break;
3821 }
Mengdong Linb8dfc4622012-08-23 17:32:30 +08003822
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003823 return state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003824}
Takashi Iwai54d17402005-11-21 16:33:22 +01003825
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003826/* sync power states of all widgets;
3827 * this is called at the end of codec parsing
3828 */
3829static void sync_power_up_states(struct hda_codec *codec)
3830{
3831 hda_nid_t nid = codec->start_nid;
3832 int i;
3833
Takashi Iwaiba615b82013-03-13 14:47:21 +01003834 /* don't care if no filter is used */
3835 if (!codec->power_filter)
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003836 return;
3837
3838 for (i = 0; i < codec->num_nodes; i++, nid++) {
3839 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9040d102013-01-24 17:47:17 +01003840 unsigned int target;
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003841 if (!(wcaps & AC_WCAP_POWER))
3842 continue;
3843 target = codec->power_filter(codec, nid, AC_PWRST_D0);
3844 if (target == AC_PWRST_D0)
3845 continue;
Takashi Iwai9040d102013-01-24 17:47:17 +01003846 if (!snd_hda_check_power_state(codec, nid, target))
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003847 snd_hda_codec_write(codec, nid, 0,
3848 AC_VERB_SET_POWER_STATE, target);
3849 }
3850}
3851
Takashi Iwai11aeff02008-07-30 15:01:46 +02003852#ifdef CONFIG_SND_HDA_HWDEP
3853/* execute additional init verbs */
3854static void hda_exec_init_verbs(struct hda_codec *codec)
3855{
3856 if (codec->init_verbs.list)
3857 snd_hda_sequence_write(codec, codec->init_verbs.list);
3858}
3859#else
3860static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3861#endif
3862
Takashi Iwai2a439522011-07-26 09:52:50 +02003863#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003864/*
3865 * call suspend and power-down; used both from PM and power-save
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003866 * this function returns the power state in the end
Takashi Iwaicb53c622007-08-10 17:21:45 +02003867 */
Dylan Reidd17344b2012-09-28 15:57:01 -07003868static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003869{
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003870 unsigned int state;
3871
Takashi Iwai989c3182012-11-19 14:14:58 +01003872 codec->in_pm = 1;
3873
Takashi Iwaicb53c622007-08-10 17:21:45 +02003874 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02003875 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02003876 hda_cleanup_all_streams(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003877 state = hda_set_power_state(codec, AC_PWRST_D3);
Dylan Reidd17344b2012-09-28 15:57:01 -07003878 /* Cancel delayed work if we aren't currently running from it. */
3879 if (!in_wq)
3880 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003881 spin_lock(&codec->power_lock);
3882 snd_hda_update_power_acct(codec);
3883 trace_hda_power_down(codec);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003884 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003885 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003886 codec->power_jiffies = jiffies;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003887 spin_unlock(&codec->power_lock);
Takashi Iwai989c3182012-11-19 14:14:58 +01003888 codec->in_pm = 0;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003889 return state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003890}
3891
Takashi Iwaic370dd62012-12-13 18:30:04 +01003892/* mark all entries of cmd and amp caches dirty */
3893static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
3894{
3895 int i;
3896 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3897 struct hda_cache_head *cmd;
3898 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
3899 cmd->dirty = 1;
3900 }
3901 for (i = 0; i < codec->amp_cache.buf.used; i++) {
3902 struct hda_amp_info *amp;
David Henningssonf038fca2013-01-15 15:27:19 +01003903 amp = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003904 amp->head.dirty = 1;
3905 }
3906}
3907
Takashi Iwaicb53c622007-08-10 17:21:45 +02003908/*
3909 * kick up codec; used both from PM and power-save
3910 */
3911static void hda_call_codec_resume(struct hda_codec *codec)
3912{
Takashi Iwai989c3182012-11-19 14:14:58 +01003913 codec->in_pm = 1;
3914
Takashi Iwaic370dd62012-12-13 18:30:04 +01003915 hda_mark_cmd_cache_dirty(codec);
3916
Takashi Iwai7f308302012-05-08 16:52:23 +02003917 /* set as if powered on for avoiding re-entering the resume
3918 * in the resume / power-save sequence
3919 */
3920 hda_keep_power_on(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003921 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003922 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003923 hda_exec_init_verbs(codec);
Takashi Iwai31614bb2013-01-23 15:58:40 +01003924 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003925 if (codec->patch_ops.resume)
3926 codec->patch_ops.resume(codec);
3927 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003928 if (codec->patch_ops.init)
3929 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003930 snd_hda_codec_resume_amp(codec);
3931 snd_hda_codec_resume_cache(codec);
3932 }
David Henningsson26a6cb62012-10-09 15:04:21 +02003933
3934 if (codec->jackpoll_interval)
3935 hda_jackpoll_work(&codec->jackpoll_work.work);
Takashi Iwai31614bb2013-01-23 15:58:40 +01003936 else
David Henningsson26a6cb62012-10-09 15:04:21 +02003937 snd_hda_jack_report_sync(codec);
Takashi Iwai989c3182012-11-19 14:14:58 +01003938
3939 codec->in_pm = 0;
Takashi Iwai7f308302012-05-08 16:52:23 +02003940 snd_hda_power_down(codec); /* flag down before returning */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003941}
Takashi Iwai2a439522011-07-26 09:52:50 +02003942#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003943
Takashi Iwai54d17402005-11-21 16:33:22 +01003944
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945/**
3946 * snd_hda_build_controls - build mixer controls
3947 * @bus: the BUS
3948 *
3949 * Creates mixer controls for each codec included in the bus.
3950 *
3951 * Returns 0 if successful, otherwise a negative error code.
3952 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01003953int snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003955 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956
Takashi Iwai0ba21762007-04-16 11:29:14 +02003957 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003958 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003959 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01003960 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003961 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003962 err = snd_hda_codec_reset(codec);
3963 if (err < 0) {
3964 printk(KERN_ERR
3965 "hda_codec: cannot revert codec\n");
3966 return err;
3967 }
3968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003970 return 0;
3971}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003972EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003974/*
3975 * add standard channel maps if not specified
3976 */
3977static int add_std_chmaps(struct hda_codec *codec)
3978{
3979 int i, str, err;
3980
3981 for (i = 0; i < codec->num_pcms; i++) {
3982 for (str = 0; str < 2; str++) {
3983 struct snd_pcm *pcm = codec->pcm_info[i].pcm;
3984 struct hda_pcm_stream *hinfo =
3985 &codec->pcm_info[i].stream[str];
3986 struct snd_pcm_chmap *chmap;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003987 const struct snd_pcm_chmap_elem *elem;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003988
3989 if (codec->pcm_info[i].own_chmap)
3990 continue;
3991 if (!pcm || !hinfo->substreams)
3992 continue;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003993 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
3994 err = snd_pcm_add_chmap_ctls(pcm, str, elem,
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003995 hinfo->channels_max,
3996 0, &chmap);
3997 if (err < 0)
3998 return err;
3999 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
4000 }
4001 }
4002 return 0;
4003}
4004
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004005/* default channel maps for 2.1 speakers;
4006 * since HD-audio supports only stereo, odd number channels are omitted
4007 */
4008const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
4009 { .channels = 2,
4010 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
4011 { .channels = 4,
4012 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
4013 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
4014 { }
4015};
4016EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
4017
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004018int snd_hda_codec_build_controls(struct hda_codec *codec)
4019{
4020 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02004021 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004022 /* continue to initialize... */
4023 if (codec->patch_ops.init)
4024 err = codec->patch_ops.init(codec);
4025 if (!err && codec->patch_ops.build_controls)
4026 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004027 if (err < 0)
4028 return err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004029
4030 /* we create chmaps here instead of build_pcms */
4031 err = add_std_chmaps(codec);
4032 if (err < 0)
4033 return err;
4034
David Henningsson26a6cb62012-10-09 15:04:21 +02004035 if (codec->jackpoll_interval)
4036 hda_jackpoll_work(&codec->jackpoll_work.work);
4037 else
4038 snd_hda_jack_report_sync(codec); /* call at the last init point */
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004039 sync_power_up_states(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 return 0;
4041}
4042
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043/*
4044 * stream formats
4045 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02004046struct hda_rate_tbl {
4047 unsigned int hz;
4048 unsigned int alsa_bits;
4049 unsigned int hda_fmt;
4050};
4051
Takashi Iwai92f10b32010-08-03 14:21:00 +02004052/* rate = base * mult / div */
4053#define HDA_RATE(base, mult, div) \
4054 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
4055 (((div) - 1) << AC_FMT_DIV_SHIFT))
4056
Takashi Iwaibefdf312005-08-22 13:57:55 +02004057static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004059
4060 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004061 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
4062 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
4063 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
4064 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
4065 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
4066 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
4067 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
4068 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
4069 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
4070 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
4071 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004072#define AC_PAR_PCM_RATE_BITS 11
4073 /* up to bits 10, 384kHZ isn't supported properly */
4074
4075 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004076 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004077
Takashi Iwaibefdf312005-08-22 13:57:55 +02004078 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079};
4080
4081/**
4082 * snd_hda_calc_stream_format - calculate format bitset
4083 * @rate: the sample rate
4084 * @channels: the number of channels
4085 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
4086 * @maxbps: the max. bps
4087 *
4088 * Calculate the format bitset from the given rate, channels and th PCM format.
4089 *
4090 * Return zero if invalid.
4091 */
4092unsigned int snd_hda_calc_stream_format(unsigned int rate,
4093 unsigned int channels,
4094 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03004095 unsigned int maxbps,
4096 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097{
4098 int i;
4099 unsigned int val = 0;
4100
Takashi Iwaibefdf312005-08-22 13:57:55 +02004101 for (i = 0; rate_bits[i].hz; i++)
4102 if (rate_bits[i].hz == rate) {
4103 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 break;
4105 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02004106 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 snd_printdd("invalid rate %d\n", rate);
4108 return 0;
4109 }
4110
4111 if (channels == 0 || channels > 8) {
4112 snd_printdd("invalid channels %d\n", channels);
4113 return 0;
4114 }
4115 val |= channels - 1;
4116
4117 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004118 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004119 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004120 break;
4121 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004122 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004123 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 case 20:
4125 case 24:
4126 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004127 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004128 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004130 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02004132 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 break;
4134 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004135 snd_printdd("invalid format width %d\n",
4136 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 return 0;
4138 }
4139
Anssi Hannula32c168c2010-08-03 13:28:57 +03004140 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004141 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03004142
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 return val;
4144}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004145EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004147static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
4148 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004149{
4150 unsigned int val = 0;
4151 if (nid != codec->afg &&
4152 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
4153 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
4154 if (!val || val == -1)
4155 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
4156 if (!val || val == -1)
4157 return 0;
4158 return val;
4159}
4160
4161static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
4162{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004163 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004164 get_pcm_param);
4165}
4166
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004167static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
4168 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004169{
4170 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
4171 if (!streams || streams == -1)
4172 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
4173 if (!streams || streams == -1)
4174 return 0;
4175 return streams;
4176}
4177
4178static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
4179{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004180 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004181 get_stream_param);
4182}
4183
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184/**
4185 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4186 * @codec: the HDA codec
4187 * @nid: NID to query
4188 * @ratesp: the pointer to store the detected rate bitflags
4189 * @formatsp: the pointer to store the detected formats
4190 * @bpsp: the pointer to store the detected format widths
4191 *
4192 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4193 * or @bsps argument is ignored.
4194 *
4195 * Returns 0 if successful, otherwise a negative error code.
4196 */
Stephen Warren384a48d2011-06-01 11:14:21 -06004197int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
4199{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004200 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004202 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004203 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204
4205 if (ratesp) {
4206 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004207 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02004209 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004211 if (rates == 0) {
4212 snd_printk(KERN_ERR "hda_codec: rates == 0 "
4213 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
4214 nid, val,
4215 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
4216 return -EIO;
4217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 *ratesp = rates;
4219 }
4220
4221 if (formatsp || bpsp) {
4222 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004223 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004225 streams = query_stream_param(codec, nid);
4226 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228
4229 bps = 0;
4230 if (streams & AC_SUPFMT_PCM) {
4231 if (val & AC_SUPPCM_BITS_8) {
4232 formats |= SNDRV_PCM_FMTBIT_U8;
4233 bps = 8;
4234 }
4235 if (val & AC_SUPPCM_BITS_16) {
4236 formats |= SNDRV_PCM_FMTBIT_S16_LE;
4237 bps = 16;
4238 }
4239 if (wcaps & AC_WCAP_DIGITAL) {
4240 if (val & AC_SUPPCM_BITS_32)
4241 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4242 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4243 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4244 if (val & AC_SUPPCM_BITS_24)
4245 bps = 24;
4246 else if (val & AC_SUPPCM_BITS_20)
4247 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004248 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4249 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4251 if (val & AC_SUPPCM_BITS_32)
4252 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 else if (val & AC_SUPPCM_BITS_24)
4254 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02004255 else if (val & AC_SUPPCM_BITS_20)
4256 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257 }
4258 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004259#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02004260 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004262 if (!bps)
4263 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02004264 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004265#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02004266 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02004267 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 /* temporary hack: we have still no proper support
4269 * for the direct AC3 stream...
4270 */
4271 formats |= SNDRV_PCM_FMTBIT_U8;
4272 bps = 8;
4273 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004274 if (formats == 0) {
4275 snd_printk(KERN_ERR "hda_codec: formats == 0 "
4276 "(nid=0x%x, val=0x%x, ovrd=%i, "
4277 "streams=0x%x)\n",
4278 nid, val,
4279 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4280 streams);
4281 return -EIO;
4282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 if (formatsp)
4284 *formatsp = formats;
4285 if (bpsp)
4286 *bpsp = bps;
4287 }
4288
4289 return 0;
4290}
Stephen Warren384a48d2011-06-01 11:14:21 -06004291EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292
4293/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004294 * snd_hda_is_supported_format - Check the validity of the format
4295 * @codec: HD-audio codec
4296 * @nid: NID to check
4297 * @format: the HD-audio format value to check
4298 *
4299 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 *
4301 * Returns 1 if supported, 0 if not.
4302 */
4303int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4304 unsigned int format)
4305{
4306 int i;
4307 unsigned int val = 0, rate, stream;
4308
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004309 val = query_pcm_param(codec, nid);
4310 if (!val)
4311 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312
4313 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004314 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02004315 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 if (val & (1 << i))
4317 break;
4318 return 0;
4319 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004320 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 return 0;
4322
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004323 stream = query_stream_param(codec, nid);
4324 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 return 0;
4326
4327 if (stream & AC_SUPFMT_PCM) {
4328 switch (format & 0xf0) {
4329 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004330 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 return 0;
4332 break;
4333 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004334 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 return 0;
4336 break;
4337 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004338 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 return 0;
4340 break;
4341 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004342 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 return 0;
4344 break;
4345 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004346 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 return 0;
4348 break;
4349 default:
4350 return 0;
4351 }
4352 } else {
4353 /* FIXME: check for float32 and AC3? */
4354 }
4355
4356 return 1;
4357}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004358EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359
4360/*
4361 * PCM stuff
4362 */
4363static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4364 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004365 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366{
4367 return 0;
4368}
4369
4370static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4371 struct hda_codec *codec,
4372 unsigned int stream_tag,
4373 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004374 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375{
4376 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4377 return 0;
4378}
4379
4380static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4381 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004382 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383{
Takashi Iwai888afa12008-03-18 09:57:50 +01004384 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 return 0;
4386}
4387
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004388static int set_pcm_default_values(struct hda_codec *codec,
4389 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004391 int err;
4392
Takashi Iwai0ba21762007-04-16 11:29:14 +02004393 /* query support PCM information from the given NID */
4394 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004395 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02004396 info->rates ? NULL : &info->rates,
4397 info->formats ? NULL : &info->formats,
4398 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004399 if (err < 0)
4400 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 }
4402 if (info->ops.open == NULL)
4403 info->ops.open = hda_pcm_default_open_close;
4404 if (info->ops.close == NULL)
4405 info->ops.close = hda_pcm_default_open_close;
4406 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004407 if (snd_BUG_ON(!info->nid))
4408 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 info->ops.prepare = hda_pcm_default_prepare;
4410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004412 if (snd_BUG_ON(!info->nid))
4413 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 info->ops.cleanup = hda_pcm_default_cleanup;
4415 }
4416 return 0;
4417}
4418
Takashi Iwaieb541332010-08-06 13:48:11 +02004419/*
4420 * codec prepare/cleanup entries
4421 */
4422int snd_hda_codec_prepare(struct hda_codec *codec,
4423 struct hda_pcm_stream *hinfo,
4424 unsigned int stream,
4425 unsigned int format,
4426 struct snd_pcm_substream *substream)
4427{
4428 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004429 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004430 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4431 if (ret >= 0)
4432 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004433 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004434 return ret;
4435}
4436EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
4437
4438void snd_hda_codec_cleanup(struct hda_codec *codec,
4439 struct hda_pcm_stream *hinfo,
4440 struct snd_pcm_substream *substream)
4441{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004442 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004443 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004444 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004445}
4446EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
4447
Takashi Iwaid5191e52009-11-16 14:58:17 +01004448/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01004449const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4450 "Audio", "SPDIF", "HDMI", "Modem"
4451};
4452
Takashi Iwai176d5332008-07-30 15:01:44 +02004453/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004454 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01004455 *
4456 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004457 */
4458static int get_empty_pcm_device(struct hda_bus *bus, int type)
4459{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004460 /* audio device indices; not linear to keep compatibility */
4461 static int audio_idx[HDA_PCM_NTYPES][5] = {
4462 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4463 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01004464 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004465 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004466 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004467 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004468
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004469 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004470 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
4471 return -EINVAL;
4472 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004473
4474 for (i = 0; audio_idx[type][i] >= 0 ; i++)
4475 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4476 return audio_idx[type][i];
4477
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004478 /* non-fixed slots starting from 10 */
4479 for (i = 10; i < 32; i++) {
4480 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4481 return i;
4482 }
4483
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004484 snd_printk(KERN_WARNING "Too many %s devices\n",
4485 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004486 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004487}
4488
4489/*
Takashi Iwai176d5332008-07-30 15:01:44 +02004490 * attach a new PCM stream
4491 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004492static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02004493{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004494 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02004495 struct hda_pcm_stream *info;
4496 int stream, err;
4497
Takashi Iwaib91f0802008-11-04 08:43:08 +01004498 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02004499 return -EINVAL;
4500 for (stream = 0; stream < 2; stream++) {
4501 info = &pcm->stream[stream];
4502 if (info->substreams) {
4503 err = set_pcm_default_values(codec, info);
4504 if (err < 0)
4505 return err;
4506 }
4507 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004508 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02004509}
4510
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004511/* assign all PCMs of the given codec */
4512int snd_hda_codec_build_pcms(struct hda_codec *codec)
4513{
4514 unsigned int pcm;
4515 int err;
4516
4517 if (!codec->num_pcms) {
4518 if (!codec->patch_ops.build_pcms)
4519 return 0;
4520 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004521 if (err < 0) {
4522 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004523 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004524 err = snd_hda_codec_reset(codec);
4525 if (err < 0) {
4526 printk(KERN_ERR
4527 "hda_codec: cannot revert codec\n");
4528 return err;
4529 }
4530 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004531 }
4532 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4533 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4534 int dev;
4535
4536 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004537 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004538
4539 if (!cpcm->pcm) {
4540 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4541 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004542 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004543 cpcm->device = dev;
4544 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004545 if (err < 0) {
4546 printk(KERN_ERR "hda_codec: cannot attach "
4547 "PCM stream %d for codec #%d\n",
4548 dev, codec->addr);
4549 continue; /* no fatal error */
4550 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004551 }
4552 }
4553 return 0;
4554}
4555
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556/**
4557 * snd_hda_build_pcms - build PCM information
4558 * @bus: the BUS
4559 *
4560 * Create PCM information for each codec included in the bus.
4561 *
4562 * The build_pcms codec patch is requested to set up codec->num_pcms and
4563 * codec->pcm_info properly. The array is referred by the top-level driver
4564 * to create its PCM instances.
4565 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4566 * callback.
4567 *
4568 * At least, substreams, channels_min and channels_max must be filled for
4569 * each stream. substreams = 0 indicates that the stream doesn't exist.
4570 * When rates and/or formats are zero, the supported values are queried
4571 * from the given nid. The nid is used also by the default ops.prepare
4572 * and ops.cleanup callbacks.
4573 *
4574 * The driver needs to call ops.open in its open callback. Similarly,
4575 * ops.close is supposed to be called in the close callback.
4576 * ops.prepare should be called in the prepare or hw_params callback
4577 * with the proper parameters for set up.
4578 * ops.cleanup should be called in hw_free for clean up of streams.
4579 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004580 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 */
Takashi Iwai5cb543d2012-08-09 13:49:23 +02004582int snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004584 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585
Takashi Iwai0ba21762007-04-16 11:29:14 +02004586 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004587 int err = snd_hda_codec_build_pcms(codec);
4588 if (err < 0)
4589 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 }
4591 return 0;
4592}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004593EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595/**
4596 * snd_hda_check_board_config - compare the current codec with the config table
4597 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004598 * @num_configs: number of config enums
4599 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600 * @tbl: configuration table, terminated by null entries
4601 *
4602 * Compares the modelname or PCI subsystem id of the current codec with the
4603 * given configuration table. If a matching entry is found, returns its
4604 * config value (supposed to be 0 or positive).
4605 *
4606 * If no entries are matching, the function returns a negative value.
4607 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004608int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004609 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02004610 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611{
Takashi Iwaif44ac832008-07-30 15:01:45 +02004612 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004613 int i;
4614 for (i = 0; i < num_configs; i++) {
4615 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02004616 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004617 snd_printd(KERN_INFO "hda_codec: model '%s' is "
4618 "selected\n", models[i]);
4619 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620 }
4621 }
4622 }
4623
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004624 if (!codec->bus->pci || !tbl)
4625 return -1;
4626
4627 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
4628 if (!tbl)
4629 return -1;
4630 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02004631#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004632 char tmp[10];
4633 const char *model = NULL;
4634 if (models)
4635 model = models[tbl->value];
4636 if (!model) {
4637 sprintf(tmp, "#%d", tbl->value);
4638 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004640 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4641 "for config %x:%x (%s)\n",
4642 model, tbl->subvendor, tbl->subdevice,
4643 (tbl->name ? tbl->name : "Unknown device"));
4644#endif
4645 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 }
4647 return -1;
4648}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004649EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650
4651/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004652 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004653 subsystem ID with the
4654 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004655
4656 This is important for Gateway notebooks with SB450 HDA Audio
4657 where the vendor ID of the PCI device is:
4658 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4659 and the vendor/subvendor are found only at the codec.
4660
4661 * @codec: the HDA codec
4662 * @num_configs: number of config enums
4663 * @models: array of model name strings
4664 * @tbl: configuration table, terminated by null entries
4665 *
4666 * Compares the modelname or PCI subsystem id of the current codec with the
4667 * given configuration table. If a matching entry is found, returns its
4668 * config value (supposed to be 0 or positive).
4669 *
4670 * If no entries are matching, the function returns a negative value.
4671 */
4672int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004673 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004674 const struct snd_pci_quirk *tbl)
4675{
4676 const struct snd_pci_quirk *q;
4677
4678 /* Search for codec ID */
4679 for (q = tbl; q->subvendor; q++) {
Takashi Iwaie2301a42011-11-22 19:58:56 +01004680 unsigned int mask = 0xffff0000 | q->subdevice_mask;
4681 unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
4682 if ((codec->subsystem_id & mask) == id)
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004683 break;
4684 }
4685
4686 if (!q->subvendor)
4687 return -1;
4688
4689 tbl = q;
4690
4691 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02004692#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004693 char tmp[10];
4694 const char *model = NULL;
4695 if (models)
4696 model = models[tbl->value];
4697 if (!model) {
4698 sprintf(tmp, "#%d", tbl->value);
4699 model = tmp;
4700 }
4701 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4702 "for config %x:%x (%s)\n",
4703 model, tbl->subvendor, tbl->subdevice,
4704 (tbl->name ? tbl->name : "Unknown device"));
4705#endif
4706 return tbl->value;
4707 }
4708 return -1;
4709}
4710EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4711
4712/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713 * snd_hda_add_new_ctls - create controls from the array
4714 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004715 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716 *
4717 * This helper function creates and add new controls in the given array.
4718 * The array must be terminated with an empty entry as terminator.
4719 *
4720 * Returns 0 if successful, or a negative error code.
4721 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004722int snd_hda_add_new_ctls(struct hda_codec *codec,
4723 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004725 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726
4727 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004728 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004729 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004730 if (knew->iface == -1) /* skip this codec private value */
4731 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004732 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004733 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004734 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004735 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004736 if (addr > 0)
4737 kctl->id.device = addr;
4738 if (idx > 0)
4739 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004740 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004741 if (!err)
4742 break;
4743 /* try first with another device index corresponding to
4744 * the codec addr; if it still fails (or it's the
4745 * primary codec), then try another control index
4746 */
4747 if (!addr && codec->addr)
4748 addr = codec->addr;
4749 else if (!idx && !knew->index) {
4750 idx = find_empty_mixer_ctl_idx(codec,
Takashi Iwaidcda5802012-10-12 17:24:51 +02004751 knew->name, 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004752 if (idx <= 0)
4753 return err;
4754 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004755 return err;
4756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757 }
4758 return 0;
4759}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004760EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761
Takashi Iwai83012a72012-08-24 18:38:08 +02004762#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02004763static void hda_power_work(struct work_struct *work)
4764{
4765 struct hda_codec *codec =
4766 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004767 struct hda_bus *bus = codec->bus;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004768 unsigned int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004769
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004770 spin_lock(&codec->power_lock);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004771 if (codec->power_transition > 0) { /* during power-up sequence? */
4772 spin_unlock(&codec->power_lock);
4773 return;
4774 }
Maxim Levitsky2e492462007-09-03 15:26:57 +02004775 if (!codec->power_on || codec->power_count) {
4776 codec->power_transition = 0;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004777 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004778 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02004779 }
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004780 spin_unlock(&codec->power_lock);
4781
Dylan Reidd17344b2012-09-28 15:57:01 -07004782 state = hda_call_codec_suspend(codec, true);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004783 codec->pm_down_notified = 0;
4784 if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK)) {
4785 codec->pm_down_notified = 1;
Takashi Iwai68467f52012-08-28 09:14:29 -07004786 hda_call_pm_notify(bus, false);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004787 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004788}
4789
4790static void hda_keep_power_on(struct hda_codec *codec)
4791{
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004792 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004793 codec->power_count++;
4794 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004795 codec->power_jiffies = jiffies;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004796 spin_unlock(&codec->power_lock);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004797}
4798
Takashi Iwaid5191e52009-11-16 14:58:17 +01004799/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01004800void snd_hda_update_power_acct(struct hda_codec *codec)
4801{
4802 unsigned long delta = jiffies - codec->power_jiffies;
4803 if (codec->power_on)
4804 codec->power_on_acct += delta;
4805 else
4806 codec->power_off_acct += delta;
4807 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004808}
4809
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004810/* Transition to powered up, if wait_power_down then wait for a pending
4811 * transition to D3 to complete. A pending D3 transition is indicated
4812 * with power_transition == -1. */
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004813/* call this with codec->power_lock held! */
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004814static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004815{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004816 struct hda_bus *bus = codec->bus;
4817
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004818 /* Return if power_on or transitioning to power_on, unless currently
4819 * powering down. */
4820 if ((codec->power_on || codec->power_transition > 0) &&
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004821 !(wait_power_down && codec->power_transition < 0))
Takashi Iwaicb53c622007-08-10 17:21:45 +02004822 return;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004823 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004824
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004825 cancel_delayed_work_sync(&codec->power_work);
4826
4827 spin_lock(&codec->power_lock);
Dylan Reidb43d2242012-06-21 21:51:22 -07004828 /* If the power down delayed work was cancelled above before starting,
4829 * then there is no need to go through power up here.
4830 */
4831 if (codec->power_on) {
Takashi Iwai535b6c52012-08-20 21:25:22 +02004832 if (codec->power_transition < 0)
4833 codec->power_transition = 0;
Dylan Reidb43d2242012-06-21 21:51:22 -07004834 return;
4835 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004836
Takashi Iwaid66fee52011-08-02 15:39:31 +02004837 trace_hda_power_up(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004838 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004839 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004840 codec->power_jiffies = jiffies;
Takashi Iwai7f308302012-05-08 16:52:23 +02004841 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004842 spin_unlock(&codec->power_lock);
4843
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004844 if (codec->pm_down_notified) {
4845 codec->pm_down_notified = 0;
Takashi Iwai68467f52012-08-28 09:14:29 -07004846 hda_call_pm_notify(bus, true);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004847 }
4848
Takashi Iwaicb53c622007-08-10 17:21:45 +02004849 hda_call_codec_resume(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004850
4851 spin_lock(&codec->power_lock);
Takashi Iwaia221e282007-08-16 16:35:33 +02004852 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004853}
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004854
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004855#define power_save(codec) \
4856 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004857
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004858/* Transition to powered down */
4859static void __snd_hda_power_down(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004860{
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004861 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004862 return;
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004863
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004864 if (power_save(codec)) {
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004865 codec->power_transition = -1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01004866 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004867 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02004868 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004869}
4870
4871/**
4872 * snd_hda_power_save - Power-up/down/sync the codec
4873 * @codec: HD-audio codec
4874 * @delta: the counter delta to change
4875 *
4876 * Change the power-up counter via @delta, and power up or down the hardware
4877 * appropriately. For the power-down, queue to the delayed action.
4878 * Passing zero to @delta means to synchronize the power state.
4879 */
4880void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
4881{
4882 spin_lock(&codec->power_lock);
4883 codec->power_count += delta;
4884 trace_hda_power_count(codec);
4885 if (delta > 0)
4886 __snd_hda_power_up(codec, d3wait);
4887 else
4888 __snd_hda_power_down(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004889 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004890}
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004891EXPORT_SYMBOL_HDA(snd_hda_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004892
Takashi Iwaid5191e52009-11-16 14:58:17 +01004893/**
4894 * snd_hda_check_amp_list_power - Check the amp list and update the power
4895 * @codec: HD-audio codec
4896 * @check: the object containing an AMP list and the status
4897 * @nid: NID to check / update
4898 *
4899 * Check whether the given NID is in the amp list. If it's in the list,
4900 * check the current AMP status, and update the the power-status according
4901 * to the mute status.
4902 *
4903 * This function is supposed to be set or called from the check_power_status
4904 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004905 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004906int snd_hda_check_amp_list_power(struct hda_codec *codec,
4907 struct hda_loopback_check *check,
4908 hda_nid_t nid)
4909{
Takashi Iwai031024e2011-05-02 11:29:30 +02004910 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004911 int ch, v;
4912
4913 if (!check->amplist)
4914 return 0;
4915 for (p = check->amplist; p->nid; p++) {
4916 if (p->nid == nid)
4917 break;
4918 }
4919 if (!p->nid)
4920 return 0; /* nothing changed */
4921
4922 for (p = check->amplist; p->nid; p++) {
4923 for (ch = 0; ch < 2; ch++) {
4924 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4925 p->idx);
4926 if (!(v & HDA_AMP_MUTE) && v > 0) {
4927 if (!check->power_on) {
4928 check->power_on = 1;
4929 snd_hda_power_up(codec);
4930 }
4931 return 1;
4932 }
4933 }
4934 }
4935 if (check->power_on) {
4936 check->power_on = 0;
4937 snd_hda_power_down(codec);
4938 }
4939 return 0;
4940}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004941EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004942#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004944/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004945 * Channel mode helper
4946 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004947
4948/**
4949 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4950 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004951int snd_hda_ch_mode_info(struct hda_codec *codec,
4952 struct snd_ctl_elem_info *uinfo,
4953 const struct hda_channel_mode *chmode,
4954 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004955{
4956 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4957 uinfo->count = 1;
4958 uinfo->value.enumerated.items = num_chmodes;
4959 if (uinfo->value.enumerated.item >= num_chmodes)
4960 uinfo->value.enumerated.item = num_chmodes - 1;
4961 sprintf(uinfo->value.enumerated.name, "%dch",
4962 chmode[uinfo->value.enumerated.item].channels);
4963 return 0;
4964}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004965EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004966
Takashi Iwaid5191e52009-11-16 14:58:17 +01004967/**
4968 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4969 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004970int snd_hda_ch_mode_get(struct hda_codec *codec,
4971 struct snd_ctl_elem_value *ucontrol,
4972 const struct hda_channel_mode *chmode,
4973 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004974 int max_channels)
4975{
4976 int i;
4977
4978 for (i = 0; i < num_chmodes; i++) {
4979 if (max_channels == chmode[i].channels) {
4980 ucontrol->value.enumerated.item[0] = i;
4981 break;
4982 }
4983 }
4984 return 0;
4985}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004986EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004987
Takashi Iwaid5191e52009-11-16 14:58:17 +01004988/**
4989 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4990 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004991int snd_hda_ch_mode_put(struct hda_codec *codec,
4992 struct snd_ctl_elem_value *ucontrol,
4993 const struct hda_channel_mode *chmode,
4994 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004995 int *max_channelsp)
4996{
4997 unsigned int mode;
4998
4999 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01005000 if (mode >= num_chmodes)
5001 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005002 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005003 return 0;
5004 /* change the current channel setting */
5005 *max_channelsp = chmode[mode].channels;
5006 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005007 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005008 return 1;
5009}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005010EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005011
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012/*
5013 * input MUX helper
5014 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01005015
5016/**
5017 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
5018 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005019int snd_hda_input_mux_info(const struct hda_input_mux *imux,
5020 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021{
5022 unsigned int index;
5023
5024 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5025 uinfo->count = 1;
5026 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02005027 if (!imux->num_items)
5028 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029 index = uinfo->value.enumerated.item;
5030 if (index >= imux->num_items)
5031 index = imux->num_items - 1;
5032 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
5033 return 0;
5034}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005035EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036
Takashi Iwaid5191e52009-11-16 14:58:17 +01005037/**
5038 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
5039 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005040int snd_hda_input_mux_put(struct hda_codec *codec,
5041 const struct hda_input_mux *imux,
5042 struct snd_ctl_elem_value *ucontrol,
5043 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044 unsigned int *cur_val)
5045{
5046 unsigned int idx;
5047
Takashi Iwai5513b0c2007-10-09 11:58:41 +02005048 if (!imux->num_items)
5049 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050 idx = ucontrol->value.enumerated.item[0];
5051 if (idx >= imux->num_items)
5052 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005053 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005055 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
5056 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057 *cur_val = idx;
5058 return 1;
5059}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005060EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061
5062
5063/*
Takashi Iwaidda415d2012-11-30 18:34:38 +01005064 * process kcontrol info callback of a simple string enum array
5065 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
5066 */
5067int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
5068 struct snd_ctl_elem_info *uinfo,
5069 int num_items, const char * const *texts)
5070{
5071 static const char * const texts_default[] = {
5072 "Disabled", "Enabled"
5073 };
5074
5075 if (!texts || !num_items) {
5076 num_items = 2;
5077 texts = texts_default;
5078 }
5079
5080 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5081 uinfo->count = 1;
5082 uinfo->value.enumerated.items = num_items;
5083 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
5084 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
5085 strcpy(uinfo->value.enumerated.name,
5086 texts[uinfo->value.enumerated.item]);
5087 return 0;
5088}
5089EXPORT_SYMBOL_HDA(snd_hda_enum_helper_info);
5090
5091/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092 * Multi-channel / digital-out PCM helper functions
5093 */
5094
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005095/* setup SPDIF output stream */
5096static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
5097 unsigned int stream_tag, unsigned int format)
5098{
Laurence Darby3bef1c32012-11-03 17:00:06 +00005099 struct hda_spdif_out *spdif;
5100 unsigned int curr_fmt;
5101 bool reset;
Stephen Warren7c935972011-06-01 11:14:17 -06005102
Laurence Darby3bef1c32012-11-03 17:00:06 +00005103 spdif = snd_hda_spdif_out_of_nid(codec, nid);
5104 curr_fmt = snd_hda_codec_read(codec, nid, 0,
5105 AC_VERB_GET_STREAM_FORMAT, 0);
5106 reset = codec->spdif_status_reset &&
5107 (spdif->ctls & AC_DIG1_ENABLE) &&
5108 curr_fmt != format;
5109
5110 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
5111 updated */
5112 if (reset)
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005113 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06005114 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02005115 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005116 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02005117 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02005118 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02005119 for (d = codec->slave_dig_outs; *d; d++)
5120 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
5121 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005122 }
Takashi Iwai2f728532008-09-25 16:32:41 +02005123 /* turn on again (if needed) */
Laurence Darby3bef1c32012-11-03 17:00:06 +00005124 if (reset)
Takashi Iwai2f728532008-09-25 16:32:41 +02005125 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06005126 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02005127}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005128
Takashi Iwai2f728532008-09-25 16:32:41 +02005129static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
5130{
5131 snd_hda_codec_cleanup_stream(codec, nid);
5132 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02005133 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02005134 for (d = codec->slave_dig_outs; *d; d++)
5135 snd_hda_codec_cleanup_stream(codec, *d);
5136 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005137}
5138
Takashi Iwaid5191e52009-11-16 14:58:17 +01005139/**
5140 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
5141 * @bus: HD-audio bus
5142 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005143void snd_hda_bus_reboot_notify(struct hda_bus *bus)
5144{
5145 struct hda_codec *codec;
5146
5147 if (!bus)
5148 return;
5149 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005150 if (hda_codec_is_power_on(codec) &&
5151 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005152 codec->patch_ops.reboot_notify(codec);
5153 }
5154}
Takashi Iwai8f217a22009-11-10 18:26:12 +01005155EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005156
Takashi Iwaid5191e52009-11-16 14:58:17 +01005157/**
5158 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005160int snd_hda_multi_out_dig_open(struct hda_codec *codec,
5161 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162{
Ingo Molnar62932df2006-01-16 16:34:20 +01005163 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02005164 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
5165 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02005166 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01005168 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169 return 0;
5170}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005171EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172
Takashi Iwaid5191e52009-11-16 14:58:17 +01005173/**
5174 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
5175 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005176int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
5177 struct hda_multi_out *mout,
5178 unsigned int stream_tag,
5179 unsigned int format,
5180 struct snd_pcm_substream *substream)
5181{
5182 mutex_lock(&codec->spdif_mutex);
5183 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
5184 mutex_unlock(&codec->spdif_mutex);
5185 return 0;
5186}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005187EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005188
Takashi Iwaid5191e52009-11-16 14:58:17 +01005189/**
5190 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
5191 */
Takashi Iwai9411e212009-02-13 11:32:28 +01005192int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
5193 struct hda_multi_out *mout)
5194{
5195 mutex_lock(&codec->spdif_mutex);
5196 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5197 mutex_unlock(&codec->spdif_mutex);
5198 return 0;
5199}
5200EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
5201
Takashi Iwaid5191e52009-11-16 14:58:17 +01005202/**
5203 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005205int snd_hda_multi_out_dig_close(struct hda_codec *codec,
5206 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207{
Ingo Molnar62932df2006-01-16 16:34:20 +01005208 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01005210 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211 return 0;
5212}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005213EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214
Takashi Iwaid5191e52009-11-16 14:58:17 +01005215/**
5216 * snd_hda_multi_out_analog_open - open analog outputs
5217 *
5218 * Open analog outputs and set up the hw-constraints.
5219 * If the digital outputs can be opened as slave, open the digital
5220 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005222int snd_hda_multi_out_analog_open(struct hda_codec *codec,
5223 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01005224 struct snd_pcm_substream *substream,
5225 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226{
Takashi Iwai9a081602008-02-12 18:37:26 +01005227 struct snd_pcm_runtime *runtime = substream->runtime;
5228 runtime->hw.channels_max = mout->max_channels;
5229 if (mout->dig_out_nid) {
5230 if (!mout->analog_rates) {
5231 mout->analog_rates = hinfo->rates;
5232 mout->analog_formats = hinfo->formats;
5233 mout->analog_maxbps = hinfo->maxbps;
5234 } else {
5235 runtime->hw.rates = mout->analog_rates;
5236 runtime->hw.formats = mout->analog_formats;
5237 hinfo->maxbps = mout->analog_maxbps;
5238 }
5239 if (!mout->spdif_rates) {
5240 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
5241 &mout->spdif_rates,
5242 &mout->spdif_formats,
5243 &mout->spdif_maxbps);
5244 }
5245 mutex_lock(&codec->spdif_mutex);
5246 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02005247 if ((runtime->hw.rates & mout->spdif_rates) &&
5248 (runtime->hw.formats & mout->spdif_formats)) {
5249 runtime->hw.rates &= mout->spdif_rates;
5250 runtime->hw.formats &= mout->spdif_formats;
5251 if (mout->spdif_maxbps < hinfo->maxbps)
5252 hinfo->maxbps = mout->spdif_maxbps;
5253 } else {
5254 mout->share_spdif = 0;
5255 /* FIXME: need notify? */
5256 }
Takashi Iwai9a081602008-02-12 18:37:26 +01005257 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02005258 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01005259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260 return snd_pcm_hw_constraint_step(substream->runtime, 0,
5261 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
5262}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005263EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005264
Takashi Iwaid5191e52009-11-16 14:58:17 +01005265/**
5266 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
5267 *
5268 * Set up the i/o for analog out.
5269 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005271int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5272 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273 unsigned int stream_tag,
5274 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01005275 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276{
Takashi Iwaidda14412011-05-02 11:29:30 +02005277 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005278 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005279 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280 int i;
5281
Ingo Molnar62932df2006-01-16 16:34:20 +01005282 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005283 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01005284 if (mout->dig_out_nid && mout->share_spdif &&
5285 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02005287 snd_hda_is_supported_format(codec, mout->dig_out_nid,
5288 format) &&
Stephen Warren7c935972011-06-01 11:14:17 -06005289 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005291 setup_dig_out_stream(codec, mout->dig_out_nid,
5292 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293 } else {
5294 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02005295 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296 }
5297 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005298 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299
5300 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005301 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5302 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005303 if (!mout->no_share_stream &&
5304 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005306 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5307 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005308 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005309 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5310 if (!mout->no_share_stream && mout->hp_out_nid[i])
5311 snd_hda_codec_setup_stream(codec,
5312 mout->hp_out_nid[i],
5313 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005314 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02005315 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01005316 snd_hda_codec_setup_stream(codec,
5317 mout->extra_out_nid[i],
5318 stream_tag, 0, format);
5319
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320 /* surrounds */
5321 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02005322 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005323 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5324 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005325 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005326 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5327 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328 }
5329 return 0;
5330}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005331EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332
Takashi Iwaid5191e52009-11-16 14:58:17 +01005333/**
5334 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005336int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5337 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338{
Takashi Iwaidda14412011-05-02 11:29:30 +02005339 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340 int i;
5341
5342 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01005343 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01005345 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005346 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5347 if (mout->hp_out_nid[i])
5348 snd_hda_codec_cleanup_stream(codec,
5349 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005350 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5351 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01005352 snd_hda_codec_cleanup_stream(codec,
5353 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01005354 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02005356 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357 mout->dig_out_used = 0;
5358 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005359 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360 return 0;
5361}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005362EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363
Takashi Iwai47408602012-04-20 13:06:53 +02005364/**
5365 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
5366 *
5367 * Guess the suitable VREF pin bits to be set as the pin-control value.
5368 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5369 */
5370unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5371{
5372 unsigned int pincap;
5373 unsigned int oldval;
5374 oldval = snd_hda_codec_read(codec, pin, 0,
5375 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5376 pincap = snd_hda_query_pin_caps(codec, pin);
5377 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5378 /* Exception: if the default pin setup is vref50, we give it priority */
5379 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5380 return AC_PINCTL_VREF_80;
5381 else if (pincap & AC_PINCAP_VREF_50)
5382 return AC_PINCTL_VREF_50;
5383 else if (pincap & AC_PINCAP_VREF_100)
5384 return AC_PINCTL_VREF_100;
5385 else if (pincap & AC_PINCAP_VREF_GRD)
5386 return AC_PINCTL_VREF_GRD;
5387 return AC_PINCTL_VREF_HIZ;
5388}
5389EXPORT_SYMBOL_HDA(snd_hda_get_default_vref);
5390
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005391/* correct the pin ctl value for matching with the pin cap */
5392unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
5393 hda_nid_t pin, unsigned int val)
5394{
5395 static unsigned int cap_lists[][2] = {
5396 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
5397 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
5398 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
5399 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
5400 };
5401 unsigned int cap;
5402
5403 if (!val)
5404 return 0;
5405 cap = snd_hda_query_pin_caps(codec, pin);
5406 if (!cap)
5407 return val; /* don't know what to do... */
5408
5409 if (val & AC_PINCTL_OUT_EN) {
5410 if (!(cap & AC_PINCAP_OUT))
5411 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5412 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
5413 val &= ~AC_PINCTL_HP_EN;
5414 }
5415
5416 if (val & AC_PINCTL_IN_EN) {
5417 if (!(cap & AC_PINCAP_IN))
5418 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5419 else {
5420 unsigned int vcap, vref;
5421 int i;
5422 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5423 vref = val & AC_PINCTL_VREFEN;
5424 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
5425 if (vref == cap_lists[i][0] &&
5426 !(vcap & cap_lists[i][1])) {
5427 if (i == ARRAY_SIZE(cap_lists) - 1)
5428 vref = AC_PINCTL_VREF_HIZ;
5429 else
5430 vref = cap_lists[i + 1][0];
5431 }
5432 }
5433 val &= ~AC_PINCTL_VREFEN;
5434 val |= vref;
5435 }
5436 }
5437
5438 return val;
5439}
5440EXPORT_SYMBOL_HDA(snd_hda_correct_pin_ctl);
5441
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005442int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5443 unsigned int val, bool cached)
5444{
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005445 val = snd_hda_correct_pin_ctl(codec, pin, val);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01005446 snd_hda_codec_set_pin_target(codec, pin, val);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005447 if (cached)
5448 return snd_hda_codec_update_cache(codec, pin, 0,
5449 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5450 else
5451 return snd_hda_codec_write(codec, pin, 0,
5452 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5453}
5454EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl);
5455
Takashi Iwai990061c2010-09-09 22:08:44 +02005456/**
5457 * snd_hda_add_imux_item - Add an item to input_mux
5458 *
5459 * When the same label is used already in the existing items, the number
5460 * suffix is appended to the label. This label index number is stored
5461 * to type_idx when non-NULL pointer is given.
5462 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005463int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5464 int index, int *type_idx)
5465{
5466 int i, label_idx = 0;
5467 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5468 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5469 return -EINVAL;
5470 }
5471 for (i = 0; i < imux->num_items; i++) {
5472 if (!strncmp(label, imux->items[i].label, strlen(label)))
5473 label_idx++;
5474 }
5475 if (type_idx)
5476 *type_idx = label_idx;
5477 if (label_idx > 0)
5478 snprintf(imux->items[imux->num_items].label,
5479 sizeof(imux->items[imux->num_items].label),
5480 "%s %d", label, label_idx);
5481 else
5482 strlcpy(imux->items[imux->num_items].label, label,
5483 sizeof(imux->items[imux->num_items].label));
5484 imux->items[imux->num_items].index = index;
5485 imux->num_items++;
5486 return 0;
5487}
5488EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005489
Takashi Iwai4a471b72005-12-07 13:56:29 +01005490
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491#ifdef CONFIG_PM
5492/*
5493 * power management
5494 */
5495
5496/**
5497 * snd_hda_suspend - suspend the codecs
5498 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499 *
5500 * Returns 0 if successful.
5501 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005502int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005504 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505
Takashi Iwai0ba21762007-04-16 11:29:14 +02005506 list_for_each_entry(codec, &bus->codec_list, list) {
David Henningsson26a6cb62012-10-09 15:04:21 +02005507 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005508 if (hda_codec_is_power_on(codec))
Dylan Reidd17344b2012-09-28 15:57:01 -07005509 hda_call_codec_suspend(codec, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510 }
5511 return 0;
5512}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005513EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514
5515/**
5516 * snd_hda_resume - resume the codecs
5517 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518 *
5519 * Returns 0 if successful.
5520 */
5521int snd_hda_resume(struct hda_bus *bus)
5522{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005523 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524
Takashi Iwai0ba21762007-04-16 11:29:14 +02005525 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai7f308302012-05-08 16:52:23 +02005526 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528 return 0;
5529}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005530EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005531#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005532
5533/*
5534 * generic arrays
5535 */
5536
Takashi Iwaid5191e52009-11-16 14:58:17 +01005537/**
5538 * snd_array_new - get a new element from the given array
5539 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005540 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005541 * Get a new element from the given array. If it exceeds the
5542 * pre-allocated array size, re-allocate the array.
5543 *
5544 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005545 */
5546void *snd_array_new(struct snd_array *array)
5547{
Takashi Iwai12f17712012-10-10 08:59:14 +02005548 if (snd_BUG_ON(!array->elem_size))
5549 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005550 if (array->used >= array->alloced) {
5551 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005552 int size = (num + 1) * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005553 void *nlist;
5554 if (snd_BUG_ON(num >= 4096))
5555 return NULL;
Takashi Iwai5265fd92013-03-13 12:15:28 +01005556 nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005557 if (!nlist)
5558 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005559 array->list = nlist;
5560 array->alloced = num;
5561 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005562 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005563}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005564EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005565
Takashi Iwaid5191e52009-11-16 14:58:17 +01005566/**
5567 * snd_array_free - free the given array elements
5568 * @array: the array object
5569 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005570void snd_array_free(struct snd_array *array)
5571{
5572 kfree(array->list);
5573 array->used = 0;
5574 array->alloced = 0;
5575 array->list = NULL;
5576}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005577EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005578
Takashi Iwaid5191e52009-11-16 14:58:17 +01005579/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005580 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5581 * @pcm: PCM caps bits
5582 * @buf: the string buffer to write
5583 * @buflen: the max buffer length
5584 *
5585 * used by hda_proc.c and hda_eld.c
5586 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005587void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5588{
5589 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5590 int i, j;
5591
5592 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5593 if (pcm & (AC_SUPPCM_BITS_8 << i))
5594 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5595
5596 buf[j] = '\0'; /* necessary when j == 0 */
5597}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005598EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005599
5600MODULE_DESCRIPTION("HDA codec core");
5601MODULE_LICENSE("GPL");