blob: 68261a778ee545a0f52c9a4c29e975d1cd6dacd7 [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);
Dan Carpentera26f3d72014-11-27 01:34:43 +0300330 if (parm == -1) {
331 *start_id = 0;
Danny Tholene8a7f132007-09-11 21:41:56 +0200332 return 0;
Dan Carpentera26f3d72014-11-27 01:34:43 +0300333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 *start_id = (parm >> 16) & 0x7fff;
335 return (int)(parm & 0x7fff);
336}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100337EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100339/* connection list element */
340struct hda_conn_list {
341 struct list_head list;
342 int len;
343 hda_nid_t nid;
344 hda_nid_t conns[0];
345};
346
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200347/* look up the cached results */
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100348static struct hda_conn_list *
349lookup_conn_list(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200350{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100351 struct hda_conn_list *p;
352 list_for_each_entry(p, &codec->conn_list, list) {
353 if (p->nid == nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200354 return p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200355 }
356 return NULL;
357}
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200358
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100359static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
360 const hda_nid_t *list)
361{
362 struct hda_conn_list *p;
363
364 p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL);
365 if (!p)
366 return -ENOMEM;
367 p->len = len;
368 p->nid = nid;
369 memcpy(p->conns, list, len * sizeof(hda_nid_t));
370 list_add(&p->list, &codec->conn_list);
371 return 0;
372}
373
374static void remove_conn_list(struct hda_codec *codec)
375{
376 while (!list_empty(&codec->conn_list)) {
377 struct hda_conn_list *p;
378 p = list_first_entry(&codec->conn_list, typeof(*p), list);
379 list_del(&p->list);
380 kfree(p);
381 }
382}
383
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200384/* read the connection and add to the cache */
385static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200386{
Takashi Iwai4eea3092013-02-07 18:18:19 +0100387 hda_nid_t list[32];
388 hda_nid_t *result = list;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200389 int len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200390
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200391 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
Takashi Iwai4eea3092013-02-07 18:18:19 +0100392 if (len == -ENOSPC) {
393 len = snd_hda_get_num_raw_conns(codec, nid);
394 result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL);
395 if (!result)
396 return -ENOMEM;
397 len = snd_hda_get_raw_connections(codec, nid, result, len);
398 }
399 if (len >= 0)
400 len = snd_hda_override_conn_list(codec, nid, len, result);
401 if (result != list)
402 kfree(result);
403 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200404}
Takashi Iwaidce20792011-06-24 14:10:28 +0200405
406/**
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100407 * snd_hda_get_conn_list - get connection list
408 * @codec: the HDA codec
409 * @nid: NID to parse
410 * @len: number of connection list entries
411 * @listp: the pointer to store NID list
412 *
413 * Parses the connection list of the given widget and stores the pointer
414 * to the list of NIDs.
415 *
416 * Returns the number of connections, or a negative error code.
417 *
418 * Note that the returned pointer isn't protected against the list
419 * modification. If snd_hda_override_conn_list() might be called
420 * concurrently, protect with a mutex appropriately.
421 */
422int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
423 const hda_nid_t **listp)
424{
425 bool added = false;
426
427 for (;;) {
428 int err;
429 const struct hda_conn_list *p;
430
431 /* if the connection-list is already cached, read it */
432 p = lookup_conn_list(codec, nid);
433 if (p) {
434 if (listp)
435 *listp = p->conns;
436 return p->len;
437 }
438 if (snd_BUG_ON(added))
439 return -EINVAL;
440
441 err = read_and_add_raw_conns(codec, nid);
442 if (err < 0)
443 return err;
444 added = true;
445 }
446}
447EXPORT_SYMBOL_HDA(snd_hda_get_conn_list);
448
449/**
Takashi Iwaidce20792011-06-24 14:10:28 +0200450 * snd_hda_get_connections - copy connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 * @codec: the HDA codec
452 * @nid: NID to parse
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200453 * @conn_list: connection list array; when NULL, checks only the size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 * @max_conns: max. number of connections to store
455 *
456 * Parses the connection list of the given widget and stores the list
457 * of NIDs.
458 *
459 * Returns the number of connections, or a negative error code.
460 */
461int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200462 hda_nid_t *conn_list, int max_conns)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200463{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100464 const hda_nid_t *list;
465 int len = snd_hda_get_conn_list(codec, nid, &list);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200466
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100467 if (len > 0 && conn_list) {
468 if (len > max_conns) {
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200469 snd_printk(KERN_ERR "hda_codec: "
470 "Too many connections %d for NID 0x%x\n",
471 len, nid);
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200472 return -EINVAL;
473 }
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100474 memcpy(conn_list, list, len * sizeof(hda_nid_t));
Takashi Iwaidce20792011-06-24 14:10:28 +0200475 }
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200476
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100477 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200478}
479EXPORT_SYMBOL_HDA(snd_hda_get_connections);
480
Takashi Iwai4eea3092013-02-07 18:18:19 +0100481/* return CONNLIST_LEN parameter of the given widget */
482static unsigned int get_num_conns(struct hda_codec *codec, hda_nid_t nid)
483{
484 unsigned int wcaps = get_wcaps(codec, nid);
485 unsigned int parm;
486
487 if (!(wcaps & AC_WCAP_CONN_LIST) &&
488 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
489 return 0;
490
491 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
492 if (parm == -1)
493 parm = 0;
494 return parm;
495}
496
497int snd_hda_get_num_raw_conns(struct hda_codec *codec, hda_nid_t nid)
498{
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100499 return snd_hda_get_raw_connections(codec, nid, NULL, 0);
Takashi Iwai4eea3092013-02-07 18:18:19 +0100500}
501
Takashi Iwai9e7717c2011-07-11 15:42:52 +0200502/**
503 * snd_hda_get_raw_connections - copy connection list without cache
504 * @codec: the HDA codec
505 * @nid: NID to parse
506 * @conn_list: connection list array
507 * @max_conns: max. number of connections to store
508 *
509 * Like snd_hda_get_connections(), copy the connection list but without
510 * checking through the connection-list cache.
511 * Currently called only from hda_proc.c, so not exported.
512 */
513int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
514 hda_nid_t *conn_list, int max_conns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
516 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100517 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 unsigned int shift, num_elems, mask;
Takashi Iwai54d17402005-11-21 16:33:22 +0100519 hda_nid_t prev_nid;
Takashi Iwai5fdaecd2012-12-20 10:45:55 +0100520 int null_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Takashi Iwai4eea3092013-02-07 18:18:19 +0100522 parm = get_num_conns(codec, nid);
523 if (!parm)
Takashi Iwai8d087c72011-06-28 12:45:47 +0200524 return 0;
Jaroslav Kysela16a433d2009-07-22 16:20:40 +0200525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 if (parm & AC_CLIST_LONG) {
527 /* long form */
528 shift = 16;
529 num_elems = 2;
530 } else {
531 /* short form */
532 shift = 8;
533 num_elems = 4;
534 }
535 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 mask = (1 << (shift-1)) - 1;
537
Takashi Iwai0ba21762007-04-16 11:29:14 +0200538 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 return 0; /* no connection */
540
541 if (conn_len == 1) {
542 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200543 parm = snd_hda_codec_read(codec, nid, 0,
544 AC_VERB_GET_CONNECT_LIST, 0);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200545 if (parm == -1 && codec->bus->rirb_error)
546 return -EIO;
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100547 if (conn_list)
548 conn_list[0] = parm & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 return 1;
550 }
551
552 /* multi connection */
553 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100554 prev_nid = 0;
555 for (i = 0; i < conn_len; i++) {
556 int range_val;
557 hda_nid_t val, n;
558
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200559 if (i % num_elems == 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100560 parm = snd_hda_codec_read(codec, nid, 0,
561 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200562 if (parm == -1 && codec->bus->rirb_error)
563 return -EIO;
564 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200565 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100566 val = parm & mask;
Takashi Iwai5fdaecd2012-12-20 10:45:55 +0100567 if (val == 0 && null_count++) { /* no second chance */
Jaroslav Kysela2e9bf242009-07-18 11:48:19 +0200568 snd_printk(KERN_WARNING "hda_codec: "
569 "invalid CONNECT_LIST verb %x[%i]:%x\n",
570 nid, i, parm);
571 return 0;
572 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100573 parm >>= shift;
574 if (range_val) {
575 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200576 if (!prev_nid || prev_nid >= val) {
577 snd_printk(KERN_WARNING "hda_codec: "
578 "invalid dep_range_val %x:%x\n",
579 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100580 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100582 for (n = prev_nid + 1; n <= val; n++) {
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100583 if (conn_list) {
584 if (conns >= max_conns)
585 return -ENOSPC;
586 conn_list[conns] = n;
587 }
588 conns++;
Takashi Iwai54d17402005-11-21 16:33:22 +0100589 }
590 } else {
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100591 if (conn_list) {
592 if (conns >= max_conns)
593 return -ENOSPC;
594 conn_list[conns] = val;
595 }
596 conns++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100598 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 }
600 return conns;
601}
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200604 * snd_hda_override_conn_list - add/modify the connection-list to cache
605 * @codec: the HDA codec
606 * @nid: NID to parse
607 * @len: number of connection list entries
608 * @list: the list of connection entries
609 *
610 * Add or modify the given connection-list to the cache. If the corresponding
611 * cache already exists, invalidate it and append a new one.
612 *
613 * Returns zero or a negative error code.
614 */
615int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
616 const hda_nid_t *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100618 struct hda_conn_list *p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200619
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100620 p = lookup_conn_list(codec, nid);
621 if (p) {
622 list_del(&p->list);
623 kfree(p);
624 }
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200625
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100626 return add_conn_list(codec, nid, len, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627}
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200628EXPORT_SYMBOL_HDA(snd_hda_override_conn_list);
629
630/**
Takashi Iwai8d087c72011-06-28 12:45:47 +0200631 * snd_hda_get_conn_index - get the connection index of the given NID
632 * @codec: the HDA codec
633 * @mux: NID containing the list
634 * @nid: NID to select
635 * @recursive: 1 when searching NID recursively, otherwise 0
636 *
637 * Parses the connection list of the widget @mux and checks whether the
638 * widget @nid is present. If it is, return the connection index.
639 * Otherwise it returns -1.
640 */
641int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
642 hda_nid_t nid, int recursive)
643{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100644 const hda_nid_t *conn;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200645 int i, nums;
646
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100647 nums = snd_hda_get_conn_list(codec, mux, &conn);
Takashi Iwai8d087c72011-06-28 12:45:47 +0200648 for (i = 0; i < nums; i++)
649 if (conn[i] == nid)
650 return i;
651 if (!recursive)
652 return -1;
Takashi Iwaid94ddd82012-12-20 14:42:42 +0100653 if (recursive > 10) {
Takashi Iwai8d087c72011-06-28 12:45:47 +0200654 snd_printd("hda_codec: too deep connection for 0x%x\n", nid);
655 return -1;
656 }
657 recursive++;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200658 for (i = 0; i < nums; i++) {
659 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
660 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
661 continue;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200662 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
663 return i;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200664 }
Takashi Iwai8d087c72011-06-28 12:45:47 +0200665 return -1;
666}
667EXPORT_SYMBOL_HDA(snd_hda_get_conn_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669/**
670 * snd_hda_queue_unsol_event - add an unsolicited event to queue
671 * @bus: the BUS
672 * @res: unsolicited event (lower 32bit of RIRB entry)
673 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
674 *
675 * Adds the given event to the queue. The events are processed in
676 * the workqueue asynchronously. Call this function in the interrupt
677 * hanlder when RIRB receives an unsolicited event.
678 *
679 * Returns 0 if successful, or a negative error code.
680 */
681int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
682{
683 struct hda_bus_unsolicited *unsol;
684 unsigned int wp;
685
Wang YanQing2195b062013-05-07 11:27:33 +0800686 if (!bus || !bus->workq)
687 return 0;
688
Takashi Iwaiecf726f2011-08-09 14:22:44 +0200689 trace_hda_unsol_event(bus, res, res_ex);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200690 unsol = bus->unsol;
691 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 return 0;
693
694 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
695 unsol->wp = wp;
696
697 wp <<= 1;
698 unsol->queue[wp] = res;
699 unsol->queue[wp + 1] = res_ex;
700
Takashi Iwai6acaed32009-01-12 10:09:24 +0100701 queue_work(bus->workq, &unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 return 0;
704}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100705EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800708 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 */
David Howellsc4028952006-11-22 14:57:56 +0000710static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
David Howellsc4028952006-11-22 14:57:56 +0000712 struct hda_bus_unsolicited *unsol =
713 container_of(work, struct hda_bus_unsolicited, work);
714 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 struct hda_codec *codec;
716 unsigned int rp, caddr, res;
717
718 while (unsol->rp != unsol->wp) {
719 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
720 unsol->rp = rp;
721 rp <<= 1;
722 res = unsol->queue[rp];
723 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200724 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 continue;
726 codec = bus->caddr_tbl[caddr & 0x0f];
727 if (codec && codec->patch_ops.unsol_event)
728 codec->patch_ops.unsol_event(codec, res);
729 }
730}
731
732/*
733 * initialize unsolicited queue
734 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200735static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
737 struct hda_bus_unsolicited *unsol;
738
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100739 if (bus->unsol) /* already initialized */
740 return 0;
741
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200742 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200743 if (!unsol) {
744 snd_printk(KERN_ERR "hda_codec: "
745 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 return -ENOMEM;
747 }
David Howellsc4028952006-11-22 14:57:56 +0000748 INIT_WORK(&unsol->work, process_unsol_events);
749 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 bus->unsol = unsol;
751 return 0;
752}
753
754/*
755 * destructor
756 */
757static void snd_hda_codec_free(struct hda_codec *codec);
758
759static int snd_hda_bus_free(struct hda_bus *bus)
760{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200761 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Takashi Iwai0ba21762007-04-16 11:29:14 +0200763 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 return 0;
Takashi Iwai6acaed32009-01-12 10:09:24 +0100765 if (bus->workq)
766 flush_workqueue(bus->workq);
767 if (bus->unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 kfree(bus->unsol);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200769 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 snd_hda_codec_free(codec);
771 }
772 if (bus->ops.private_free)
773 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100774 if (bus->workq)
775 destroy_workqueue(bus->workq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 kfree(bus);
777 return 0;
778}
779
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100780static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
782 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100783 bus->shutdown = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 return snd_hda_bus_free(bus);
785}
786
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200787#ifdef CONFIG_SND_HDA_HWDEP
788static int snd_hda_bus_dev_register(struct snd_device *device)
789{
790 struct hda_bus *bus = device->device_data;
791 struct hda_codec *codec;
792 list_for_each_entry(codec, &bus->codec_list, list) {
793 snd_hda_hwdep_add_sysfs(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +0100794 snd_hda_hwdep_add_power_sysfs(codec);
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200795 }
796 return 0;
797}
798#else
799#define snd_hda_bus_dev_register NULL
800#endif
801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802/**
803 * snd_hda_bus_new - create a HDA bus
804 * @card: the card entry
805 * @temp: the template for hda_bus information
806 * @busp: the pointer to store the created bus instance
807 *
808 * Returns 0 if successful, or a negative error code.
809 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +0100810int snd_hda_bus_new(struct snd_card *card,
Takashi Iwai756e2b02007-04-16 11:27:07 +0200811 const struct hda_bus_template *temp,
812 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
814 struct hda_bus *bus;
815 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100816 static struct snd_device_ops dev_ops = {
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200817 .dev_register = snd_hda_bus_dev_register,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 .dev_free = snd_hda_bus_dev_free,
819 };
820
Takashi Iwaida3cec32008-08-08 17:12:14 +0200821 if (snd_BUG_ON(!temp))
822 return -EINVAL;
823 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
824 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826 if (busp)
827 *busp = NULL;
828
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200829 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if (bus == NULL) {
831 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
832 return -ENOMEM;
833 }
834
835 bus->card = card;
836 bus->private_data = temp->private_data;
837 bus->pci = temp->pci;
838 bus->modelname = temp->modelname;
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100839 bus->power_save = temp->power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 bus->ops = temp->ops;
841
Ingo Molnar62932df2006-01-16 16:34:20 +0100842 mutex_init(&bus->cmd_mutex);
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200843 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 INIT_LIST_HEAD(&bus->codec_list);
845
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100846 snprintf(bus->workq_name, sizeof(bus->workq_name),
847 "hd-audio%d", card->number);
848 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100849 if (!bus->workq) {
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100850 snd_printk(KERN_ERR "cannot create workqueue %s\n",
851 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100852 kfree(bus);
853 return -ENOMEM;
854 }
855
Takashi Iwai0ba21762007-04-16 11:29:14 +0200856 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
857 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 snd_hda_bus_free(bus);
859 return err;
860 }
861 if (busp)
862 *busp = bus;
863 return 0;
864}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100865EXPORT_SYMBOL_HDA(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Takashi Iwai82467612007-07-27 19:15:54 +0200867#ifdef CONFIG_SND_HDA_GENERIC
868#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200869 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200870#else
871#define is_generic_config(codec) 0
872#endif
873
Takashi Iwai645f10c2008-11-28 15:07:37 +0100874#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100875#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
876#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100877#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100878#endif
879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880/*
881 * find a matching codec preset
882 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200883static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200884find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100886 struct hda_codec_preset_list *tbl;
887 const struct hda_codec_preset *preset;
Takashi Iwai5d908ab2012-08-24 18:40:10 +0200888 unsigned int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Takashi Iwai82467612007-07-27 19:15:54 +0200890 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100891 return NULL; /* use the generic parser */
892
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100893 again:
894 mutex_lock(&preset_mutex);
895 list_for_each_entry(tbl, &hda_preset_tables, list) {
896 if (!try_module_get(tbl->owner)) {
897 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
898 continue;
899 }
900 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100902 if (preset->afg && preset->afg != codec->afg)
903 continue;
904 if (preset->mfg && preset->mfg != codec->mfg)
905 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200906 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200908 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200909 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100910 preset->rev == codec->revision_id)) {
911 mutex_unlock(&preset_mutex);
912 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100916 module_put(tbl->owner);
917 }
918 mutex_unlock(&preset_mutex);
919
920 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
921 char name[32];
922 if (!mod_requested)
923 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
924 codec->vendor_id);
925 else
926 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
927 (codec->vendor_id >> 16) & 0xffff);
928 request_module(name);
929 mod_requested++;
930 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 }
932 return NULL;
933}
934
935/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200936 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200938static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939{
940 const struct hda_vendor_id *c;
941 const char *vendor = NULL;
942 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200943 char tmp[16];
944
945 if (codec->vendor_name)
946 goto get_chip_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
948 for (c = hda_vendor_ids; c->id; c++) {
949 if (c->id == vendor_id) {
950 vendor = c->name;
951 break;
952 }
953 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200954 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 sprintf(tmp, "Generic %04x", vendor_id);
956 vendor = tmp;
957 }
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200958 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
959 if (!codec->vendor_name)
960 return -ENOMEM;
961
962 get_chip_name:
963 if (codec->chip_name)
964 return 0;
965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 if (codec->preset && codec->preset->name)
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200967 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
968 else {
969 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
970 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
971 }
972 if (!codec->chip_name)
Takashi Iwaif44ac832008-07-30 15:01:45 +0200973 return -ENOMEM;
974 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975}
976
977/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200978 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +0100980static void setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981{
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200982 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 hda_nid_t nid;
984
985 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
986 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200987 function_id = snd_hda_param_read(codec, nid,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200988 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +0200989 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200990 case AC_GRP_AUDIO_FUNCTION:
991 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200992 codec->afg_function_id = function_id & 0xff;
993 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200994 break;
995 case AC_GRP_MODEM_FUNCTION:
996 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200997 codec->mfg_function_id = function_id & 0xff;
998 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200999 break;
1000 default:
1001 break;
1002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004}
1005
1006/*
Takashi Iwai54d17402005-11-21 16:33:22 +01001007 * read widget caps for each widget and store in cache
1008 */
1009static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
1010{
1011 int i;
1012 hda_nid_t nid;
1013
1014 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
1015 &codec->start_nid);
1016 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001017 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +01001018 return -ENOMEM;
1019 nid = codec->start_nid;
1020 for (i = 0; i < codec->num_nodes; i++, nid++)
1021 codec->wcaps[i] = snd_hda_param_read(codec, nid,
1022 AC_PAR_AUDIO_WIDGET_CAP);
1023 return 0;
1024}
1025
Takashi Iwai3be14142009-02-20 14:11:16 +01001026/* read all pin default configurations and save codec->init_pins */
1027static int read_pin_defaults(struct hda_codec *codec)
1028{
1029 int i;
1030 hda_nid_t nid = codec->start_nid;
1031
1032 for (i = 0; i < codec->num_nodes; i++, nid++) {
1033 struct hda_pincfg *pin;
1034 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +02001035 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +01001036 if (wid_type != AC_WID_PIN)
1037 continue;
1038 pin = snd_array_new(&codec->init_pins);
1039 if (!pin)
1040 return -ENOMEM;
1041 pin->nid = nid;
1042 pin->cfg = snd_hda_codec_read(codec, nid, 0,
1043 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001044 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
1045 AC_VERB_GET_PIN_WIDGET_CONTROL,
1046 0);
Takashi Iwai3be14142009-02-20 14:11:16 +01001047 }
1048 return 0;
1049}
1050
1051/* look up the given pin config list and return the item matching with NID */
1052static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
1053 struct snd_array *array,
1054 hda_nid_t nid)
1055{
1056 int i;
1057 for (i = 0; i < array->used; i++) {
1058 struct hda_pincfg *pin = snd_array_elem(array, i);
1059 if (pin->nid == nid)
1060 return pin;
1061 }
1062 return NULL;
1063}
1064
Takashi Iwai3be14142009-02-20 14:11:16 +01001065/* set the current pin config value for the given NID.
1066 * the value is cached, and read via snd_hda_codec_get_pincfg()
1067 */
1068int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
1069 hda_nid_t nid, unsigned int cfg)
1070{
1071 struct hda_pincfg *pin;
1072
Takashi Iwaid5657ec2013-04-18 09:59:28 +02001073 /* the check below may be invalid when pins are added by a fixup
1074 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
1075 * for now
1076 */
1077 /*
Takashi Iwaib82855a2009-12-27 11:24:56 +01001078 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1079 return -EINVAL;
Takashi Iwaid5657ec2013-04-18 09:59:28 +02001080 */
Takashi Iwaib82855a2009-12-27 11:24:56 +01001081
Takashi Iwai3be14142009-02-20 14:11:16 +01001082 pin = look_up_pincfg(codec, list, nid);
1083 if (!pin) {
1084 pin = snd_array_new(list);
1085 if (!pin)
1086 return -ENOMEM;
1087 pin->nid = nid;
1088 }
1089 pin->cfg = cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001090 return 0;
1091}
1092
Takashi Iwaid5191e52009-11-16 14:58:17 +01001093/**
1094 * snd_hda_codec_set_pincfg - Override a pin default configuration
1095 * @codec: the HDA codec
1096 * @nid: NID to set the pin config
1097 * @cfg: the pin default config value
1098 *
1099 * Override a pin default configuration value in the cache.
1100 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1101 * priority than the real hardware value.
1102 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001103int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1104 hda_nid_t nid, unsigned int cfg)
1105{
Takashi Iwai346ff702009-02-23 09:42:57 +01001106 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001107}
1108EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
1109
Takashi Iwaid5191e52009-11-16 14:58:17 +01001110/**
1111 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1112 * @codec: the HDA codec
1113 * @nid: NID to get the pin config
1114 *
1115 * Get the current pin config value of the given pin NID.
1116 * If the pincfg value is cached or overridden via sysfs or driver,
1117 * returns the cached value.
1118 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001119unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1120{
1121 struct hda_pincfg *pin;
1122
Takashi Iwai3be14142009-02-20 14:11:16 +01001123#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai09b70e82013-01-10 18:21:56 +01001124 {
1125 unsigned int cfg = 0;
1126 mutex_lock(&codec->user_mutex);
1127 pin = look_up_pincfg(codec, &codec->user_pins, nid);
1128 if (pin)
1129 cfg = pin->cfg;
1130 mutex_unlock(&codec->user_mutex);
1131 if (cfg)
1132 return cfg;
1133 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001134#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001135 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1136 if (pin)
1137 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001138 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1139 if (pin)
1140 return pin->cfg;
1141 return 0;
1142}
1143EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
1144
Takashi Iwaid7fdc002013-01-10 08:38:04 +01001145/* remember the current pinctl target value */
1146int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
1147 unsigned int val)
1148{
1149 struct hda_pincfg *pin;
1150
1151 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1152 if (!pin)
1153 return -EINVAL;
1154 pin->target = val;
1155 return 0;
1156}
1157EXPORT_SYMBOL_HDA(snd_hda_codec_set_pin_target);
1158
1159/* return the current pinctl target value */
1160int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
1161{
1162 struct hda_pincfg *pin;
1163
1164 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1165 if (!pin)
1166 return 0;
1167 return pin->target;
1168}
1169EXPORT_SYMBOL_HDA(snd_hda_codec_get_pin_target);
1170
Takashi Iwai92ee6162009-12-27 11:18:59 +01001171/**
1172 * snd_hda_shutup_pins - Shut up all pins
1173 * @codec: the HDA codec
1174 *
1175 * Clear all pin controls to shup up before suspend for avoiding click noise.
1176 * The controls aren't cached so that they can be resumed properly.
1177 */
1178void snd_hda_shutup_pins(struct hda_codec *codec)
1179{
1180 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001181 /* don't shut up pins when unloading the driver; otherwise it breaks
1182 * the default pin setup at the next load of the driver
1183 */
1184 if (codec->bus->shutdown)
1185 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001186 for (i = 0; i < codec->init_pins.used; i++) {
1187 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1188 /* use read here for syncing after issuing each verb */
1189 snd_hda_codec_read(codec, pin->nid, 0,
1190 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1191 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001192 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001193}
1194EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
1195
Takashi Iwai2a439522011-07-26 09:52:50 +02001196#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001197/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1198static void restore_shutup_pins(struct hda_codec *codec)
1199{
1200 int i;
1201 if (!codec->pins_shutup)
1202 return;
1203 if (codec->bus->shutdown)
1204 return;
1205 for (i = 0; i < codec->init_pins.used; i++) {
1206 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1207 snd_hda_codec_write(codec, pin->nid, 0,
1208 AC_VERB_SET_PIN_WIDGET_CONTROL,
1209 pin->ctrl);
1210 }
1211 codec->pins_shutup = 0;
1212}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001213#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001214
David Henningsson26a6cb62012-10-09 15:04:21 +02001215static void hda_jackpoll_work(struct work_struct *work)
1216{
1217 struct hda_codec *codec =
1218 container_of(work, struct hda_codec, jackpoll_work.work);
1219 if (!codec->jackpoll_interval)
1220 return;
1221
1222 snd_hda_jack_set_dirty_all(codec);
1223 snd_hda_jack_poll_all(codec);
1224 queue_delayed_work(codec->bus->workq, &codec->jackpoll_work,
1225 codec->jackpoll_interval);
1226}
1227
Takashi Iwai01751f52007-08-10 16:59:39 +02001228static void init_hda_cache(struct hda_cache_rec *cache,
1229 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001230static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001231
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001232/* release all pincfg lists */
1233static void free_init_pincfgs(struct hda_codec *codec)
Takashi Iwai3be14142009-02-20 14:11:16 +01001234{
Takashi Iwai346ff702009-02-23 09:42:57 +01001235 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001236#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001237 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001238#endif
Takashi Iwai3be14142009-02-20 14:11:16 +01001239 snd_array_free(&codec->init_pins);
1240}
1241
Takashi Iwai54d17402005-11-21 16:33:22 +01001242/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001243 * audio-converter setup caches
1244 */
1245struct hda_cvt_setup {
1246 hda_nid_t nid;
1247 u8 stream_tag;
1248 u8 channel_id;
1249 u16 format_id;
1250 unsigned char active; /* cvt is currently used */
1251 unsigned char dirty; /* setups should be cleared */
1252};
1253
1254/* get or create a cache entry for the given audio converter NID */
1255static struct hda_cvt_setup *
1256get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1257{
1258 struct hda_cvt_setup *p;
1259 int i;
1260
1261 for (i = 0; i < codec->cvt_setups.used; i++) {
1262 p = snd_array_elem(&codec->cvt_setups, i);
1263 if (p->nid == nid)
1264 return p;
1265 }
1266 p = snd_array_new(&codec->cvt_setups);
1267 if (p)
1268 p->nid = nid;
1269 return p;
1270}
1271
1272/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 * codec destructor
1274 */
1275static void snd_hda_codec_free(struct hda_codec *codec)
1276{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001277 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 return;
David Henningsson26a6cb62012-10-09 15:04:21 +02001279 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai59cad162012-06-26 15:00:20 +02001280 snd_hda_jack_tbl_clear(codec);
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001281 free_init_pincfgs(codec);
Takashi Iwai83012a72012-08-24 18:38:08 +02001282#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02001283 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001284 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001285#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001287 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001288 snd_array_free(&codec->nids);
Takashi Iwai59cad162012-06-26 15:00:20 +02001289 snd_array_free(&codec->cvt_setups);
Stephen Warren7c935972011-06-01 11:14:17 -06001290 snd_array_free(&codec->spdif_out);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001291 remove_conn_list(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 codec->bus->caddr_tbl[codec->addr] = NULL;
1293 if (codec->patch_ops.free)
1294 codec->patch_ops.free(codec);
Takashi Iwai83012a72012-08-24 18:38:08 +02001295#ifdef CONFIG_PM
Takashi Iwai08fa20a2012-08-31 07:46:56 -07001296 if (!codec->pm_down_notified) /* cancel leftover refcounts */
Takashi Iwai68467f52012-08-28 09:14:29 -07001297 hda_call_pm_notify(codec->bus, false);
1298#endif
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001299 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +02001300 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001301 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001302 kfree(codec->vendor_name);
1303 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001304 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001305 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 kfree(codec);
1307}
1308
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001309static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec,
1310 hda_nid_t fg, unsigned int power_state);
1311
Takashi Iwaid8193872012-08-31 07:54:38 -07001312static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001313 unsigned int power_state);
1314
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315/**
1316 * snd_hda_codec_new - create a HDA codec
1317 * @bus: the bus to assign
1318 * @codec_addr: the codec address
1319 * @codecp: the pointer to store the generated codec
1320 *
1321 * Returns 0 if successful, or a negative error code.
1322 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001323int snd_hda_codec_new(struct hda_bus *bus,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001324 unsigned int codec_addr,
1325 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326{
1327 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001328 char component[31];
Takashi Iwaid8193872012-08-31 07:54:38 -07001329 hda_nid_t fg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 int err;
1331
Takashi Iwaida3cec32008-08-08 17:12:14 +02001332 if (snd_BUG_ON(!bus))
1333 return -EINVAL;
1334 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1335 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
1337 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001338 snd_printk(KERN_ERR "hda_codec: "
1339 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 return -EBUSY;
1341 }
1342
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001343 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 if (codec == NULL) {
1345 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1346 return -ENOMEM;
1347 }
1348
1349 codec->bus = bus;
1350 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001351 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001352 mutex_init(&codec->control_mutex);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001353 mutex_init(&codec->hash_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001354 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001355 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001356 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1357 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001358 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001359 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001360 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Stephen Warren7c935972011-06-01 11:14:17 -06001361 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai361dab32012-05-09 14:35:27 +02001362 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01001363 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001364 INIT_LIST_HEAD(&codec->conn_list);
1365
David Henningsson26a6cb62012-10-09 15:04:21 +02001366 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
Takashi Iwai83012a72012-08-24 18:38:08 +02001368#ifdef CONFIG_PM
Takashi Iwai5536c6d2012-05-08 17:08:10 +02001369 spin_lock_init(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001370 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1371 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1372 * the caller has to power down appropriatley after initialization
1373 * phase.
1374 */
1375 hda_keep_power_on(codec);
Takashi Iwai68467f52012-08-28 09:14:29 -07001376 hda_call_pm_notify(bus, true);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001377#endif
1378
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001379 if (codec->bus->modelname) {
1380 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1381 if (!codec->modelname) {
1382 snd_hda_codec_free(codec);
1383 return -ENODEV;
1384 }
1385 }
1386
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 list_add_tail(&codec->list, &bus->codec_list);
1388 bus->caddr_tbl[codec_addr] = codec;
1389
Takashi Iwai0ba21762007-04-16 11:29:14 +02001390 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1391 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001392 if (codec->vendor_id == -1)
1393 /* read again, hopefully the access method was corrected
1394 * in the last read...
1395 */
1396 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1397 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001398 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1399 AC_PAR_SUBSYSTEM_ID);
1400 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1401 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001403 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001404 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001405 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001406 err = -ENODEV;
1407 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 }
1409
Takashi Iwaid8193872012-08-31 07:54:38 -07001410 fg = codec->afg ? codec->afg : codec->mfg;
1411 err = read_widget_caps(codec, fg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001412 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001413 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001414 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001415 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001416 err = read_pin_defaults(codec);
1417 if (err < 0)
1418 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001419
Takashi Iwai0ba21762007-04-16 11:29:14 +02001420 if (!codec->subsystem_id) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001421 codec->subsystem_id =
Takashi Iwaid8193872012-08-31 07:54:38 -07001422 snd_hda_codec_read(codec, fg, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001423 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001424 }
1425
Takashi Iwai83012a72012-08-24 18:38:08 +02001426#ifdef CONFIG_PM
Takashi Iwaid8193872012-08-31 07:54:38 -07001427 codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg,
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001428 AC_PWRST_CLKSTOP);
1429 if (!codec->d3_stop_clk)
1430 bus->power_keep_link_on = 1;
Mengdong Lin5d6147f2012-08-24 12:06:30 +08001431#endif
Takashi Iwaid8193872012-08-31 07:54:38 -07001432 codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
Takashi Iwai983f6b92012-08-28 09:18:01 -07001433 AC_PWRST_EPSS);
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001434
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001435 /* power-up all before initialization */
Takashi Iwaid8193872012-08-31 07:54:38 -07001436 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001437
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001438 snd_hda_codec_proc_new(codec);
1439
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001440 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001441
1442 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1443 codec->subsystem_id, codec->revision_id);
1444 snd_component_add(codec->bus->card, component);
1445
1446 if (codecp)
1447 *codecp = codec;
1448 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001449
1450 error:
1451 snd_hda_codec_free(codec);
1452 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001453}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001454EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001455
Mengdong Lina15d05d2013-02-08 17:09:31 -05001456int snd_hda_codec_update_widgets(struct hda_codec *codec)
1457{
1458 hda_nid_t fg;
1459 int err;
1460
1461 /* Assume the function group node does not change,
1462 * only the widget nodes may change.
1463 */
1464 kfree(codec->wcaps);
1465 fg = codec->afg ? codec->afg : codec->mfg;
1466 err = read_widget_caps(codec, fg);
1467 if (err < 0) {
1468 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
1469 return err;
1470 }
1471
1472 snd_array_free(&codec->init_pins);
1473 err = read_pin_defaults(codec);
1474
1475 return err;
1476}
1477EXPORT_SYMBOL_HDA(snd_hda_codec_update_widgets);
1478
1479
Takashi Iwaid5191e52009-11-16 14:58:17 +01001480/**
1481 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1482 * @codec: the HDA codec
1483 *
1484 * Start parsing of the given codec tree and (re-)initialize the whole
1485 * patch instance.
1486 *
1487 * Returns 0 if successful or a negative error code.
1488 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001489int snd_hda_codec_configure(struct hda_codec *codec)
1490{
1491 int err;
1492
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001493 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001494 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001495 err = get_codec_name(codec);
1496 if (err < 0)
1497 return err;
1498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
Takashi Iwai82467612007-07-27 19:15:54 +02001500 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001502 goto patched;
1503 }
Takashi Iwai82467612007-07-27 19:15:54 +02001504 if (codec->preset && codec->preset->patch) {
1505 err = codec->preset->patch(codec);
1506 goto patched;
1507 }
1508
1509 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001510 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001511 if (err < 0)
1512 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001513
1514 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001515 if (!err && codec->patch_ops.unsol_event)
1516 err = init_unsol_queue(codec->bus);
Takashi Iwaif62faed2009-12-23 09:27:51 +01001517 /* audio codec should override the mixer name */
1518 if (!err && (codec->afg || !*codec->bus->card->mixername))
1519 snprintf(codec->bus->card->mixername,
1520 sizeof(codec->bus->card->mixername),
1521 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001522 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001524EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
Takashi Iwaied360812012-08-08 17:12:52 +02001526/* update the stream-id if changed */
1527static void update_pcm_stream_id(struct hda_codec *codec,
1528 struct hda_cvt_setup *p, hda_nid_t nid,
1529 u32 stream_tag, int channel_id)
1530{
1531 unsigned int oldval, newval;
1532
1533 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1534 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1535 newval = (stream_tag << 4) | channel_id;
1536 if (oldval != newval)
1537 snd_hda_codec_write(codec, nid, 0,
1538 AC_VERB_SET_CHANNEL_STREAMID,
1539 newval);
1540 p->stream_tag = stream_tag;
1541 p->channel_id = channel_id;
1542 }
1543}
1544
1545/* update the format-id if changed */
1546static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1547 hda_nid_t nid, int format)
1548{
1549 unsigned int oldval;
1550
1551 if (p->format_id != format) {
1552 oldval = snd_hda_codec_read(codec, nid, 0,
1553 AC_VERB_GET_STREAM_FORMAT, 0);
1554 if (oldval != format) {
1555 msleep(1);
1556 snd_hda_codec_write(codec, nid, 0,
1557 AC_VERB_SET_STREAM_FORMAT,
1558 format);
1559 }
1560 p->format_id = format;
1561 }
1562}
1563
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564/**
1565 * snd_hda_codec_setup_stream - set up the codec for streaming
1566 * @codec: the CODEC to set up
1567 * @nid: the NID to set up
1568 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1569 * @channel_id: channel id to pass, zero based.
1570 * @format: stream format.
1571 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001572void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1573 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 int channel_id, int format)
1575{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001576 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001577 struct hda_cvt_setup *p;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001578 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001579 int i;
1580
Takashi Iwai0ba21762007-04-16 11:29:14 +02001581 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001582 return;
1583
Takashi Iwai0ba21762007-04-16 11:29:14 +02001584 snd_printdd("hda_codec_setup_stream: "
1585 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001587 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001588 if (!p)
Takashi Iwaieb541332010-08-06 13:48:11 +02001589 return;
Takashi Iwaied360812012-08-08 17:12:52 +02001590
1591 if (codec->pcm_format_first)
1592 update_pcm_format(codec, p, nid, format);
1593 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1594 if (!codec->pcm_format_first)
1595 update_pcm_format(codec, p, nid, format);
1596
Takashi Iwaieb541332010-08-06 13:48:11 +02001597 p->active = 1;
1598 p->dirty = 0;
1599
1600 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001601 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001602 list_for_each_entry(c, &codec->bus->codec_list, list) {
1603 for (i = 0; i < c->cvt_setups.used; i++) {
1604 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001605 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001606 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001607 p->dirty = 1;
1608 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001609 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001611EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
Takashi Iwaif0cea792010-08-13 11:56:53 +02001613static void really_cleanup_stream(struct hda_codec *codec,
1614 struct hda_cvt_setup *q);
1615
Takashi Iwaid5191e52009-11-16 14:58:17 +01001616/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001617 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001618 * @codec: the CODEC to clean up
1619 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001620 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001621 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001622void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1623 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001624{
Takashi Iwaieb541332010-08-06 13:48:11 +02001625 struct hda_cvt_setup *p;
1626
Takashi Iwai888afa12008-03-18 09:57:50 +01001627 if (!nid)
1628 return;
1629
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001630 if (codec->no_sticky_stream)
1631 do_now = 1;
1632
Takashi Iwai888afa12008-03-18 09:57:50 +01001633 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001634 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001635 if (p) {
Takashi Iwaif0cea792010-08-13 11:56:53 +02001636 /* here we just clear the active flag when do_now isn't set;
1637 * actual clean-ups will be done later in
1638 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1639 */
1640 if (do_now)
1641 really_cleanup_stream(codec, p);
1642 else
1643 p->active = 0;
1644 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001645}
Takashi Iwaif0cea792010-08-13 11:56:53 +02001646EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001647
Takashi Iwaieb541332010-08-06 13:48:11 +02001648static void really_cleanup_stream(struct hda_codec *codec,
1649 struct hda_cvt_setup *q)
1650{
1651 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001652 if (q->stream_tag || q->channel_id)
1653 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1654 if (q->format_id)
1655 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1656);
Takashi Iwaieb541332010-08-06 13:48:11 +02001657 memset(q, 0, sizeof(*q));
1658 q->nid = nid;
1659}
1660
1661/* clean up the all conflicting obsolete streams */
1662static void purify_inactive_streams(struct hda_codec *codec)
1663{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001664 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001665 int i;
1666
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001667 list_for_each_entry(c, &codec->bus->codec_list, list) {
1668 for (i = 0; i < c->cvt_setups.used; i++) {
1669 struct hda_cvt_setup *p;
1670 p = snd_array_elem(&c->cvt_setups, i);
1671 if (p->dirty)
1672 really_cleanup_stream(c, p);
1673 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001674 }
1675}
1676
Takashi Iwai2a439522011-07-26 09:52:50 +02001677#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001678/* clean up all streams; called from suspend */
1679static void hda_cleanup_all_streams(struct hda_codec *codec)
1680{
1681 int i;
1682
1683 for (i = 0; i < codec->cvt_setups.used; i++) {
1684 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1685 if (p->stream_tag)
1686 really_cleanup_stream(codec, p);
1687 }
1688}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001689#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001690
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691/*
1692 * amp access functions
1693 */
1694
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001695/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001696#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001697#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001698#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1699#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001701#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
1703/* initialize the hash table */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001704static void init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001705 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706{
Takashi Iwai01751f52007-08-10 16:59:39 +02001707 memset(cache, 0, sizeof(*cache));
1708 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001709 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001710}
1711
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001712static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001713{
Takashi Iwai603c4012008-07-30 15:01:44 +02001714 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715}
1716
1717/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001718static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719{
Takashi Iwai01751f52007-08-10 16:59:39 +02001720 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1721 u16 cur = cache->hash[idx];
1722 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
1724 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001725 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 if (info->key == key)
1727 return info;
1728 cur = info->next;
1729 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001730 return NULL;
1731}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001733/* query the hash. allocate an entry if not found. */
1734static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1735 u32 key)
1736{
1737 struct hda_cache_head *info = get_hash(cache, key);
1738 if (!info) {
1739 u16 idx, cur;
1740 /* add a new hash entry */
1741 info = snd_array_new(&cache->buf);
1742 if (!info)
1743 return NULL;
1744 cur = snd_array_index(&cache->buf, info);
1745 info->key = key;
1746 info->val = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001747 info->dirty = 0;
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001748 idx = key % (u16)ARRAY_SIZE(cache->hash);
1749 info->next = cache->hash[idx];
1750 cache->hash[idx] = cur;
1751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 return info;
1753}
1754
Takashi Iwai01751f52007-08-10 16:59:39 +02001755/* query and allocate an amp hash entry */
1756static inline struct hda_amp_info *
1757get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1758{
1759 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1760}
1761
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001762/* overwrite the value with the key in the caps hash */
1763static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1764{
1765 struct hda_amp_info *info;
1766
1767 mutex_lock(&codec->hash_mutex);
1768 info = get_alloc_amp_hash(codec, key);
1769 if (!info) {
1770 mutex_unlock(&codec->hash_mutex);
1771 return -EINVAL;
1772 }
1773 info->amp_caps = val;
1774 info->head.val |= INFO_AMP_CAPS;
1775 mutex_unlock(&codec->hash_mutex);
1776 return 0;
1777}
1778
1779/* query the value from the caps hash; if not found, fetch the current
1780 * value from the given function and store in the hash
1781 */
1782static unsigned int
1783query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1784 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1785{
1786 struct hda_amp_info *info;
1787 unsigned int val;
1788
1789 mutex_lock(&codec->hash_mutex);
1790 info = get_alloc_amp_hash(codec, key);
1791 if (!info) {
1792 mutex_unlock(&codec->hash_mutex);
1793 return 0;
1794 }
1795 if (!(info->head.val & INFO_AMP_CAPS)) {
1796 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1797 val = func(codec, nid, dir);
1798 write_caps_hash(codec, key, val);
1799 } else {
1800 val = info->amp_caps;
1801 mutex_unlock(&codec->hash_mutex);
1802 }
1803 return val;
1804}
1805
1806static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1807 int direction)
1808{
1809 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1810 nid = codec->afg;
1811 return snd_hda_param_read(codec, nid,
1812 direction == HDA_OUTPUT ?
1813 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1814}
1815
Takashi Iwaid5191e52009-11-16 14:58:17 +01001816/**
1817 * query_amp_caps - query AMP capabilities
1818 * @codec: the HD-auio codec
1819 * @nid: the NID to query
1820 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1821 *
1822 * Query AMP capabilities for the given widget and direction.
1823 * Returns the obtained capability bits.
1824 *
1825 * When cap bits have been already read, this doesn't read again but
1826 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001828u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001830 return query_caps_hash(codec, nid, direction,
1831 HDA_HASH_KEY(nid, direction, 0),
1832 read_amp_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001834EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
Takashi Iwaid5191e52009-11-16 14:58:17 +01001836/**
1837 * snd_hda_override_amp_caps - Override the AMP capabilities
1838 * @codec: the CODEC to clean up
1839 * @nid: the NID to clean up
1840 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1841 * @caps: the capability bits to set
1842 *
1843 * Override the cached AMP caps bits value by the given one.
1844 * This function is useful if the driver needs to adjust the AMP ranges,
1845 * e.g. limit to 0dB, etc.
1846 *
1847 * Returns zero if successful or a negative error code.
1848 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001849int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1850 unsigned int caps)
1851{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001852 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001853}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001854EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001855
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001856static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
1857 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001858{
1859 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1860}
1861
Takashi Iwaid5191e52009-11-16 14:58:17 +01001862/**
1863 * snd_hda_query_pin_caps - Query PIN capabilities
1864 * @codec: the HD-auio codec
1865 * @nid: the NID to query
1866 *
1867 * Query PIN capabilities for the given widget.
1868 * Returns the obtained capability bits.
1869 *
1870 * When cap bits have been already read, this doesn't read again but
1871 * returns the cached value.
1872 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001873u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1874{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001875 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001876 read_pin_cap);
1877}
Takashi Iwai1327a322009-03-23 13:07:47 +01001878EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1879
Wu Fengguang864f92b2009-11-18 12:38:02 +08001880/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02001881 * snd_hda_override_pin_caps - Override the pin capabilities
1882 * @codec: the CODEC
1883 * @nid: the NID to override
1884 * @caps: the capability bits to set
1885 *
1886 * Override the cached PIN capabilitiy bits value by the given one.
1887 *
1888 * Returns zero if successful or a negative error code.
1889 */
1890int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1891 unsigned int caps)
1892{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001893 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02001894}
1895EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps);
1896
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001897/* read or sync the hash value with the current value;
1898 * call within hash_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 */
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001900static struct hda_amp_info *
1901update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
Takashi Iwai280e57d2012-12-14 10:32:21 +01001902 int direction, int index, bool init_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001904 struct hda_amp_info *info;
1905 unsigned int parm, val = 0;
1906 bool val_read = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001908 retry:
1909 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1910 if (!info)
1911 return NULL;
1912 if (!(info->head.val & INFO_AMP_VOL(ch))) {
1913 if (!val_read) {
1914 mutex_unlock(&codec->hash_mutex);
1915 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1916 parm |= direction == HDA_OUTPUT ?
1917 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1918 parm |= index;
1919 val = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001920 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001921 val &= 0xff;
1922 val_read = true;
1923 mutex_lock(&codec->hash_mutex);
1924 goto retry;
1925 }
1926 info->vol[ch] = val;
1927 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001928 } else if (init_only)
1929 return NULL;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001930 return info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931}
1932
1933/*
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001934 * write the current volume in info to the h/w
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 */
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001936static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001937 hda_nid_t nid, int ch, int direction, int index,
1938 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939{
1940 u32 parm;
1941
1942 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1943 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1944 parm |= index << AC_AMP_SET_INDEX_SHIFT;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001945 if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) &&
1946 (amp_caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwai38681372012-02-27 15:00:58 +01001947 ; /* set the zero value as a fake mute */
1948 else
1949 parm |= val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1951}
1952
Takashi Iwaid5191e52009-11-16 14:58:17 +01001953/**
1954 * snd_hda_codec_amp_read - Read AMP value
1955 * @codec: HD-audio codec
1956 * @nid: NID to read the AMP value
1957 * @ch: channel (left=0 or right=1)
1958 * @direction: #HDA_INPUT or #HDA_OUTPUT
1959 * @index: the index value (only for input direction)
1960 *
1961 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 */
Takashi Iwai834be882006-03-01 14:16:17 +01001963int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1964 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001966 struct hda_amp_info *info;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001967 unsigned int val = 0;
1968
1969 mutex_lock(&codec->hash_mutex);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001970 info = update_amp_hash(codec, nid, ch, direction, index, false);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001971 if (info)
1972 val = info->vol[ch];
1973 mutex_unlock(&codec->hash_mutex);
1974 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001976EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
Takashi Iwai280e57d2012-12-14 10:32:21 +01001978static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1979 int direction, int idx, int mask, int val,
1980 bool init_only)
1981{
1982 struct hda_amp_info *info;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001983 unsigned int caps;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01001984 unsigned int cache_only;
Takashi Iwai280e57d2012-12-14 10:32:21 +01001985
1986 if (snd_BUG_ON(mask & ~0xff))
1987 mask &= 0xff;
1988 val &= mask;
1989
1990 mutex_lock(&codec->hash_mutex);
1991 info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
1992 if (!info) {
1993 mutex_unlock(&codec->hash_mutex);
1994 return 0;
1995 }
1996 val |= info->vol[ch] & ~mask;
1997 if (info->vol[ch] == val) {
1998 mutex_unlock(&codec->hash_mutex);
1999 return 0;
2000 }
2001 info->vol[ch] = val;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01002002 cache_only = info->head.dirty = codec->cached_write;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002003 caps = info->amp_caps;
Takashi Iwai280e57d2012-12-14 10:32:21 +01002004 mutex_unlock(&codec->hash_mutex);
Takashi Iwaide1e37b2012-12-20 11:00:21 +01002005 if (!cache_only)
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002006 put_vol_mute(codec, caps, nid, ch, direction, idx, val);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002007 return 1;
2008}
2009
Takashi Iwaid5191e52009-11-16 14:58:17 +01002010/**
2011 * snd_hda_codec_amp_update - update the AMP value
2012 * @codec: HD-audio codec
2013 * @nid: NID to read the AMP value
2014 * @ch: channel (left=0 or right=1)
2015 * @direction: #HDA_INPUT or #HDA_OUTPUT
2016 * @idx: the index value (only for input direction)
2017 * @mask: bit mask to set
2018 * @val: the bits value to set
2019 *
2020 * Update the AMP value with a bit mask.
2021 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002022 */
Takashi Iwai834be882006-03-01 14:16:17 +01002023int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
2024 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025{
Takashi Iwai280e57d2012-12-14 10:32:21 +01002026 return codec_amp_update(codec, nid, ch, direction, idx, mask, val, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002028EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
Takashi Iwaid5191e52009-11-16 14:58:17 +01002030/**
2031 * snd_hda_codec_amp_stereo - update the AMP stereo values
2032 * @codec: HD-audio codec
2033 * @nid: NID to read the AMP value
2034 * @direction: #HDA_INPUT or #HDA_OUTPUT
2035 * @idx: the index value (only for input direction)
2036 * @mask: bit mask to set
2037 * @val: the bits value to set
2038 *
2039 * Update the AMP values like snd_hda_codec_amp_update(), but for a
2040 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02002041 */
2042int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
2043 int direction, int idx, int mask, int val)
2044{
2045 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02002046
2047 if (snd_BUG_ON(mask & ~0xff))
2048 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02002049 for (ch = 0; ch < 2; ch++)
2050 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
2051 idx, mask, val);
2052 return ret;
2053}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002054EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02002055
Takashi Iwai280e57d2012-12-14 10:32:21 +01002056/* Works like snd_hda_codec_amp_update() but it writes the value only at
2057 * the first access. If the amp was already initialized / updated beforehand,
2058 * this does nothing.
2059 */
2060int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
2061 int dir, int idx, int mask, int val)
2062{
2063 return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true);
2064}
2065EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init);
2066
2067int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
2068 int dir, int idx, int mask, int val)
2069{
2070 int ch, ret = 0;
2071
2072 if (snd_BUG_ON(mask & ~0xff))
2073 mask &= 0xff;
2074 for (ch = 0; ch < 2; ch++)
2075 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
2076 idx, mask, val);
2077 return ret;
2078}
2079EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init_stereo);
2080
Takashi Iwai4453c822015-04-27 10:36:11 +02002081/* meta hook to call each driver's vmaster hook */
2082static void vmaster_hook(void *private_data, int enabled)
2083{
2084 struct hda_vmaster_mute_hook *hook = private_data;
2085
2086 if (hook->mute_mode != HDA_VMUTE_FOLLOW_MASTER)
2087 enabled = hook->mute_mode;
2088 hook->hook(hook->codec, enabled);
2089}
2090
Takashi Iwaid5191e52009-11-16 14:58:17 +01002091/**
2092 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
2093 * @codec: HD-audio codec
2094 *
2095 * Resume the all amp commands from the cache.
2096 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002097void snd_hda_codec_resume_amp(struct hda_codec *codec)
2098{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002099 int i;
2100
Takashi Iwaic370dd62012-12-13 18:30:04 +01002101 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01002102 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002103 for (i = 0; i < codec->amp_cache.buf.used; i++) {
2104 struct hda_amp_info *buffer;
2105 u32 key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002106 hda_nid_t nid;
2107 unsigned int idx, dir, ch;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002108 struct hda_amp_info info;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002109
2110 buffer = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwai8565f052012-12-20 12:54:18 +01002111 if (!buffer->head.dirty)
2112 continue;
2113 buffer->head.dirty = 0;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002114 info = *buffer;
2115 key = info.head.key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002116 if (!key)
2117 continue;
2118 nid = key & 0xff;
2119 idx = (key >> 16) & 0xff;
2120 dir = (key >> 24) & 0xff;
2121 for (ch = 0; ch < 2; ch++) {
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002122 if (!(info.head.val & INFO_AMP_VOL(ch)))
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002123 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002124 mutex_unlock(&codec->hash_mutex);
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002125 put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx,
2126 info.vol[ch]);
Takashi Iwaic370dd62012-12-13 18:30:04 +01002127 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002128 }
2129 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01002130 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002131}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002132EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002134static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
2135 unsigned int ofs)
2136{
2137 u32 caps = query_amp_caps(codec, nid, dir);
2138 /* get num steps */
2139 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2140 if (ofs < caps)
2141 caps -= ofs;
2142 return caps;
2143}
2144
Takashi Iwaid5191e52009-11-16 14:58:17 +01002145/**
2146 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
2147 *
2148 * The control element is supposed to have the private_value field
2149 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2150 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002151int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
2152 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153{
2154 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2155 u16 nid = get_amp_nid(kcontrol);
2156 u8 chs = get_amp_channels(kcontrol);
2157 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002158 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002160 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2161 uinfo->count = chs == 3 ? 2 : 1;
2162 uinfo->value.integer.min = 0;
2163 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
2164 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002165 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01002166 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
2167 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 return -EINVAL;
2169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 return 0;
2171}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002172EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002174
2175static inline unsigned int
2176read_amp_value(struct hda_codec *codec, hda_nid_t nid,
2177 int ch, int dir, int idx, unsigned int ofs)
2178{
2179 unsigned int val;
2180 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
2181 val &= HDA_AMP_VOLMASK;
2182 if (val >= ofs)
2183 val -= ofs;
2184 else
2185 val = 0;
2186 return val;
2187}
2188
2189static inline int
2190update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2191 int ch, int dir, int idx, unsigned int ofs,
2192 unsigned int val)
2193{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002194 unsigned int maxval;
2195
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002196 if (val > 0)
2197 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02002198 /* ofs = 0: raw max value */
2199 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002200 if (val > maxval)
2201 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002202 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
2203 HDA_AMP_VOLMASK, val);
2204}
2205
Takashi Iwaid5191e52009-11-16 14:58:17 +01002206/**
2207 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
2208 *
2209 * The control element is supposed to have the private_value field
2210 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2211 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002212int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2213 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214{
2215 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2216 hda_nid_t nid = get_amp_nid(kcontrol);
2217 int chs = get_amp_channels(kcontrol);
2218 int dir = get_amp_direction(kcontrol);
2219 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002220 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 long *valp = ucontrol->value.integer.value;
2222
2223 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002224 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002226 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 return 0;
2228}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002229EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230
Takashi Iwaid5191e52009-11-16 14:58:17 +01002231/**
2232 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2233 *
2234 * The control element is supposed to have the private_value field
2235 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2236 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002237int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2238 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239{
2240 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2241 hda_nid_t nid = get_amp_nid(kcontrol);
2242 int chs = get_amp_channels(kcontrol);
2243 int dir = get_amp_direction(kcontrol);
2244 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002245 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 long *valp = ucontrol->value.integer.value;
2247 int change = 0;
2248
Takashi Iwaicb53c622007-08-10 17:21:45 +02002249 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002250 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002251 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002252 valp++;
2253 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002254 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002255 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002256 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 return change;
2258}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002259EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
Takashi Iwaid5191e52009-11-16 14:58:17 +01002261/**
2262 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2263 *
2264 * The control element is supposed to have the private_value field
2265 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2266 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002267int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2268 unsigned int size, unsigned int __user *_tlv)
2269{
2270 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2271 hda_nid_t nid = get_amp_nid(kcontrol);
2272 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002273 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002274 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002275 u32 caps, val1, val2;
2276
2277 if (size < 4 * sizeof(unsigned int))
2278 return -ENOMEM;
2279 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002280 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2281 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002282 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002283 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002284 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01002285 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02002286 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002287 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2288 return -EFAULT;
2289 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2290 return -EFAULT;
2291 if (put_user(val1, _tlv + 2))
2292 return -EFAULT;
2293 if (put_user(val2, _tlv + 3))
2294 return -EFAULT;
2295 return 0;
2296}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002297EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002298
Takashi Iwaid5191e52009-11-16 14:58:17 +01002299/**
2300 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2301 * @codec: HD-audio codec
2302 * @nid: NID of a reference widget
2303 * @dir: #HDA_INPUT or #HDA_OUTPUT
2304 * @tlv: TLV data to be stored, at least 4 elements
2305 *
2306 * Set (static) TLV data for a virtual master volume using the AMP caps
2307 * obtained from the reference NID.
2308 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002309 */
2310void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2311 unsigned int *tlv)
2312{
2313 u32 caps;
2314 int nums, step;
2315
2316 caps = query_amp_caps(codec, nid, dir);
2317 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2318 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2319 step = (step + 1) * 25;
2320 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2321 tlv[1] = 2 * sizeof(unsigned int);
2322 tlv[2] = -nums * step;
2323 tlv[3] = step;
2324}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002325EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002326
2327/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002328static struct snd_kcontrol *
Takashi Iwaidcda5802012-10-12 17:24:51 +02002329find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002330{
2331 struct snd_ctl_elem_id id;
2332 memset(&id, 0, sizeof(id));
2333 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002334 id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01002335 id.index = idx;
Takashi Iwai18cb71092009-04-16 10:22:24 +02002336 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2337 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002338 strcpy(id.name, name);
2339 return snd_ctl_find_id(codec->bus->card, &id);
2340}
2341
Takashi Iwaid5191e52009-11-16 14:58:17 +01002342/**
2343 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2344 * @codec: HD-audio codec
2345 * @name: ctl id name string
2346 *
2347 * Get the control element with the given id string and IFACE_MIXER.
2348 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002349struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2350 const char *name)
2351{
Takashi Iwaidcda5802012-10-12 17:24:51 +02002352 return find_mixer_ctl(codec, name, 0, 0);
Takashi Iwai09f99702008-02-04 12:31:13 +01002353}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002354EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002355
Takashi Iwaidcda5802012-10-12 17:24:51 +02002356static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002357 int start_idx)
Takashi Iwai1afe2062010-12-23 10:17:52 +01002358{
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002359 int i, idx;
2360 /* 16 ctlrs should be large enough */
2361 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
2362 if (!find_mixer_ctl(codec, name, 0, idx))
Takashi Iwai1afe2062010-12-23 10:17:52 +01002363 return idx;
2364 }
2365 return -EBUSY;
2366}
2367
Takashi Iwaid5191e52009-11-16 14:58:17 +01002368/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002369 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002370 * @codec: HD-audio codec
2371 * @nid: corresponding NID (optional)
2372 * @kctl: the control element to assign
2373 *
2374 * Add the given control element to an array inside the codec instance.
2375 * All control elements belonging to a codec are supposed to be added
2376 * by this function so that a proper clean-up works at the free or
2377 * reconfiguration time.
2378 *
2379 * If non-zero @nid is passed, the NID is assigned to the control element.
2380 * The assignment is shown in the codec proc file.
2381 *
2382 * snd_hda_ctl_add() checks the control subdev id field whether
2383 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002384 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2385 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002386 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002387int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2388 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002389{
2390 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002391 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002392 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002393
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002394 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002395 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002396 if (nid == 0)
2397 nid = get_amp_nid_(kctl->private_value);
2398 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002399 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2400 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002401 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002402 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002403 err = snd_ctl_add(codec->bus->card, kctl);
2404 if (err < 0)
2405 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002406 item = snd_array_new(&codec->mixers);
2407 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002408 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002409 item->kctl = kctl;
2410 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002411 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002412 return 0;
2413}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002414EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002415
Takashi Iwaid5191e52009-11-16 14:58:17 +01002416/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002417 * snd_hda_add_nid - Assign a NID to a control element
2418 * @codec: HD-audio codec
2419 * @nid: corresponding NID (optional)
2420 * @kctl: the control element to assign
2421 * @index: index to kctl
2422 *
2423 * Add the given control element to an array inside the codec instance.
2424 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2425 * NID:KCTL mapping - for example "Capture Source" selector.
2426 */
2427int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2428 unsigned int index, hda_nid_t nid)
2429{
2430 struct hda_nid_item *item;
2431
2432 if (nid > 0) {
2433 item = snd_array_new(&codec->nids);
2434 if (!item)
2435 return -ENOMEM;
2436 item->kctl = kctl;
2437 item->index = index;
2438 item->nid = nid;
2439 return 0;
2440 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01002441 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2442 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002443 return -EINVAL;
2444}
2445EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2446
2447/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002448 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2449 * @codec: HD-audio codec
2450 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002451void snd_hda_ctls_clear(struct hda_codec *codec)
2452{
2453 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002454 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002455 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002456 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002457 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002458 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002459}
2460
Takashi Iwaia65d6292009-02-23 16:57:04 +01002461/* pseudo device locking
2462 * toggle card->shutdown to allow/disallow the device access (as a hack)
2463 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002464int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002465{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002466 struct snd_card *card = bus->card;
2467 struct hda_codec *codec;
2468
Takashi Iwaia65d6292009-02-23 16:57:04 +01002469 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002470 if (card->shutdown)
2471 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002472 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002473 if (!list_empty(&card->ctl_files))
2474 goto err_clear;
2475
2476 list_for_each_entry(codec, &bus->codec_list, list) {
2477 int pcm;
2478 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2479 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2480 if (!cpcm->pcm)
2481 continue;
2482 if (cpcm->pcm->streams[0].substream_opened ||
2483 cpcm->pcm->streams[1].substream_opened)
2484 goto err_clear;
2485 }
2486 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01002487 spin_unlock(&card->files_lock);
2488 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002489
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002490 err_clear:
2491 card->shutdown = 0;
2492 err_unlock:
2493 spin_unlock(&card->files_lock);
2494 return -EINVAL;
2495}
2496EXPORT_SYMBOL_HDA(snd_hda_lock_devices);
2497
2498void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002499{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002500 struct snd_card *card = bus->card;
2501
2502 card = bus->card;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002503 spin_lock(&card->files_lock);
2504 card->shutdown = 0;
2505 spin_unlock(&card->files_lock);
2506}
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002507EXPORT_SYMBOL_HDA(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002508
Takashi Iwaid5191e52009-11-16 14:58:17 +01002509/**
2510 * snd_hda_codec_reset - Clear all objects assigned to the codec
2511 * @codec: HD-audio codec
2512 *
2513 * This frees the all PCM and control elements assigned to the codec, and
2514 * clears the caches and restores the pin default configurations.
2515 *
2516 * When a device is being used, it returns -EBSY. If successfully freed,
2517 * returns zero.
2518 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002519int snd_hda_codec_reset(struct hda_codec *codec)
2520{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002521 struct hda_bus *bus = codec->bus;
2522 struct snd_card *card = bus->card;
2523 int i;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002524
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002525 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002526 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002527
2528 /* OK, let it free */
David Henningsson26a6cb62012-10-09 15:04:21 +02002529 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai83012a72012-08-24 18:38:08 +02002530#ifdef CONFIG_PM
Takashi Iwaia2d96e72012-05-09 12:36:22 +02002531 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002532 flush_workqueue(bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002533#endif
2534 snd_hda_ctls_clear(codec);
2535 /* relase PCMs */
2536 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002537 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002538 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002539 clear_bit(codec->pcm_info[i].device,
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002540 bus->pcm_dev_bits);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002541 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002542 }
2543 if (codec->patch_ops.free)
2544 codec->patch_ops.free(codec);
Takashi Iwai07dc59f2012-09-10 09:39:31 +02002545 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002546 snd_hda_jack_tbl_clear(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002547 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002548 codec->spec = NULL;
2549 free_hda_cache(&codec->amp_cache);
2550 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002551 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2552 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002553 /* free only driver_pins so that init_pins + user_pins are restored */
2554 snd_array_free(&codec->driver_pins);
Takashi Iwai09a60712012-06-26 15:01:33 +02002555 snd_array_free(&codec->cvt_setups);
2556 snd_array_free(&codec->spdif_out);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01002557 snd_array_free(&codec->verbs);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002558 codec->num_pcms = 0;
2559 codec->pcm_info = NULL;
2560 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002561 codec->slave_dig_outs = NULL;
2562 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002563 module_put(codec->owner);
2564 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002565
2566 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002567 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002568 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002569}
2570
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002571typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2572
2573/* apply the function to all matching slave ctls in the mixer list */
2574static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002575 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002576{
2577 struct hda_nid_item *items;
2578 const char * const *s;
2579 int i, err;
2580
2581 items = codec->mixers.list;
2582 for (i = 0; i < codec->mixers.used; i++) {
2583 struct snd_kcontrol *sctl = items[i].kctl;
2584 if (!sctl || !sctl->id.name ||
2585 sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2586 continue;
2587 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002588 char tmpname[sizeof(sctl->id.name)];
2589 const char *name = *s;
2590 if (suffix) {
2591 snprintf(tmpname, sizeof(tmpname), "%s %s",
2592 name, suffix);
2593 name = tmpname;
2594 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002595 if (!strcmp(sctl->id.name, name)) {
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002596 err = func(data, sctl);
2597 if (err)
2598 return err;
2599 break;
2600 }
2601 }
2602 }
2603 return 0;
2604}
2605
2606static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2607{
2608 return 1;
2609}
2610
Takashi Iwai18478e82012-03-09 17:51:10 +01002611/* guess the value corresponding to 0dB */
2612static int get_kctl_0dB_offset(struct snd_kcontrol *kctl)
2613{
2614 int _tlv[4];
2615 const int *tlv = NULL;
2616 int val = -1;
2617
2618 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2619 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2620 mm_segment_t fs = get_fs();
2621 set_fs(get_ds());
2622 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2623 tlv = _tlv;
2624 set_fs(fs);
2625 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2626 tlv = kctl->tlv.p;
2627 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE)
2628 val = -tlv[2] / tlv[3];
2629 return val;
2630}
2631
2632/* call kctl->put with the given value(s) */
2633static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2634{
2635 struct snd_ctl_elem_value *ucontrol;
2636 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2637 if (!ucontrol)
2638 return -ENOMEM;
2639 ucontrol->value.integer.value[0] = val;
2640 ucontrol->value.integer.value[1] = val;
2641 kctl->put(kctl, ucontrol);
2642 kfree(ucontrol);
2643 return 0;
2644}
2645
2646/* initialize the slave volume with 0dB */
2647static int init_slave_0dB(void *data, struct snd_kcontrol *slave)
2648{
2649 int offset = get_kctl_0dB_offset(slave);
2650 if (offset > 0)
2651 put_kctl_with_value(slave, offset);
2652 return 0;
2653}
2654
2655/* unmute the slave */
2656static int init_slave_unmute(void *data, struct snd_kcontrol *slave)
2657{
2658 return put_kctl_with_value(slave, 1);
2659}
2660
Takashi Iwaid5191e52009-11-16 14:58:17 +01002661/**
2662 * snd_hda_add_vmaster - create a virtual master control and add slaves
2663 * @codec: HD-audio codec
2664 * @name: vmaster control name
2665 * @tlv: TLV data (optional)
2666 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002667 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002668 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002669 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002670 *
2671 * Create a virtual master control with the given name. The TLV data
2672 * must be either NULL or a valid data.
2673 *
2674 * @slaves is a NULL-terminated array of strings, each of which is a
2675 * slave control name. All controls with these names are assigned to
2676 * the new virtual master control.
2677 *
2678 * This function returns zero if successful or a negative error code.
2679 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002680int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002681 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002682 const char *suffix, bool init_slave_vol,
2683 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002684{
2685 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002686 int err;
2687
Takashi Iwai29e58532012-03-12 12:25:03 +01002688 if (ctl_ret)
2689 *ctl_ret = NULL;
2690
Takashi Iwai9322ca52012-02-03 14:28:01 +01002691 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002692 if (err != 1) {
Takashi Iwai2f085542008-02-22 18:43:50 +01002693 snd_printdd("No slave found for %s\n", name);
2694 return 0;
2695 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002696 kctl = snd_ctl_make_virtual_master(name, tlv);
2697 if (!kctl)
2698 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002699 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002700 if (err < 0)
2701 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002702
Takashi Iwai9322ca52012-02-03 14:28:01 +01002703 err = map_slaves(codec, slaves, suffix,
2704 (map_slave_func_t)snd_ctl_add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002705 if (err < 0)
2706 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002707
2708 /* init with master mute & zero volume */
2709 put_kctl_with_value(kctl, 0);
2710 if (init_slave_vol)
2711 map_slaves(codec, slaves, suffix,
2712 tlv ? init_slave_0dB : init_slave_unmute, kctl);
2713
Takashi Iwai29e58532012-03-12 12:25:03 +01002714 if (ctl_ret)
2715 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002716 return 0;
2717}
Takashi Iwai18478e82012-03-09 17:51:10 +01002718EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002719
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002720/*
2721 * mute-LED control using vmaster
2722 */
2723static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2724 struct snd_ctl_elem_info *uinfo)
2725{
2726 static const char * const texts[] = {
David Henningssonc86c2d42013-01-03 14:12:29 +01002727 "On", "Off", "Follow Master"
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002728 };
2729 unsigned int index;
2730
2731 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2732 uinfo->count = 1;
2733 uinfo->value.enumerated.items = 3;
2734 index = uinfo->value.enumerated.item;
2735 if (index >= 3)
2736 index = 2;
2737 strcpy(uinfo->value.enumerated.name, texts[index]);
2738 return 0;
2739}
2740
2741static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2742 struct snd_ctl_elem_value *ucontrol)
2743{
2744 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2745 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2746 return 0;
2747}
2748
2749static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2750 struct snd_ctl_elem_value *ucontrol)
2751{
2752 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2753 unsigned int old_mode = hook->mute_mode;
2754
2755 hook->mute_mode = ucontrol->value.enumerated.item[0];
2756 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2757 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2758 if (old_mode == hook->mute_mode)
2759 return 0;
2760 snd_hda_sync_vmaster_hook(hook);
2761 return 1;
2762}
2763
2764static struct snd_kcontrol_new vmaster_mute_mode = {
2765 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2766 .name = "Mute-LED Mode",
2767 .info = vmaster_mute_mode_info,
2768 .get = vmaster_mute_mode_get,
2769 .put = vmaster_mute_mode_put,
2770};
2771
2772/*
2773 * Add a mute-LED hook with the given vmaster switch kctl
2774 * "Mute-LED Mode" control is automatically created and associated with
2775 * the given hook.
2776 */
2777int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735c2012-03-13 07:55:10 +01002778 struct hda_vmaster_mute_hook *hook,
2779 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002780{
2781 struct snd_kcontrol *kctl;
2782
2783 if (!hook->hook || !hook->sw_kctl)
2784 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002785 hook->codec = codec;
2786 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwai4453c822015-04-27 10:36:11 +02002787 snd_ctl_add_vmaster_hook(hook->sw_kctl, vmaster_hook, hook);
Takashi Iwaif29735c2012-03-13 07:55:10 +01002788 if (!expose_enum_ctl)
2789 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002790 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2791 if (!kctl)
2792 return -ENOMEM;
2793 return snd_hda_ctl_add(codec, 0, kctl);
2794}
2795EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook);
2796
2797/*
2798 * Call the hook with the current value for synchronization
2799 * Should be called in init callback
2800 */
2801void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2802{
2803 if (!hook->hook || !hook->codec)
2804 return;
Takashi Iwai594813f2013-04-17 18:16:05 +02002805 /* don't call vmaster hook in the destructor since it might have
2806 * been already destroyed
2807 */
2808 if (hook->codec->bus->shutdown)
2809 return;
Takashi Iwai4453c822015-04-27 10:36:11 +02002810 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002811}
2812EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook);
2813
2814
Takashi Iwaid5191e52009-11-16 14:58:17 +01002815/**
2816 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2817 *
2818 * The control element is supposed to have the private_value field
2819 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2820 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002821int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2822 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823{
2824 int chs = get_amp_channels(kcontrol);
2825
2826 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2827 uinfo->count = chs == 3 ? 2 : 1;
2828 uinfo->value.integer.min = 0;
2829 uinfo->value.integer.max = 1;
2830 return 0;
2831}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002832EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833
Takashi Iwaid5191e52009-11-16 14:58:17 +01002834/**
2835 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2836 *
2837 * The control element is supposed to have the private_value field
2838 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2839 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002840int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2841 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842{
2843 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2844 hda_nid_t nid = get_amp_nid(kcontrol);
2845 int chs = get_amp_channels(kcontrol);
2846 int dir = get_amp_direction(kcontrol);
2847 int idx = get_amp_index(kcontrol);
2848 long *valp = ucontrol->value.integer.value;
2849
2850 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002851 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002852 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002854 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002855 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 return 0;
2857}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002858EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859
Takashi Iwaid5191e52009-11-16 14:58:17 +01002860/**
2861 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2862 *
2863 * The control element is supposed to have the private_value field
2864 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2865 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002866int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2867 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868{
2869 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2870 hda_nid_t nid = get_amp_nid(kcontrol);
2871 int chs = get_amp_channels(kcontrol);
2872 int dir = get_amp_direction(kcontrol);
2873 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 long *valp = ucontrol->value.integer.value;
2875 int change = 0;
2876
Takashi Iwaicb53c622007-08-10 17:21:45 +02002877 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002878 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002879 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002880 HDA_AMP_MUTE,
2881 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002882 valp++;
2883 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002884 if (chs & 2)
2885 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002886 HDA_AMP_MUTE,
2887 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002888 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002889 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 return change;
2891}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002892EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893
2894/*
Takashi Iwai985be542005-11-02 18:26:49 +01002895 * bound volume controls
2896 *
2897 * bind multiple volumes (# indices, from 0)
2898 */
2899
2900#define AMP_VAL_IDX_SHIFT 19
2901#define AMP_VAL_IDX_MASK (0x0f<<19)
2902
Takashi Iwaid5191e52009-11-16 14:58:17 +01002903/**
2904 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2905 *
2906 * The control element is supposed to have the private_value field
2907 * set up via HDA_BIND_MUTE*() macros.
2908 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002909int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2910 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002911{
2912 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2913 unsigned long pval;
2914 int err;
2915
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002916 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002917 pval = kcontrol->private_value;
2918 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2919 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2920 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002921 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002922 return err;
2923}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002924EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002925
Takashi Iwaid5191e52009-11-16 14:58:17 +01002926/**
2927 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2928 *
2929 * The control element is supposed to have the private_value field
2930 * set up via HDA_BIND_MUTE*() macros.
2931 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002932int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2933 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002934{
2935 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2936 unsigned long pval;
2937 int i, indices, err = 0, change = 0;
2938
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002939 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002940 pval = kcontrol->private_value;
2941 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2942 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002943 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2944 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002945 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2946 if (err < 0)
2947 break;
2948 change |= err;
2949 }
2950 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002951 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002952 return err < 0 ? err : change;
2953}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002954EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002955
Takashi Iwaid5191e52009-11-16 14:58:17 +01002956/**
2957 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2958 *
2959 * The control element is supposed to have the private_value field
2960 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002961 */
2962int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2963 struct snd_ctl_elem_info *uinfo)
2964{
2965 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2966 struct hda_bind_ctls *c;
2967 int err;
2968
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002969 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002970 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002971 kcontrol->private_value = *c->values;
2972 err = c->ops->info(kcontrol, uinfo);
2973 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002974 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002975 return err;
2976}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002977EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002978
Takashi Iwaid5191e52009-11-16 14:58:17 +01002979/**
2980 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2981 *
2982 * The control element is supposed to have the private_value field
2983 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2984 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002985int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2986 struct snd_ctl_elem_value *ucontrol)
2987{
2988 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2989 struct hda_bind_ctls *c;
2990 int err;
2991
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002992 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002993 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002994 kcontrol->private_value = *c->values;
2995 err = c->ops->get(kcontrol, ucontrol);
2996 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002997 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002998 return err;
2999}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003000EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02003001
Takashi Iwaid5191e52009-11-16 14:58:17 +01003002/**
3003 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
3004 *
3005 * The control element is supposed to have the private_value field
3006 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3007 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003008int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
3009 struct snd_ctl_elem_value *ucontrol)
3010{
3011 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3012 struct hda_bind_ctls *c;
3013 unsigned long *vals;
3014 int err = 0, change = 0;
3015
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003016 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003017 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003018 for (vals = c->values; *vals; vals++) {
3019 kcontrol->private_value = *vals;
3020 err = c->ops->put(kcontrol, ucontrol);
3021 if (err < 0)
3022 break;
3023 change |= err;
3024 }
3025 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003026 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003027 return err < 0 ? err : change;
3028}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003029EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02003030
Takashi Iwaid5191e52009-11-16 14:58:17 +01003031/**
3032 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
3033 *
3034 * The control element is supposed to have the private_value field
3035 * set up via HDA_BIND_VOL() macro.
3036 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003037int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
3038 unsigned int size, unsigned int __user *tlv)
3039{
3040 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3041 struct hda_bind_ctls *c;
3042 int err;
3043
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003044 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003045 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003046 kcontrol->private_value = *c->values;
3047 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
3048 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003049 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003050 return err;
3051}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003052EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02003053
3054struct hda_ctl_ops snd_hda_bind_vol = {
3055 .info = snd_hda_mixer_amp_volume_info,
3056 .get = snd_hda_mixer_amp_volume_get,
3057 .put = snd_hda_mixer_amp_volume_put,
3058 .tlv = snd_hda_mixer_amp_tlv
3059};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003060EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02003061
3062struct hda_ctl_ops snd_hda_bind_sw = {
3063 .info = snd_hda_mixer_amp_switch_info,
3064 .get = snd_hda_mixer_amp_switch_get,
3065 .put = snd_hda_mixer_amp_switch_put,
3066 .tlv = snd_hda_mixer_amp_tlv
3067};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003068EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02003069
3070/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 * SPDIF out controls
3072 */
3073
Takashi Iwai0ba21762007-04-16 11:29:14 +02003074static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
3075 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076{
3077 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
3078 uinfo->count = 1;
3079 return 0;
3080}
3081
Takashi Iwai0ba21762007-04-16 11:29:14 +02003082static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
3083 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084{
3085 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3086 IEC958_AES0_NONAUDIO |
3087 IEC958_AES0_CON_EMPHASIS_5015 |
3088 IEC958_AES0_CON_NOT_COPYRIGHT;
3089 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
3090 IEC958_AES1_CON_ORIGINAL;
3091 return 0;
3092}
3093
Takashi Iwai0ba21762007-04-16 11:29:14 +02003094static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
3095 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096{
3097 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3098 IEC958_AES0_NONAUDIO |
3099 IEC958_AES0_PRO_EMPHASIS_5015;
3100 return 0;
3101}
3102
Takashi Iwai0ba21762007-04-16 11:29:14 +02003103static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
3104 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105{
3106 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003107 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003108 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003110 mutex_lock(&codec->spdif_mutex);
3111 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003112 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
3113 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
3114 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
3115 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003116 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117
3118 return 0;
3119}
3120
3121/* convert from SPDIF status bits to HDA SPDIF bits
3122 * bit 0 (DigEn) is always set zero (to be filled later)
3123 */
3124static unsigned short convert_from_spdif_status(unsigned int sbits)
3125{
3126 unsigned short val = 0;
3127
3128 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003129 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003131 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003133 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
3134 IEC958_AES0_PRO_EMPHASIS_5015)
3135 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003137 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
3138 IEC958_AES0_CON_EMPHASIS_5015)
3139 val |= AC_DIG1_EMPHASIS;
3140 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
3141 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02003143 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
3145 }
3146 return val;
3147}
3148
3149/* convert to SPDIF status bits from HDA SPDIF bits
3150 */
3151static unsigned int convert_to_spdif_status(unsigned short val)
3152{
3153 unsigned int sbits = 0;
3154
Takashi Iwai0ba21762007-04-16 11:29:14 +02003155 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003157 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 sbits |= IEC958_AES0_PROFESSIONAL;
3159 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwaia686fd12013-03-20 15:42:00 +01003160 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
3162 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003163 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003165 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003167 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3169 sbits |= val & (0x7f << 8);
3170 }
3171 return sbits;
3172}
3173
Takashi Iwai2f728532008-09-25 16:32:41 +02003174/* set digital convert verbs both for the given NID and its slaves */
3175static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3176 int verb, int val)
3177{
Takashi Iwaidda14412011-05-02 11:29:30 +02003178 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02003179
Takashi Iwai9e976972008-11-25 08:17:20 +01003180 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003181 d = codec->slave_dig_outs;
3182 if (!d)
3183 return;
3184 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01003185 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003186}
3187
3188static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3189 int dig1, int dig2)
3190{
3191 if (dig1 != -1)
3192 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3193 if (dig2 != -1)
3194 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3195}
3196
Takashi Iwai0ba21762007-04-16 11:29:14 +02003197static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3198 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199{
3200 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003201 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003202 struct hda_spdif_out *spdif;
3203 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 unsigned short val;
3205 int change;
3206
Ingo Molnar62932df2006-01-16 16:34:20 +01003207 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003208 spdif = snd_array_elem(&codec->spdif_out, idx);
3209 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003210 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3212 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3213 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c935972011-06-01 11:14:17 -06003214 val = convert_from_spdif_status(spdif->status);
3215 val |= spdif->ctls & 1;
3216 change = spdif->ctls != val;
3217 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003218 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02003219 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01003220 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 return change;
3222}
3223
Takashi Iwaia5ce8892007-07-23 15:42:26 +02003224#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225
Takashi Iwai0ba21762007-04-16 11:29:14 +02003226static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3227 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228{
3229 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003230 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003231 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003233 mutex_lock(&codec->spdif_mutex);
3234 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003235 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003236 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 return 0;
3238}
3239
Stephen Warren74b654c2011-06-01 11:14:18 -06003240static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3241 int dig1, int dig2)
3242{
3243 set_dig_out_convert(codec, nid, dig1, dig2);
3244 /* unmute amp switch (if any) */
3245 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3246 (dig1 & AC_DIG1_ENABLE))
3247 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3248 HDA_AMP_MUTE, 0);
3249}
3250
Takashi Iwai0ba21762007-04-16 11:29:14 +02003251static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3252 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253{
3254 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003255 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003256 struct hda_spdif_out *spdif;
3257 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 unsigned short val;
3259 int change;
3260
Ingo Molnar62932df2006-01-16 16:34:20 +01003261 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003262 spdif = snd_array_elem(&codec->spdif_out, idx);
3263 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003264 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003266 val |= AC_DIG1_ENABLE;
Stephen Warren7c935972011-06-01 11:14:17 -06003267 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003268 spdif->ctls = val;
3269 if (change && nid != (u16)-1)
3270 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003271 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 return change;
3273}
3274
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003275static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 {
3277 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3278 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003279 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 .info = snd_hda_spdif_mask_info,
3281 .get = snd_hda_spdif_cmask_get,
3282 },
3283 {
3284 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3285 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003286 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 .info = snd_hda_spdif_mask_info,
3288 .get = snd_hda_spdif_pmask_get,
3289 },
3290 {
3291 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003292 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 .info = snd_hda_spdif_mask_info,
3294 .get = snd_hda_spdif_default_get,
3295 .put = snd_hda_spdif_default_put,
3296 },
3297 {
3298 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003299 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 .info = snd_hda_spdif_out_switch_info,
3301 .get = snd_hda_spdif_out_switch_get,
3302 .put = snd_hda_spdif_out_switch_put,
3303 },
3304 { } /* end */
3305};
3306
3307/**
Takashi Iwaidcda5802012-10-12 17:24:51 +02003308 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 * @codec: the HDA codec
Takashi Iwaidcda5802012-10-12 17:24:51 +02003310 * @associated_nid: NID that new ctls associated with
3311 * @cvt_nid: converter NID
3312 * @type: HDA_PCM_TYPE_*
3313 * Creates controls related with the digital output.
3314 * Called from each patch supporting the digital out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 *
3316 * Returns 0 if successful, or a negative error code.
3317 */
Takashi Iwaidcda5802012-10-12 17:24:51 +02003318int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3319 hda_nid_t associated_nid,
3320 hda_nid_t cvt_nid,
3321 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322{
3323 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003324 struct snd_kcontrol *kctl;
3325 struct snd_kcontrol_new *dig_mix;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003326 int idx = 0;
3327 const int spdif_index = 16;
Stephen Warren7c935972011-06-01 11:14:17 -06003328 struct hda_spdif_out *spdif;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003329 struct hda_bus *bus = codec->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003331 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003332 type == HDA_PCM_TYPE_SPDIF) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003333 idx = spdif_index;
3334 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003335 type == HDA_PCM_TYPE_HDMI) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003336 /* suppose a single SPDIF device */
3337 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3338 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
3339 if (!kctl)
3340 break;
3341 kctl->id.index = spdif_index;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003342 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003343 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003344 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003345 if (!bus->primary_dig_out_type)
3346 bus->primary_dig_out_type = type;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003347
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003348 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003349 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003350 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
3351 return -EBUSY;
3352 }
Stephen Warren7c935972011-06-01 11:14:17 -06003353 spdif = snd_array_new(&codec->spdif_out);
Mengdong Lin25336e82013-03-07 14:10:25 -05003354 if (!spdif)
3355 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3357 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003358 if (!kctl)
3359 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01003360 kctl->id.index = idx;
Stephen Warren7c935972011-06-01 11:14:17 -06003361 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003362 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003363 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 return err;
3365 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003366 spdif->nid = cvt_nid;
3367 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c935972011-06-01 11:14:17 -06003368 AC_VERB_GET_DIGI_CONVERT_1, 0);
3369 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 return 0;
3371}
Takashi Iwaidcda5802012-10-12 17:24:51 +02003372EXPORT_SYMBOL_HDA(snd_hda_create_dig_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003374/* get the hda_spdif_out entry from the given NID
3375 * call within spdif_mutex lock
3376 */
Stephen Warren7c935972011-06-01 11:14:17 -06003377struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3378 hda_nid_t nid)
3379{
3380 int i;
3381 for (i = 0; i < codec->spdif_out.used; i++) {
3382 struct hda_spdif_out *spdif =
3383 snd_array_elem(&codec->spdif_out, i);
3384 if (spdif->nid == nid)
3385 return spdif;
3386 }
3387 return NULL;
3388}
3389EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
3390
Stephen Warren74b654c2011-06-01 11:14:18 -06003391void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3392{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003393 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003394
3395 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003396 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003397 spdif->nid = (u16)-1;
3398 mutex_unlock(&codec->spdif_mutex);
3399}
3400EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
3401
3402void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3403{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003404 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003405 unsigned short val;
3406
3407 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003408 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003409 if (spdif->nid != nid) {
3410 spdif->nid = nid;
3411 val = spdif->ctls;
3412 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3413 }
3414 mutex_unlock(&codec->spdif_mutex);
3415}
3416EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
3417
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003419 * SPDIF sharing with analog output
3420 */
3421static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3422 struct snd_ctl_elem_value *ucontrol)
3423{
3424 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3425 ucontrol->value.integer.value[0] = mout->share_spdif;
3426 return 0;
3427}
3428
3429static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3430 struct snd_ctl_elem_value *ucontrol)
3431{
3432 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3433 mout->share_spdif = !!ucontrol->value.integer.value[0];
3434 return 0;
3435}
3436
3437static struct snd_kcontrol_new spdif_share_sw = {
3438 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3439 .name = "IEC958 Default PCM Playback Switch",
3440 .info = snd_ctl_boolean_mono_info,
3441 .get = spdif_share_sw_get,
3442 .put = spdif_share_sw_put,
3443};
3444
Takashi Iwaid5191e52009-11-16 14:58:17 +01003445/**
3446 * snd_hda_create_spdif_share_sw - create Default PCM switch
3447 * @codec: the HDA codec
3448 * @mout: multi-out instance
3449 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003450int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3451 struct hda_multi_out *mout)
3452{
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003453 struct snd_kcontrol *kctl;
3454
Takashi Iwai9a081602008-02-12 18:37:26 +01003455 if (!mout->dig_out_nid)
3456 return 0;
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003457
3458 kctl = snd_ctl_new1(&spdif_share_sw, mout);
3459 if (!kctl)
3460 return -ENOMEM;
Takashi Iwai9a081602008-02-12 18:37:26 +01003461 /* ATTENTION: here mout is passed as private_data, instead of codec */
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003462 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
Takashi Iwai9a081602008-02-12 18:37:26 +01003463}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003464EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003465
3466/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 * SPDIF input
3468 */
3469
3470#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3471
Takashi Iwai0ba21762007-04-16 11:29:14 +02003472static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3473 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474{
3475 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3476
3477 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3478 return 0;
3479}
3480
Takashi Iwai0ba21762007-04-16 11:29:14 +02003481static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3482 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483{
3484 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3485 hda_nid_t nid = kcontrol->private_value;
3486 unsigned int val = !!ucontrol->value.integer.value[0];
3487 int change;
3488
Ingo Molnar62932df2006-01-16 16:34:20 +01003489 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003491 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003493 snd_hda_codec_write_cache(codec, nid, 0,
3494 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003496 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 return change;
3498}
3499
Takashi Iwai0ba21762007-04-16 11:29:14 +02003500static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3501 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502{
3503 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3504 hda_nid_t nid = kcontrol->private_value;
3505 unsigned short val;
3506 unsigned int sbits;
3507
Andrew Paprocki3982d172007-12-19 12:13:44 +01003508 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 sbits = convert_to_spdif_status(val);
3510 ucontrol->value.iec958.status[0] = sbits;
3511 ucontrol->value.iec958.status[1] = sbits >> 8;
3512 ucontrol->value.iec958.status[2] = sbits >> 16;
3513 ucontrol->value.iec958.status[3] = sbits >> 24;
3514 return 0;
3515}
3516
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003517static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 {
3519 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003520 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 .info = snd_hda_spdif_in_switch_info,
3522 .get = snd_hda_spdif_in_switch_get,
3523 .put = snd_hda_spdif_in_switch_put,
3524 },
3525 {
3526 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3527 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003528 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 .info = snd_hda_spdif_mask_info,
3530 .get = snd_hda_spdif_in_status_get,
3531 },
3532 { } /* end */
3533};
3534
3535/**
3536 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3537 * @codec: the HDA codec
3538 * @nid: audio in widget NID
3539 *
3540 * Creates controls related with the SPDIF input.
3541 * Called from each patch supporting the SPDIF in.
3542 *
3543 * Returns 0 if successful, or a negative error code.
3544 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003545int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546{
3547 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003548 struct snd_kcontrol *kctl;
3549 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003550 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551
Takashi Iwaidcda5802012-10-12 17:24:51 +02003552 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003553 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003554 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3555 return -EBUSY;
3556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3558 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003559 if (!kctl)
3560 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003562 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003563 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 return err;
3565 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003566 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003567 snd_hda_codec_read(codec, nid, 0,
3568 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003569 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 return 0;
3571}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003572EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003574/*
3575 * command cache
3576 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577
Takashi Iwaic370dd62012-12-13 18:30:04 +01003578/* build a 31bit cache key with the widget id and the command parameter */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003579#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3580#define get_cmd_cache_nid(key) ((key) & 0xff)
3581#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3582
3583/**
3584 * snd_hda_codec_write_cache - send a single command with caching
3585 * @codec: the HDA codec
3586 * @nid: NID to send the command
3587 * @direct: direct flag
3588 * @verb: the verb to send
3589 * @parm: the parameter for the verb
3590 *
3591 * Send a single command without waiting for response.
3592 *
3593 * Returns 0 if successful, or a negative error code.
3594 */
3595int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3596 int direct, unsigned int verb, unsigned int parm)
3597{
Takashi Iwaic370dd62012-12-13 18:30:04 +01003598 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003599 struct hda_cache_head *c;
3600 u32 key;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003601 unsigned int cache_only;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003602
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003603 cache_only = codec->cached_write;
3604 if (!cache_only) {
Takashi Iwaic370dd62012-12-13 18:30:04 +01003605 err = snd_hda_codec_write(codec, nid, direct, verb, parm);
3606 if (err < 0)
3607 return err;
3608 }
3609
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003610 /* parm may contain the verb stuff for get/set amp */
3611 verb = verb | (parm >> 8);
3612 parm &= 0xff;
3613 key = build_cmd_cache_key(nid, verb);
3614 mutex_lock(&codec->bus->cmd_mutex);
3615 c = get_alloc_hash(&codec->cmd_cache, key);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003616 if (c) {
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003617 c->val = parm;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003618 c->dirty = cache_only;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003619 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003620 mutex_unlock(&codec->bus->cmd_mutex);
3621 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003622}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003623EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003624
Takashi Iwaid5191e52009-11-16 14:58:17 +01003625/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003626 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3627 * @codec: the HDA codec
3628 * @nid: NID to send the command
3629 * @direct: direct flag
3630 * @verb: the verb to send
3631 * @parm: the parameter for the verb
3632 *
3633 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3634 * command if the parameter is already identical with the cached value.
3635 * If not, it sends the command and refreshes the cache.
3636 *
3637 * Returns 0 if successful, or a negative error code.
3638 */
3639int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3640 int direct, unsigned int verb, unsigned int parm)
3641{
3642 struct hda_cache_head *c;
3643 u32 key;
3644
3645 /* parm may contain the verb stuff for get/set amp */
3646 verb = verb | (parm >> 8);
3647 parm &= 0xff;
3648 key = build_cmd_cache_key(nid, verb);
3649 mutex_lock(&codec->bus->cmd_mutex);
3650 c = get_hash(&codec->cmd_cache, key);
3651 if (c && c->val == parm) {
3652 mutex_unlock(&codec->bus->cmd_mutex);
3653 return 0;
3654 }
3655 mutex_unlock(&codec->bus->cmd_mutex);
3656 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3657}
3658EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3659
3660/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003661 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3662 * @codec: HD-audio codec
3663 *
3664 * Execute all verbs recorded in the command caches to resume.
3665 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003666void snd_hda_codec_resume_cache(struct hda_codec *codec)
3667{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003668 int i;
3669
Takashi Iwaic370dd62012-12-13 18:30:04 +01003670 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01003671 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003672 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3673 struct hda_cache_head *buffer;
3674 u32 key;
3675
3676 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3677 key = buffer->key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003678 if (!key)
3679 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003680 if (!buffer->dirty)
3681 continue;
3682 buffer->dirty = 0;
3683 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003684 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3685 get_cmd_cache_cmd(key), buffer->val);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003686 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003687 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01003688 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003689}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003690EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003691
3692/**
3693 * snd_hda_sequence_write_cache - sequence writes with caching
3694 * @codec: the HDA codec
3695 * @seq: VERB array to send
3696 *
3697 * Send the commands sequentially from the given array.
3698 * Thte commands are recorded on cache for power-save and resume.
3699 * The array must be terminated with NID=0.
3700 */
3701void snd_hda_sequence_write_cache(struct hda_codec *codec,
3702 const struct hda_verb *seq)
3703{
3704 for (; seq->nid; seq++)
3705 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3706 seq->param);
3707}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003708EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003709
Takashi Iwaidc870f32013-01-22 15:24:30 +01003710/**
3711 * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
3712 * @codec: HD-audio codec
3713 */
3714void snd_hda_codec_flush_cache(struct hda_codec *codec)
3715{
3716 snd_hda_codec_resume_amp(codec);
3717 snd_hda_codec_resume_cache(codec);
3718}
3719EXPORT_SYMBOL_HDA(snd_hda_codec_flush_cache);
3720
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003721void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003722 unsigned int power_state)
Takashi Iwai54d17402005-11-21 16:33:22 +01003723{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003724 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003725 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003726
Takashi Iwaicb53c622007-08-10 17:21:45 +02003727 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003728 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003729 unsigned int state = power_state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003730 if (!(wcaps & AC_WCAP_POWER))
3731 continue;
Takashi Iwai9419ab62013-01-24 17:23:35 +01003732 if (codec->power_filter) {
3733 state = codec->power_filter(codec, nid, power_state);
3734 if (state != power_state && power_state == AC_PWRST_D3)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003735 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003736 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003737 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003738 state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003739 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003740}
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003741EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3742
3743/*
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003744 * supported power states check
3745 */
3746static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
3747 unsigned int power_state)
3748{
3749 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
3750
Mengdong Line037cb42012-08-10 14:11:58 +02003751 if (sup == -1)
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003752 return false;
3753 if (sup & power_state)
3754 return true;
3755 else
3756 return false;
3757}
3758
3759/*
Takashi Iwai432c6412012-08-28 09:59:20 -07003760 * wait until the state is reached, returns the current state
3761 */
3762static unsigned int hda_sync_power_state(struct hda_codec *codec,
3763 hda_nid_t fg,
3764 unsigned int power_state)
3765{
3766 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3767 unsigned int state, actual_state;
3768
3769 for (;;) {
3770 state = snd_hda_codec_read(codec, fg, 0,
3771 AC_VERB_GET_POWER_STATE, 0);
3772 if (state & AC_PWRST_ERROR)
3773 break;
3774 actual_state = (state >> 4) & 0x0f;
3775 if (actual_state == power_state)
3776 break;
3777 if (time_after_eq(jiffies, end_time))
3778 break;
3779 /* wait until the codec reachs to the target state */
3780 msleep(1);
3781 }
3782 return state;
3783}
3784
Takashi Iwai9419ab62013-01-24 17:23:35 +01003785/* don't power down the widget if it controls eapd and EAPD_BTLENABLE is set */
Takashi Iwaiba615b82013-03-13 14:47:21 +01003786unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
3787 hda_nid_t nid,
3788 unsigned int power_state)
Takashi Iwai9419ab62013-01-24 17:23:35 +01003789{
3790 if (power_state == AC_PWRST_D3 &&
3791 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
3792 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3793 int eapd = snd_hda_codec_read(codec, nid, 0,
3794 AC_VERB_GET_EAPD_BTLENABLE, 0);
3795 if (eapd & 0x02)
3796 return AC_PWRST_D0;
3797 }
3798 return power_state;
3799}
Takashi Iwaiba615b82013-03-13 14:47:21 +01003800EXPORT_SYMBOL_HDA(snd_hda_codec_eapd_power_filter);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003801
Takashi Iwai432c6412012-08-28 09:59:20 -07003802/*
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003803 * set power state of the codec, and return the power state
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003804 */
Takashi Iwaid8193872012-08-31 07:54:38 -07003805static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003806 unsigned int power_state)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003807{
Takashi Iwaid8193872012-08-31 07:54:38 -07003808 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003809 int count;
3810 unsigned int state;
3811
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003812 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003813 if (power_state == AC_PWRST_D3) {
3814 /* transition time less than 10ms for power down */
Takashi Iwai983f6b92012-08-28 09:18:01 -07003815 msleep(codec->epss ? 10 : 100);
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003816 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08003817
3818 /* repeat power states setting at most 10 times*/
3819 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07003820 if (codec->patch_ops.set_power_state)
3821 codec->patch_ops.set_power_state(codec, fg,
3822 power_state);
3823 else {
3824 snd_hda_codec_read(codec, fg, 0,
3825 AC_VERB_SET_POWER_STATE,
3826 power_state);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003827 snd_hda_codec_set_power_to_all(codec, fg, power_state);
Takashi Iwai432c6412012-08-28 09:59:20 -07003828 }
3829 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08003830 if (!(state & AC_PWRST_ERROR))
3831 break;
3832 }
Mengdong Linb8dfc4622012-08-23 17:32:30 +08003833
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003834 return state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003835}
Takashi Iwai54d17402005-11-21 16:33:22 +01003836
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003837/* sync power states of all widgets;
3838 * this is called at the end of codec parsing
3839 */
3840static void sync_power_up_states(struct hda_codec *codec)
3841{
3842 hda_nid_t nid = codec->start_nid;
3843 int i;
3844
Takashi Iwaiba615b82013-03-13 14:47:21 +01003845 /* don't care if no filter is used */
3846 if (!codec->power_filter)
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003847 return;
3848
3849 for (i = 0; i < codec->num_nodes; i++, nid++) {
3850 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9040d102013-01-24 17:47:17 +01003851 unsigned int target;
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003852 if (!(wcaps & AC_WCAP_POWER))
3853 continue;
3854 target = codec->power_filter(codec, nid, AC_PWRST_D0);
3855 if (target == AC_PWRST_D0)
3856 continue;
Takashi Iwai9040d102013-01-24 17:47:17 +01003857 if (!snd_hda_check_power_state(codec, nid, target))
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003858 snd_hda_codec_write(codec, nid, 0,
3859 AC_VERB_SET_POWER_STATE, target);
3860 }
3861}
3862
Takashi Iwai11aeff02008-07-30 15:01:46 +02003863#ifdef CONFIG_SND_HDA_HWDEP
3864/* execute additional init verbs */
3865static void hda_exec_init_verbs(struct hda_codec *codec)
3866{
3867 if (codec->init_verbs.list)
3868 snd_hda_sequence_write(codec, codec->init_verbs.list);
3869}
3870#else
3871static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3872#endif
3873
Takashi Iwai2a439522011-07-26 09:52:50 +02003874#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003875/*
3876 * call suspend and power-down; used both from PM and power-save
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003877 * this function returns the power state in the end
Takashi Iwaicb53c622007-08-10 17:21:45 +02003878 */
Dylan Reidd17344b2012-09-28 15:57:01 -07003879static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003880{
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003881 unsigned int state;
3882
Takashi Iwai989c3182012-11-19 14:14:58 +01003883 codec->in_pm = 1;
3884
Takashi Iwaicb53c622007-08-10 17:21:45 +02003885 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02003886 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02003887 hda_cleanup_all_streams(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003888 state = hda_set_power_state(codec, AC_PWRST_D3);
Dylan Reidd17344b2012-09-28 15:57:01 -07003889 /* Cancel delayed work if we aren't currently running from it. */
3890 if (!in_wq)
3891 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003892 spin_lock(&codec->power_lock);
3893 snd_hda_update_power_acct(codec);
3894 trace_hda_power_down(codec);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003895 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003896 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003897 codec->power_jiffies = jiffies;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003898 spin_unlock(&codec->power_lock);
Takashi Iwai989c3182012-11-19 14:14:58 +01003899 codec->in_pm = 0;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003900 return state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003901}
3902
Takashi Iwaic370dd62012-12-13 18:30:04 +01003903/* mark all entries of cmd and amp caches dirty */
3904static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
3905{
3906 int i;
3907 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3908 struct hda_cache_head *cmd;
3909 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
3910 cmd->dirty = 1;
3911 }
3912 for (i = 0; i < codec->amp_cache.buf.used; i++) {
3913 struct hda_amp_info *amp;
David Henningssonf038fca2013-01-15 15:27:19 +01003914 amp = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003915 amp->head.dirty = 1;
3916 }
3917}
3918
Takashi Iwaicb53c622007-08-10 17:21:45 +02003919/*
3920 * kick up codec; used both from PM and power-save
3921 */
3922static void hda_call_codec_resume(struct hda_codec *codec)
3923{
Takashi Iwai989c3182012-11-19 14:14:58 +01003924 codec->in_pm = 1;
3925
Takashi Iwaic370dd62012-12-13 18:30:04 +01003926 hda_mark_cmd_cache_dirty(codec);
3927
Takashi Iwai7f308302012-05-08 16:52:23 +02003928 /* set as if powered on for avoiding re-entering the resume
3929 * in the resume / power-save sequence
3930 */
3931 hda_keep_power_on(codec);
Takashi Iwaic3bd14e2013-11-20 12:15:07 +01003932 if (codec->pm_down_notified) {
3933 codec->pm_down_notified = 0;
3934 hda_call_pm_notify(codec->bus, true);
3935 }
Takashi Iwaid8193872012-08-31 07:54:38 -07003936 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003937 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003938 hda_exec_init_verbs(codec);
Takashi Iwai31614bb2013-01-23 15:58:40 +01003939 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003940 if (codec->patch_ops.resume)
3941 codec->patch_ops.resume(codec);
3942 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003943 if (codec->patch_ops.init)
3944 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003945 snd_hda_codec_resume_amp(codec);
3946 snd_hda_codec_resume_cache(codec);
3947 }
David Henningsson26a6cb62012-10-09 15:04:21 +02003948
3949 if (codec->jackpoll_interval)
3950 hda_jackpoll_work(&codec->jackpoll_work.work);
Takashi Iwai31614bb2013-01-23 15:58:40 +01003951 else
David Henningsson26a6cb62012-10-09 15:04:21 +02003952 snd_hda_jack_report_sync(codec);
Takashi Iwai989c3182012-11-19 14:14:58 +01003953
3954 codec->in_pm = 0;
Takashi Iwai7f308302012-05-08 16:52:23 +02003955 snd_hda_power_down(codec); /* flag down before returning */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003956}
Takashi Iwai2a439522011-07-26 09:52:50 +02003957#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003958
Takashi Iwai54d17402005-11-21 16:33:22 +01003959
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960/**
3961 * snd_hda_build_controls - build mixer controls
3962 * @bus: the BUS
3963 *
3964 * Creates mixer controls for each codec included in the bus.
3965 *
3966 * Returns 0 if successful, otherwise a negative error code.
3967 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01003968int snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003970 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971
Takashi Iwai0ba21762007-04-16 11:29:14 +02003972 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003973 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003974 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01003975 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003976 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003977 err = snd_hda_codec_reset(codec);
3978 if (err < 0) {
3979 printk(KERN_ERR
3980 "hda_codec: cannot revert codec\n");
3981 return err;
3982 }
3983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003985 return 0;
3986}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003987EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003989/*
3990 * add standard channel maps if not specified
3991 */
3992static int add_std_chmaps(struct hda_codec *codec)
3993{
3994 int i, str, err;
3995
3996 for (i = 0; i < codec->num_pcms; i++) {
3997 for (str = 0; str < 2; str++) {
3998 struct snd_pcm *pcm = codec->pcm_info[i].pcm;
3999 struct hda_pcm_stream *hinfo =
4000 &codec->pcm_info[i].stream[str];
4001 struct snd_pcm_chmap *chmap;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004002 const struct snd_pcm_chmap_elem *elem;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004003
4004 if (codec->pcm_info[i].own_chmap)
4005 continue;
4006 if (!pcm || !hinfo->substreams)
4007 continue;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004008 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
4009 err = snd_pcm_add_chmap_ctls(pcm, str, elem,
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004010 hinfo->channels_max,
4011 0, &chmap);
4012 if (err < 0)
4013 return err;
4014 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
4015 }
4016 }
4017 return 0;
4018}
4019
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004020/* default channel maps for 2.1 speakers;
4021 * since HD-audio supports only stereo, odd number channels are omitted
4022 */
4023const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
4024 { .channels = 2,
4025 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
4026 { .channels = 4,
4027 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
4028 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
4029 { }
4030};
4031EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
4032
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004033int snd_hda_codec_build_controls(struct hda_codec *codec)
4034{
4035 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02004036 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004037 /* continue to initialize... */
4038 if (codec->patch_ops.init)
4039 err = codec->patch_ops.init(codec);
4040 if (!err && codec->patch_ops.build_controls)
4041 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004042 if (err < 0)
4043 return err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004044
4045 /* we create chmaps here instead of build_pcms */
4046 err = add_std_chmaps(codec);
4047 if (err < 0)
4048 return err;
4049
David Henningsson26a6cb62012-10-09 15:04:21 +02004050 if (codec->jackpoll_interval)
4051 hda_jackpoll_work(&codec->jackpoll_work.work);
4052 else
4053 snd_hda_jack_report_sync(codec); /* call at the last init point */
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004054 sync_power_up_states(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 return 0;
4056}
4057
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058/*
4059 * stream formats
4060 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02004061struct hda_rate_tbl {
4062 unsigned int hz;
4063 unsigned int alsa_bits;
4064 unsigned int hda_fmt;
4065};
4066
Takashi Iwai92f10b32010-08-03 14:21:00 +02004067/* rate = base * mult / div */
4068#define HDA_RATE(base, mult, div) \
4069 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
4070 (((div) - 1) << AC_FMT_DIV_SHIFT))
4071
Takashi Iwaibefdf312005-08-22 13:57:55 +02004072static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004074
4075 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004076 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
4077 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
4078 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
4079 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
4080 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
4081 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
4082 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
4083 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
4084 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
4085 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
4086 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004087#define AC_PAR_PCM_RATE_BITS 11
4088 /* up to bits 10, 384kHZ isn't supported properly */
4089
4090 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004091 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004092
Takashi Iwaibefdf312005-08-22 13:57:55 +02004093 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094};
4095
4096/**
4097 * snd_hda_calc_stream_format - calculate format bitset
4098 * @rate: the sample rate
4099 * @channels: the number of channels
4100 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
4101 * @maxbps: the max. bps
4102 *
4103 * Calculate the format bitset from the given rate, channels and th PCM format.
4104 *
4105 * Return zero if invalid.
4106 */
4107unsigned int snd_hda_calc_stream_format(unsigned int rate,
4108 unsigned int channels,
4109 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03004110 unsigned int maxbps,
4111 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112{
4113 int i;
4114 unsigned int val = 0;
4115
Takashi Iwaibefdf312005-08-22 13:57:55 +02004116 for (i = 0; rate_bits[i].hz; i++)
4117 if (rate_bits[i].hz == rate) {
4118 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 break;
4120 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02004121 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 snd_printdd("invalid rate %d\n", rate);
4123 return 0;
4124 }
4125
4126 if (channels == 0 || channels > 8) {
4127 snd_printdd("invalid channels %d\n", channels);
4128 return 0;
4129 }
4130 val |= channels - 1;
4131
4132 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004133 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004134 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004135 break;
4136 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004137 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004138 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 case 20:
4140 case 24:
4141 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004142 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004143 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004145 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02004147 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 break;
4149 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004150 snd_printdd("invalid format width %d\n",
4151 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 return 0;
4153 }
4154
Anssi Hannula32c168c2010-08-03 13:28:57 +03004155 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004156 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03004157
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 return val;
4159}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004160EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004162static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
4163 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004164{
4165 unsigned int val = 0;
4166 if (nid != codec->afg &&
4167 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
4168 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
4169 if (!val || val == -1)
4170 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
4171 if (!val || val == -1)
4172 return 0;
4173 return val;
4174}
4175
4176static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
4177{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004178 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004179 get_pcm_param);
4180}
4181
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004182static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
4183 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004184{
4185 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
4186 if (!streams || streams == -1)
4187 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
4188 if (!streams || streams == -1)
4189 return 0;
4190 return streams;
4191}
4192
4193static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
4194{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004195 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004196 get_stream_param);
4197}
4198
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199/**
4200 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4201 * @codec: the HDA codec
4202 * @nid: NID to query
4203 * @ratesp: the pointer to store the detected rate bitflags
4204 * @formatsp: the pointer to store the detected formats
4205 * @bpsp: the pointer to store the detected format widths
4206 *
4207 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4208 * or @bsps argument is ignored.
4209 *
4210 * Returns 0 if successful, otherwise a negative error code.
4211 */
Stephen Warren384a48d2011-06-01 11:14:21 -06004212int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
4214{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004215 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004217 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004218 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219
4220 if (ratesp) {
4221 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004222 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02004224 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004226 if (rates == 0) {
4227 snd_printk(KERN_ERR "hda_codec: rates == 0 "
4228 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
4229 nid, val,
4230 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
4231 return -EIO;
4232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 *ratesp = rates;
4234 }
4235
4236 if (formatsp || bpsp) {
4237 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004238 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004240 streams = query_stream_param(codec, nid);
4241 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243
4244 bps = 0;
4245 if (streams & AC_SUPFMT_PCM) {
4246 if (val & AC_SUPPCM_BITS_8) {
4247 formats |= SNDRV_PCM_FMTBIT_U8;
4248 bps = 8;
4249 }
4250 if (val & AC_SUPPCM_BITS_16) {
4251 formats |= SNDRV_PCM_FMTBIT_S16_LE;
4252 bps = 16;
4253 }
4254 if (wcaps & AC_WCAP_DIGITAL) {
4255 if (val & AC_SUPPCM_BITS_32)
4256 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4257 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4258 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4259 if (val & AC_SUPPCM_BITS_24)
4260 bps = 24;
4261 else if (val & AC_SUPPCM_BITS_20)
4262 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004263 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4264 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4266 if (val & AC_SUPPCM_BITS_32)
4267 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 else if (val & AC_SUPPCM_BITS_24)
4269 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02004270 else if (val & AC_SUPPCM_BITS_20)
4271 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 }
4273 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004274#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02004275 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004277 if (!bps)
4278 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02004279 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004280#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02004281 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02004282 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 /* temporary hack: we have still no proper support
4284 * for the direct AC3 stream...
4285 */
4286 formats |= SNDRV_PCM_FMTBIT_U8;
4287 bps = 8;
4288 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004289 if (formats == 0) {
4290 snd_printk(KERN_ERR "hda_codec: formats == 0 "
4291 "(nid=0x%x, val=0x%x, ovrd=%i, "
4292 "streams=0x%x)\n",
4293 nid, val,
4294 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4295 streams);
4296 return -EIO;
4297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 if (formatsp)
4299 *formatsp = formats;
4300 if (bpsp)
4301 *bpsp = bps;
4302 }
4303
4304 return 0;
4305}
Stephen Warren384a48d2011-06-01 11:14:21 -06004306EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307
4308/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004309 * snd_hda_is_supported_format - Check the validity of the format
4310 * @codec: HD-audio codec
4311 * @nid: NID to check
4312 * @format: the HD-audio format value to check
4313 *
4314 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 *
4316 * Returns 1 if supported, 0 if not.
4317 */
4318int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4319 unsigned int format)
4320{
4321 int i;
4322 unsigned int val = 0, rate, stream;
4323
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004324 val = query_pcm_param(codec, nid);
4325 if (!val)
4326 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327
4328 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004329 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02004330 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 if (val & (1 << i))
4332 break;
4333 return 0;
4334 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004335 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 return 0;
4337
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004338 stream = query_stream_param(codec, nid);
4339 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 return 0;
4341
4342 if (stream & AC_SUPFMT_PCM) {
4343 switch (format & 0xf0) {
4344 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004345 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346 return 0;
4347 break;
4348 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004349 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 return 0;
4351 break;
4352 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004353 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 return 0;
4355 break;
4356 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004357 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358 return 0;
4359 break;
4360 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004361 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 return 0;
4363 break;
4364 default:
4365 return 0;
4366 }
4367 } else {
4368 /* FIXME: check for float32 and AC3? */
4369 }
4370
4371 return 1;
4372}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004373EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374
4375/*
4376 * PCM stuff
4377 */
4378static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4379 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004380 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381{
4382 return 0;
4383}
4384
4385static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4386 struct hda_codec *codec,
4387 unsigned int stream_tag,
4388 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004389 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390{
4391 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4392 return 0;
4393}
4394
4395static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4396 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004397 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398{
Takashi Iwai888afa12008-03-18 09:57:50 +01004399 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400 return 0;
4401}
4402
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004403static int set_pcm_default_values(struct hda_codec *codec,
4404 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004406 int err;
4407
Takashi Iwai0ba21762007-04-16 11:29:14 +02004408 /* query support PCM information from the given NID */
4409 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004410 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02004411 info->rates ? NULL : &info->rates,
4412 info->formats ? NULL : &info->formats,
4413 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004414 if (err < 0)
4415 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 }
4417 if (info->ops.open == NULL)
4418 info->ops.open = hda_pcm_default_open_close;
4419 if (info->ops.close == NULL)
4420 info->ops.close = hda_pcm_default_open_close;
4421 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004422 if (snd_BUG_ON(!info->nid))
4423 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424 info->ops.prepare = hda_pcm_default_prepare;
4425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004427 if (snd_BUG_ON(!info->nid))
4428 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 info->ops.cleanup = hda_pcm_default_cleanup;
4430 }
4431 return 0;
4432}
4433
Takashi Iwaieb541332010-08-06 13:48:11 +02004434/*
4435 * codec prepare/cleanup entries
4436 */
4437int snd_hda_codec_prepare(struct hda_codec *codec,
4438 struct hda_pcm_stream *hinfo,
4439 unsigned int stream,
4440 unsigned int format,
4441 struct snd_pcm_substream *substream)
4442{
4443 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004444 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004445 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4446 if (ret >= 0)
4447 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004448 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004449 return ret;
4450}
4451EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
4452
4453void snd_hda_codec_cleanup(struct hda_codec *codec,
4454 struct hda_pcm_stream *hinfo,
4455 struct snd_pcm_substream *substream)
4456{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004457 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004458 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004459 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004460}
4461EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
4462
Takashi Iwaid5191e52009-11-16 14:58:17 +01004463/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01004464const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4465 "Audio", "SPDIF", "HDMI", "Modem"
4466};
4467
Takashi Iwai176d5332008-07-30 15:01:44 +02004468/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004469 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01004470 *
4471 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004472 */
4473static int get_empty_pcm_device(struct hda_bus *bus, int type)
4474{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004475 /* audio device indices; not linear to keep compatibility */
4476 static int audio_idx[HDA_PCM_NTYPES][5] = {
4477 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4478 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01004479 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004480 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004481 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004482 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004483
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004484 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004485 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
4486 return -EINVAL;
4487 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004488
4489 for (i = 0; audio_idx[type][i] >= 0 ; i++)
4490 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4491 return audio_idx[type][i];
4492
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004493 /* non-fixed slots starting from 10 */
4494 for (i = 10; i < 32; i++) {
4495 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4496 return i;
4497 }
4498
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004499 snd_printk(KERN_WARNING "Too many %s devices\n",
4500 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004501 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004502}
4503
4504/*
Takashi Iwai176d5332008-07-30 15:01:44 +02004505 * attach a new PCM stream
4506 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004507static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02004508{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004509 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02004510 struct hda_pcm_stream *info;
4511 int stream, err;
4512
Takashi Iwaib91f0802008-11-04 08:43:08 +01004513 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02004514 return -EINVAL;
4515 for (stream = 0; stream < 2; stream++) {
4516 info = &pcm->stream[stream];
4517 if (info->substreams) {
4518 err = set_pcm_default_values(codec, info);
4519 if (err < 0)
4520 return err;
4521 }
4522 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004523 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02004524}
4525
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004526/* assign all PCMs of the given codec */
4527int snd_hda_codec_build_pcms(struct hda_codec *codec)
4528{
4529 unsigned int pcm;
4530 int err;
4531
4532 if (!codec->num_pcms) {
4533 if (!codec->patch_ops.build_pcms)
4534 return 0;
4535 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004536 if (err < 0) {
4537 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004538 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004539 err = snd_hda_codec_reset(codec);
4540 if (err < 0) {
4541 printk(KERN_ERR
4542 "hda_codec: cannot revert codec\n");
4543 return err;
4544 }
4545 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004546 }
4547 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4548 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4549 int dev;
4550
4551 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004552 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004553
4554 if (!cpcm->pcm) {
4555 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4556 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004557 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004558 cpcm->device = dev;
4559 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004560 if (err < 0) {
4561 printk(KERN_ERR "hda_codec: cannot attach "
4562 "PCM stream %d for codec #%d\n",
4563 dev, codec->addr);
4564 continue; /* no fatal error */
4565 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004566 }
4567 }
4568 return 0;
4569}
4570
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571/**
4572 * snd_hda_build_pcms - build PCM information
4573 * @bus: the BUS
4574 *
4575 * Create PCM information for each codec included in the bus.
4576 *
4577 * The build_pcms codec patch is requested to set up codec->num_pcms and
4578 * codec->pcm_info properly. The array is referred by the top-level driver
4579 * to create its PCM instances.
4580 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4581 * callback.
4582 *
4583 * At least, substreams, channels_min and channels_max must be filled for
4584 * each stream. substreams = 0 indicates that the stream doesn't exist.
4585 * When rates and/or formats are zero, the supported values are queried
4586 * from the given nid. The nid is used also by the default ops.prepare
4587 * and ops.cleanup callbacks.
4588 *
4589 * The driver needs to call ops.open in its open callback. Similarly,
4590 * ops.close is supposed to be called in the close callback.
4591 * ops.prepare should be called in the prepare or hw_params callback
4592 * with the proper parameters for set up.
4593 * ops.cleanup should be called in hw_free for clean up of streams.
4594 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004595 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 */
Takashi Iwai5cb543d2012-08-09 13:49:23 +02004597int snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004599 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600
Takashi Iwai0ba21762007-04-16 11:29:14 +02004601 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004602 int err = snd_hda_codec_build_pcms(codec);
4603 if (err < 0)
4604 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605 }
4606 return 0;
4607}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004608EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610/**
4611 * snd_hda_check_board_config - compare the current codec with the config table
4612 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004613 * @num_configs: number of config enums
4614 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 * @tbl: configuration table, terminated by null entries
4616 *
4617 * Compares the modelname or PCI subsystem id of the current codec with the
4618 * given configuration table. If a matching entry is found, returns its
4619 * config value (supposed to be 0 or positive).
4620 *
4621 * If no entries are matching, the function returns a negative value.
4622 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004623int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004624 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02004625 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626{
Takashi Iwaif44ac832008-07-30 15:01:45 +02004627 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004628 int i;
4629 for (i = 0; i < num_configs; i++) {
4630 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02004631 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004632 snd_printd(KERN_INFO "hda_codec: model '%s' is "
4633 "selected\n", models[i]);
4634 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635 }
4636 }
4637 }
4638
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004639 if (!codec->bus->pci || !tbl)
4640 return -1;
4641
4642 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
4643 if (!tbl)
4644 return -1;
4645 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02004646#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004647 char tmp[10];
4648 const char *model = NULL;
4649 if (models)
4650 model = models[tbl->value];
4651 if (!model) {
4652 sprintf(tmp, "#%d", tbl->value);
4653 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004655 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4656 "for config %x:%x (%s)\n",
4657 model, tbl->subvendor, tbl->subdevice,
4658 (tbl->name ? tbl->name : "Unknown device"));
4659#endif
4660 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 }
4662 return -1;
4663}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004664EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665
4666/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004667 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004668 subsystem ID with the
4669 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004670
4671 This is important for Gateway notebooks with SB450 HDA Audio
4672 where the vendor ID of the PCI device is:
4673 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4674 and the vendor/subvendor are found only at the codec.
4675
4676 * @codec: the HDA codec
4677 * @num_configs: number of config enums
4678 * @models: array of model name strings
4679 * @tbl: configuration table, terminated by null entries
4680 *
4681 * Compares the modelname or PCI subsystem id of the current codec with the
4682 * given configuration table. If a matching entry is found, returns its
4683 * config value (supposed to be 0 or positive).
4684 *
4685 * If no entries are matching, the function returns a negative value.
4686 */
4687int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004688 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004689 const struct snd_pci_quirk *tbl)
4690{
4691 const struct snd_pci_quirk *q;
4692
4693 /* Search for codec ID */
4694 for (q = tbl; q->subvendor; q++) {
Takashi Iwaie2301a42011-11-22 19:58:56 +01004695 unsigned int mask = 0xffff0000 | q->subdevice_mask;
4696 unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
4697 if ((codec->subsystem_id & mask) == id)
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004698 break;
4699 }
4700
4701 if (!q->subvendor)
4702 return -1;
4703
4704 tbl = q;
4705
4706 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02004707#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004708 char tmp[10];
4709 const char *model = NULL;
4710 if (models)
4711 model = models[tbl->value];
4712 if (!model) {
4713 sprintf(tmp, "#%d", tbl->value);
4714 model = tmp;
4715 }
4716 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4717 "for config %x:%x (%s)\n",
4718 model, tbl->subvendor, tbl->subdevice,
4719 (tbl->name ? tbl->name : "Unknown device"));
4720#endif
4721 return tbl->value;
4722 }
4723 return -1;
4724}
4725EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4726
4727/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728 * snd_hda_add_new_ctls - create controls from the array
4729 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004730 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731 *
4732 * This helper function creates and add new controls in the given array.
4733 * The array must be terminated with an empty entry as terminator.
4734 *
4735 * Returns 0 if successful, or a negative error code.
4736 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004737int snd_hda_add_new_ctls(struct hda_codec *codec,
4738 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004740 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741
4742 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004743 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004744 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004745 if (knew->iface == -1) /* skip this codec private value */
4746 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004747 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004748 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004749 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004750 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004751 if (addr > 0)
4752 kctl->id.device = addr;
4753 if (idx > 0)
4754 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004755 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004756 if (!err)
4757 break;
4758 /* try first with another device index corresponding to
4759 * the codec addr; if it still fails (or it's the
4760 * primary codec), then try another control index
4761 */
4762 if (!addr && codec->addr)
4763 addr = codec->addr;
4764 else if (!idx && !knew->index) {
4765 idx = find_empty_mixer_ctl_idx(codec,
Takashi Iwaidcda5802012-10-12 17:24:51 +02004766 knew->name, 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004767 if (idx <= 0)
4768 return err;
4769 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004770 return err;
4771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 }
4773 return 0;
4774}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004775EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776
Takashi Iwai83012a72012-08-24 18:38:08 +02004777#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02004778static void hda_power_work(struct work_struct *work)
4779{
4780 struct hda_codec *codec =
4781 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004782 struct hda_bus *bus = codec->bus;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004783 unsigned int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004784
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004785 spin_lock(&codec->power_lock);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004786 if (codec->power_transition > 0) { /* during power-up sequence? */
4787 spin_unlock(&codec->power_lock);
4788 return;
4789 }
Maxim Levitsky2e492462007-09-03 15:26:57 +02004790 if (!codec->power_on || codec->power_count) {
4791 codec->power_transition = 0;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004792 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004793 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02004794 }
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004795 spin_unlock(&codec->power_lock);
4796
Dylan Reidd17344b2012-09-28 15:57:01 -07004797 state = hda_call_codec_suspend(codec, true);
Takashi Iwaic9e32ff2013-10-24 01:20:24 +02004798 if (!codec->pm_down_notified &&
4799 !bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK)) {
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004800 codec->pm_down_notified = 1;
Takashi Iwai68467f52012-08-28 09:14:29 -07004801 hda_call_pm_notify(bus, false);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004802 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004803}
4804
4805static void hda_keep_power_on(struct hda_codec *codec)
4806{
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004807 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004808 codec->power_count++;
4809 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004810 codec->power_jiffies = jiffies;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004811 spin_unlock(&codec->power_lock);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004812}
4813
Takashi Iwaid5191e52009-11-16 14:58:17 +01004814/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01004815void snd_hda_update_power_acct(struct hda_codec *codec)
4816{
4817 unsigned long delta = jiffies - codec->power_jiffies;
4818 if (codec->power_on)
4819 codec->power_on_acct += delta;
4820 else
4821 codec->power_off_acct += delta;
4822 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004823}
4824
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004825/* Transition to powered up, if wait_power_down then wait for a pending
4826 * transition to D3 to complete. A pending D3 transition is indicated
4827 * with power_transition == -1. */
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004828/* call this with codec->power_lock held! */
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004829static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004830{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004831 struct hda_bus *bus = codec->bus;
4832
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004833 /* Return if power_on or transitioning to power_on, unless currently
4834 * powering down. */
4835 if ((codec->power_on || codec->power_transition > 0) &&
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004836 !(wait_power_down && codec->power_transition < 0))
Takashi Iwaicb53c622007-08-10 17:21:45 +02004837 return;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004838 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004839
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004840 cancel_delayed_work_sync(&codec->power_work);
4841
4842 spin_lock(&codec->power_lock);
Dylan Reidb43d2242012-06-21 21:51:22 -07004843 /* If the power down delayed work was cancelled above before starting,
4844 * then there is no need to go through power up here.
4845 */
4846 if (codec->power_on) {
Takashi Iwai535b6c52012-08-20 21:25:22 +02004847 if (codec->power_transition < 0)
4848 codec->power_transition = 0;
Dylan Reidb43d2242012-06-21 21:51:22 -07004849 return;
4850 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004851
Takashi Iwaid66fee52011-08-02 15:39:31 +02004852 trace_hda_power_up(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004853 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004854 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004855 codec->power_jiffies = jiffies;
Takashi Iwai7f308302012-05-08 16:52:23 +02004856 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004857 spin_unlock(&codec->power_lock);
4858
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004859 if (codec->pm_down_notified) {
4860 codec->pm_down_notified = 0;
Takashi Iwai68467f52012-08-28 09:14:29 -07004861 hda_call_pm_notify(bus, true);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004862 }
4863
Takashi Iwaicb53c622007-08-10 17:21:45 +02004864 hda_call_codec_resume(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004865
4866 spin_lock(&codec->power_lock);
Takashi Iwaia221e282007-08-16 16:35:33 +02004867 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004868}
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004869
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004870#define power_save(codec) \
4871 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004872
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004873/* Transition to powered down */
4874static void __snd_hda_power_down(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004875{
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004876 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004877 return;
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004878
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004879 if (power_save(codec)) {
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004880 codec->power_transition = -1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01004881 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004882 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02004883 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004884}
4885
4886/**
4887 * snd_hda_power_save - Power-up/down/sync the codec
4888 * @codec: HD-audio codec
4889 * @delta: the counter delta to change
4890 *
4891 * Change the power-up counter via @delta, and power up or down the hardware
4892 * appropriately. For the power-down, queue to the delayed action.
4893 * Passing zero to @delta means to synchronize the power state.
4894 */
4895void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
4896{
4897 spin_lock(&codec->power_lock);
4898 codec->power_count += delta;
4899 trace_hda_power_count(codec);
4900 if (delta > 0)
4901 __snd_hda_power_up(codec, d3wait);
4902 else
4903 __snd_hda_power_down(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004904 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004905}
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004906EXPORT_SYMBOL_HDA(snd_hda_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004907
Takashi Iwaid5191e52009-11-16 14:58:17 +01004908/**
4909 * snd_hda_check_amp_list_power - Check the amp list and update the power
4910 * @codec: HD-audio codec
4911 * @check: the object containing an AMP list and the status
4912 * @nid: NID to check / update
4913 *
4914 * Check whether the given NID is in the amp list. If it's in the list,
4915 * check the current AMP status, and update the the power-status according
4916 * to the mute status.
4917 *
4918 * This function is supposed to be set or called from the check_power_status
4919 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004920 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004921int snd_hda_check_amp_list_power(struct hda_codec *codec,
4922 struct hda_loopback_check *check,
4923 hda_nid_t nid)
4924{
Takashi Iwai031024e2011-05-02 11:29:30 +02004925 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004926 int ch, v;
4927
4928 if (!check->amplist)
4929 return 0;
4930 for (p = check->amplist; p->nid; p++) {
4931 if (p->nid == nid)
4932 break;
4933 }
4934 if (!p->nid)
4935 return 0; /* nothing changed */
4936
4937 for (p = check->amplist; p->nid; p++) {
4938 for (ch = 0; ch < 2; ch++) {
4939 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4940 p->idx);
4941 if (!(v & HDA_AMP_MUTE) && v > 0) {
4942 if (!check->power_on) {
4943 check->power_on = 1;
4944 snd_hda_power_up(codec);
4945 }
4946 return 1;
4947 }
4948 }
4949 }
4950 if (check->power_on) {
4951 check->power_on = 0;
4952 snd_hda_power_down(codec);
4953 }
4954 return 0;
4955}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004956EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004957#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004959/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004960 * Channel mode helper
4961 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004962
4963/**
4964 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4965 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004966int snd_hda_ch_mode_info(struct hda_codec *codec,
4967 struct snd_ctl_elem_info *uinfo,
4968 const struct hda_channel_mode *chmode,
4969 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004970{
4971 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4972 uinfo->count = 1;
4973 uinfo->value.enumerated.items = num_chmodes;
4974 if (uinfo->value.enumerated.item >= num_chmodes)
4975 uinfo->value.enumerated.item = num_chmodes - 1;
4976 sprintf(uinfo->value.enumerated.name, "%dch",
4977 chmode[uinfo->value.enumerated.item].channels);
4978 return 0;
4979}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004980EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004981
Takashi Iwaid5191e52009-11-16 14:58:17 +01004982/**
4983 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4984 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004985int snd_hda_ch_mode_get(struct hda_codec *codec,
4986 struct snd_ctl_elem_value *ucontrol,
4987 const struct hda_channel_mode *chmode,
4988 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004989 int max_channels)
4990{
4991 int i;
4992
4993 for (i = 0; i < num_chmodes; i++) {
4994 if (max_channels == chmode[i].channels) {
4995 ucontrol->value.enumerated.item[0] = i;
4996 break;
4997 }
4998 }
4999 return 0;
5000}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005001EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005002
Takashi Iwaid5191e52009-11-16 14:58:17 +01005003/**
5004 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
5005 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005006int snd_hda_ch_mode_put(struct hda_codec *codec,
5007 struct snd_ctl_elem_value *ucontrol,
5008 const struct hda_channel_mode *chmode,
5009 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005010 int *max_channelsp)
5011{
5012 unsigned int mode;
5013
5014 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01005015 if (mode >= num_chmodes)
5016 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005017 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005018 return 0;
5019 /* change the current channel setting */
5020 *max_channelsp = chmode[mode].channels;
5021 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005022 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005023 return 1;
5024}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005025EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005026
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027/*
5028 * input MUX helper
5029 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01005030
5031/**
5032 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
5033 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005034int snd_hda_input_mux_info(const struct hda_input_mux *imux,
5035 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036{
5037 unsigned int index;
5038
5039 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5040 uinfo->count = 1;
5041 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02005042 if (!imux->num_items)
5043 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044 index = uinfo->value.enumerated.item;
5045 if (index >= imux->num_items)
5046 index = imux->num_items - 1;
5047 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
5048 return 0;
5049}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005050EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051
Takashi Iwaid5191e52009-11-16 14:58:17 +01005052/**
5053 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
5054 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005055int snd_hda_input_mux_put(struct hda_codec *codec,
5056 const struct hda_input_mux *imux,
5057 struct snd_ctl_elem_value *ucontrol,
5058 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059 unsigned int *cur_val)
5060{
5061 unsigned int idx;
5062
Takashi Iwai5513b0c2007-10-09 11:58:41 +02005063 if (!imux->num_items)
5064 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065 idx = ucontrol->value.enumerated.item[0];
5066 if (idx >= imux->num_items)
5067 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005068 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005070 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
5071 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072 *cur_val = idx;
5073 return 1;
5074}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005075EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076
5077
5078/*
Takashi Iwaidda415d2012-11-30 18:34:38 +01005079 * process kcontrol info callback of a simple string enum array
5080 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
5081 */
5082int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
5083 struct snd_ctl_elem_info *uinfo,
5084 int num_items, const char * const *texts)
5085{
5086 static const char * const texts_default[] = {
5087 "Disabled", "Enabled"
5088 };
5089
5090 if (!texts || !num_items) {
5091 num_items = 2;
5092 texts = texts_default;
5093 }
5094
5095 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5096 uinfo->count = 1;
5097 uinfo->value.enumerated.items = num_items;
5098 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
5099 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
5100 strcpy(uinfo->value.enumerated.name,
5101 texts[uinfo->value.enumerated.item]);
5102 return 0;
5103}
5104EXPORT_SYMBOL_HDA(snd_hda_enum_helper_info);
5105
5106/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107 * Multi-channel / digital-out PCM helper functions
5108 */
5109
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005110/* setup SPDIF output stream */
5111static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
5112 unsigned int stream_tag, unsigned int format)
5113{
Laurence Darby3bef1c32012-11-03 17:00:06 +00005114 struct hda_spdif_out *spdif;
5115 unsigned int curr_fmt;
5116 bool reset;
Stephen Warren7c935972011-06-01 11:14:17 -06005117
Laurence Darby3bef1c32012-11-03 17:00:06 +00005118 spdif = snd_hda_spdif_out_of_nid(codec, nid);
5119 curr_fmt = snd_hda_codec_read(codec, nid, 0,
5120 AC_VERB_GET_STREAM_FORMAT, 0);
5121 reset = codec->spdif_status_reset &&
5122 (spdif->ctls & AC_DIG1_ENABLE) &&
5123 curr_fmt != format;
5124
5125 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
5126 updated */
5127 if (reset)
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005128 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06005129 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02005130 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005131 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02005132 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_setup_stream(codec, *d, stream_tag, 0,
5136 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005137 }
Takashi Iwai2f728532008-09-25 16:32:41 +02005138 /* turn on again (if needed) */
Laurence Darby3bef1c32012-11-03 17:00:06 +00005139 if (reset)
Takashi Iwai2f728532008-09-25 16:32:41 +02005140 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06005141 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02005142}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005143
Takashi Iwai2f728532008-09-25 16:32:41 +02005144static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
5145{
5146 snd_hda_codec_cleanup_stream(codec, nid);
5147 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02005148 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02005149 for (d = codec->slave_dig_outs; *d; d++)
5150 snd_hda_codec_cleanup_stream(codec, *d);
5151 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005152}
5153
Takashi Iwaid5191e52009-11-16 14:58:17 +01005154/**
5155 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
5156 * @bus: HD-audio bus
5157 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005158void snd_hda_bus_reboot_notify(struct hda_bus *bus)
5159{
5160 struct hda_codec *codec;
5161
5162 if (!bus)
5163 return;
5164 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005165 if (hda_codec_is_power_on(codec) &&
5166 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005167 codec->patch_ops.reboot_notify(codec);
5168 }
5169}
Takashi Iwai8f217a22009-11-10 18:26:12 +01005170EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005171
Takashi Iwaid5191e52009-11-16 14:58:17 +01005172/**
5173 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005175int snd_hda_multi_out_dig_open(struct hda_codec *codec,
5176 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177{
Ingo Molnar62932df2006-01-16 16:34:20 +01005178 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02005179 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
5180 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02005181 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01005183 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184 return 0;
5185}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005186EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187
Takashi Iwaid5191e52009-11-16 14:58:17 +01005188/**
5189 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
5190 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005191int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
5192 struct hda_multi_out *mout,
5193 unsigned int stream_tag,
5194 unsigned int format,
5195 struct snd_pcm_substream *substream)
5196{
5197 mutex_lock(&codec->spdif_mutex);
5198 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
5199 mutex_unlock(&codec->spdif_mutex);
5200 return 0;
5201}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005202EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005203
Takashi Iwaid5191e52009-11-16 14:58:17 +01005204/**
5205 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
5206 */
Takashi Iwai9411e212009-02-13 11:32:28 +01005207int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
5208 struct hda_multi_out *mout)
5209{
5210 mutex_lock(&codec->spdif_mutex);
5211 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5212 mutex_unlock(&codec->spdif_mutex);
5213 return 0;
5214}
5215EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
5216
Takashi Iwaid5191e52009-11-16 14:58:17 +01005217/**
5218 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005220int snd_hda_multi_out_dig_close(struct hda_codec *codec,
5221 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222{
Ingo Molnar62932df2006-01-16 16:34:20 +01005223 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01005225 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226 return 0;
5227}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005228EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229
Takashi Iwaid5191e52009-11-16 14:58:17 +01005230/**
5231 * snd_hda_multi_out_analog_open - open analog outputs
5232 *
5233 * Open analog outputs and set up the hw-constraints.
5234 * If the digital outputs can be opened as slave, open the digital
5235 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005237int snd_hda_multi_out_analog_open(struct hda_codec *codec,
5238 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01005239 struct snd_pcm_substream *substream,
5240 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005241{
Takashi Iwai9a081602008-02-12 18:37:26 +01005242 struct snd_pcm_runtime *runtime = substream->runtime;
5243 runtime->hw.channels_max = mout->max_channels;
5244 if (mout->dig_out_nid) {
5245 if (!mout->analog_rates) {
5246 mout->analog_rates = hinfo->rates;
5247 mout->analog_formats = hinfo->formats;
5248 mout->analog_maxbps = hinfo->maxbps;
5249 } else {
5250 runtime->hw.rates = mout->analog_rates;
5251 runtime->hw.formats = mout->analog_formats;
5252 hinfo->maxbps = mout->analog_maxbps;
5253 }
5254 if (!mout->spdif_rates) {
5255 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
5256 &mout->spdif_rates,
5257 &mout->spdif_formats,
5258 &mout->spdif_maxbps);
5259 }
5260 mutex_lock(&codec->spdif_mutex);
5261 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02005262 if ((runtime->hw.rates & mout->spdif_rates) &&
5263 (runtime->hw.formats & mout->spdif_formats)) {
5264 runtime->hw.rates &= mout->spdif_rates;
5265 runtime->hw.formats &= mout->spdif_formats;
5266 if (mout->spdif_maxbps < hinfo->maxbps)
5267 hinfo->maxbps = mout->spdif_maxbps;
5268 } else {
5269 mout->share_spdif = 0;
5270 /* FIXME: need notify? */
5271 }
Takashi Iwai9a081602008-02-12 18:37:26 +01005272 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02005273 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01005274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005275 return snd_pcm_hw_constraint_step(substream->runtime, 0,
5276 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
5277}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005278EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279
Takashi Iwaid5191e52009-11-16 14:58:17 +01005280/**
5281 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
5282 *
5283 * Set up the i/o for analog out.
5284 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005286int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5287 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005288 unsigned int stream_tag,
5289 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01005290 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291{
Takashi Iwaidda14412011-05-02 11:29:30 +02005292 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005294 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295 int i;
5296
Ingo Molnar62932df2006-01-16 16:34:20 +01005297 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005298 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01005299 if (mout->dig_out_nid && mout->share_spdif &&
5300 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02005302 snd_hda_is_supported_format(codec, mout->dig_out_nid,
5303 format) &&
Stephen Warren7c935972011-06-01 11:14:17 -06005304 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005306 setup_dig_out_stream(codec, mout->dig_out_nid,
5307 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308 } else {
5309 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02005310 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311 }
5312 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005313 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314
5315 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005316 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5317 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005318 if (!mout->no_share_stream &&
5319 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005321 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5322 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005323 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005324 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5325 if (!mout->no_share_stream && mout->hp_out_nid[i])
5326 snd_hda_codec_setup_stream(codec,
5327 mout->hp_out_nid[i],
5328 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005329 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02005330 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01005331 snd_hda_codec_setup_stream(codec,
5332 mout->extra_out_nid[i],
5333 stream_tag, 0, format);
5334
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335 /* surrounds */
5336 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02005337 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005338 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5339 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005340 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005341 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5342 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343 }
5344 return 0;
5345}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005346EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347
Takashi Iwaid5191e52009-11-16 14:58:17 +01005348/**
5349 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005351int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5352 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353{
Takashi Iwaidda14412011-05-02 11:29:30 +02005354 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355 int i;
5356
5357 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01005358 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01005360 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005361 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5362 if (mout->hp_out_nid[i])
5363 snd_hda_codec_cleanup_stream(codec,
5364 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005365 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5366 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01005367 snd_hda_codec_cleanup_stream(codec,
5368 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01005369 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02005371 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372 mout->dig_out_used = 0;
5373 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005374 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375 return 0;
5376}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005377EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378
Takashi Iwai47408602012-04-20 13:06:53 +02005379/**
5380 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
5381 *
5382 * Guess the suitable VREF pin bits to be set as the pin-control value.
5383 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5384 */
5385unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5386{
5387 unsigned int pincap;
5388 unsigned int oldval;
5389 oldval = snd_hda_codec_read(codec, pin, 0,
5390 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5391 pincap = snd_hda_query_pin_caps(codec, pin);
5392 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5393 /* Exception: if the default pin setup is vref50, we give it priority */
5394 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5395 return AC_PINCTL_VREF_80;
5396 else if (pincap & AC_PINCAP_VREF_50)
5397 return AC_PINCTL_VREF_50;
5398 else if (pincap & AC_PINCAP_VREF_100)
5399 return AC_PINCTL_VREF_100;
5400 else if (pincap & AC_PINCAP_VREF_GRD)
5401 return AC_PINCTL_VREF_GRD;
5402 return AC_PINCTL_VREF_HIZ;
5403}
5404EXPORT_SYMBOL_HDA(snd_hda_get_default_vref);
5405
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005406/* correct the pin ctl value for matching with the pin cap */
5407unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
5408 hda_nid_t pin, unsigned int val)
5409{
5410 static unsigned int cap_lists[][2] = {
5411 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
5412 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
5413 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
5414 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
5415 };
5416 unsigned int cap;
5417
5418 if (!val)
5419 return 0;
5420 cap = snd_hda_query_pin_caps(codec, pin);
5421 if (!cap)
5422 return val; /* don't know what to do... */
5423
5424 if (val & AC_PINCTL_OUT_EN) {
5425 if (!(cap & AC_PINCAP_OUT))
5426 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5427 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
5428 val &= ~AC_PINCTL_HP_EN;
5429 }
5430
5431 if (val & AC_PINCTL_IN_EN) {
5432 if (!(cap & AC_PINCAP_IN))
5433 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5434 else {
5435 unsigned int vcap, vref;
5436 int i;
5437 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5438 vref = val & AC_PINCTL_VREFEN;
5439 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
5440 if (vref == cap_lists[i][0] &&
5441 !(vcap & cap_lists[i][1])) {
5442 if (i == ARRAY_SIZE(cap_lists) - 1)
5443 vref = AC_PINCTL_VREF_HIZ;
5444 else
5445 vref = cap_lists[i + 1][0];
5446 }
5447 }
5448 val &= ~AC_PINCTL_VREFEN;
5449 val |= vref;
5450 }
5451 }
5452
5453 return val;
5454}
5455EXPORT_SYMBOL_HDA(snd_hda_correct_pin_ctl);
5456
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005457int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5458 unsigned int val, bool cached)
5459{
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005460 val = snd_hda_correct_pin_ctl(codec, pin, val);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01005461 snd_hda_codec_set_pin_target(codec, pin, val);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005462 if (cached)
5463 return snd_hda_codec_update_cache(codec, pin, 0,
5464 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5465 else
5466 return snd_hda_codec_write(codec, pin, 0,
5467 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5468}
5469EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl);
5470
Takashi Iwai990061c2010-09-09 22:08:44 +02005471/**
5472 * snd_hda_add_imux_item - Add an item to input_mux
5473 *
5474 * When the same label is used already in the existing items, the number
5475 * suffix is appended to the label. This label index number is stored
5476 * to type_idx when non-NULL pointer is given.
5477 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005478int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5479 int index, int *type_idx)
5480{
5481 int i, label_idx = 0;
5482 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5483 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5484 return -EINVAL;
5485 }
5486 for (i = 0; i < imux->num_items; i++) {
5487 if (!strncmp(label, imux->items[i].label, strlen(label)))
5488 label_idx++;
5489 }
5490 if (type_idx)
5491 *type_idx = label_idx;
5492 if (label_idx > 0)
5493 snprintf(imux->items[imux->num_items].label,
5494 sizeof(imux->items[imux->num_items].label),
5495 "%s %d", label, label_idx);
5496 else
5497 strlcpy(imux->items[imux->num_items].label, label,
5498 sizeof(imux->items[imux->num_items].label));
5499 imux->items[imux->num_items].index = index;
5500 imux->num_items++;
5501 return 0;
5502}
5503EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005504
Takashi Iwai4a471b72005-12-07 13:56:29 +01005505
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506#ifdef CONFIG_PM
5507/*
5508 * power management
5509 */
5510
5511/**
5512 * snd_hda_suspend - suspend the codecs
5513 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 *
5515 * Returns 0 if successful.
5516 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005517int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005519 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520
Takashi Iwai0ba21762007-04-16 11:29:14 +02005521 list_for_each_entry(codec, &bus->codec_list, list) {
David Henningsson26a6cb62012-10-09 15:04:21 +02005522 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005523 if (hda_codec_is_power_on(codec))
Dylan Reidd17344b2012-09-28 15:57:01 -07005524 hda_call_codec_suspend(codec, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525 }
5526 return 0;
5527}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005528EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529
5530/**
5531 * snd_hda_resume - resume the codecs
5532 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533 *
5534 * Returns 0 if successful.
5535 */
5536int snd_hda_resume(struct hda_bus *bus)
5537{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005538 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539
Takashi Iwai0ba21762007-04-16 11:29:14 +02005540 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai7f308302012-05-08 16:52:23 +02005541 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543 return 0;
5544}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005545EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005546#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005547
5548/*
5549 * generic arrays
5550 */
5551
Takashi Iwaid5191e52009-11-16 14:58:17 +01005552/**
5553 * snd_array_new - get a new element from the given array
5554 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005555 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005556 * Get a new element from the given array. If it exceeds the
5557 * pre-allocated array size, re-allocate the array.
5558 *
5559 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005560 */
5561void *snd_array_new(struct snd_array *array)
5562{
Takashi Iwai12f17712012-10-10 08:59:14 +02005563 if (snd_BUG_ON(!array->elem_size))
5564 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005565 if (array->used >= array->alloced) {
5566 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005567 int size = (num + 1) * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005568 void *nlist;
5569 if (snd_BUG_ON(num >= 4096))
5570 return NULL;
Takashi Iwai5265fd92013-03-13 12:15:28 +01005571 nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005572 if (!nlist)
5573 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005574 array->list = nlist;
5575 array->alloced = num;
5576 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005577 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005578}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005579EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005580
Takashi Iwaid5191e52009-11-16 14:58:17 +01005581/**
5582 * snd_array_free - free the given array elements
5583 * @array: the array object
5584 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005585void snd_array_free(struct snd_array *array)
5586{
5587 kfree(array->list);
5588 array->used = 0;
5589 array->alloced = 0;
5590 array->list = NULL;
5591}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005592EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005593
Takashi Iwaid5191e52009-11-16 14:58:17 +01005594/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005595 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5596 * @pcm: PCM caps bits
5597 * @buf: the string buffer to write
5598 * @buflen: the max buffer length
5599 *
5600 * used by hda_proc.c and hda_eld.c
5601 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005602void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5603{
5604 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5605 int i, j;
5606
5607 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5608 if (pcm & (AC_SUPPCM_BITS_8 << i))
5609 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5610
5611 buf[j] = '\0'; /* necessary when j == 0 */
5612}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005613EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005614
5615MODULE_DESCRIPTION("HDA codec core");
5616MODULE_LICENSE("GPL");