blob: 55108b5fb2919c597177d9dc5f28b56cb34d28a2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Universal Interface for Intel High Definition Audio Codec
3 *
4 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
5 *
6 *
7 * This driver is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This driver is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
Takashi Iwai18478e82012-03-09 17:51:10 +010022#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/init.h>
24#include <linux/delay.h>
25#include <linux/slab.h>
26#include <linux/pci.h>
Ingo Molnar62932df2006-01-16 16:34:20 +010027#include <linux/mutex.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040028#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <sound/core.h>
30#include "hda_codec.h"
31#include <sound/asoundef.h>
Jaroslav Kysela302e9c52006-07-05 17:39:49 +020032#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <sound/initval.h>
Takashi Iwaicd372fb2011-03-03 14:40:14 +010034#include <sound/jack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "hda_local.h"
Jaroslav Kysela123c07a2009-10-21 14:48:23 +020036#include "hda_beep.h"
Takashi Iwai1835a0f2011-10-27 22:12:46 +020037#include "hda_jack.h"
Takashi Iwai28073142007-07-27 18:58:06 +020038#include <sound/hda_hwdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Takashi Iwaid66fee52011-08-02 15:39:31 +020040#define CREATE_TRACE_POINTS
41#include "hda_trace.h"
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/*
44 * vendor / preset table
45 */
46
47struct hda_vendor_id {
48 unsigned int id;
49 const char *name;
50};
51
52/* codec vendor labels */
53static struct hda_vendor_id hda_vendor_ids[] = {
Takashi Iwaic8cd1282008-02-13 16:59:29 +010054 { 0x1002, "ATI" },
Takashi Iwaie5f14242009-07-01 18:11:44 +020055 { 0x1013, "Cirrus Logic" },
Takashi Iwaia9226252006-09-17 22:05:54 +020056 { 0x1057, "Motorola" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010057 { 0x1095, "Silicon Image" },
Takashi Iwai31117b72008-12-16 14:43:21 +010058 { 0x10de, "Nvidia" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010059 { 0x10ec, "Realtek" },
Takashi Iwai4e01f542009-04-16 08:53:34 +020060 { 0x1102, "Creative" },
Joseph Chanc577b8a2006-11-29 15:29:40 +010061 { 0x1106, "VIA" },
Matthew Ranostay7f168592007-10-18 17:38:17 +020062 { 0x111d, "IDT" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010063 { 0x11c1, "LSI" },
Takashi Iwai54b903e2005-05-15 14:30:10 +020064 { 0x11d4, "Analog Devices" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 { 0x13f6, "C-Media" },
Takashi Iwaia9226252006-09-17 22:05:54 +020066 { 0x14f1, "Conexant" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010067 { 0x17e8, "Chrontel" },
68 { 0x1854, "LG" },
Mark Brown8199de32008-10-28 14:50:13 +000069 { 0x1aec, "Wolfson Microelectronics" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 { 0x434d, "C-Media" },
Takashi Iwai74c61132008-12-18 09:11:33 +010071 { 0x8086, "Intel" },
Matt2f2f4252005-04-13 14:45:30 +020072 { 0x8384, "SigmaTel" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 {} /* terminator */
74};
75
Takashi Iwai1289e9e2008-11-27 15:47:11 +010076static DEFINE_MUTEX(preset_mutex);
77static LIST_HEAD(hda_preset_tables);
78
79int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
80{
81 mutex_lock(&preset_mutex);
82 list_add_tail(&preset->list, &hda_preset_tables);
83 mutex_unlock(&preset_mutex);
84 return 0;
85}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010086EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset);
Takashi Iwai1289e9e2008-11-27 15:47:11 +010087
88int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
89{
90 mutex_lock(&preset_mutex);
91 list_del(&preset->list);
92 mutex_unlock(&preset_mutex);
93 return 0;
94}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010095EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Takashi Iwai83012a72012-08-24 18:38:08 +020097#ifdef CONFIG_PM
Takashi Iwaid846b172012-11-24 11:58:24 +010098#define codec_in_pm(codec) ((codec)->in_pm)
Takashi Iwaicb53c622007-08-10 17:21:45 +020099static void hda_power_work(struct work_struct *work);
100static void hda_keep_power_on(struct hda_codec *codec);
Takashi Iwaie581f3d2011-07-26 10:19:20 +0200101#define hda_codec_is_power_on(codec) ((codec)->power_on)
Takashi Iwai68467f52012-08-28 09:14:29 -0700102static inline void hda_call_pm_notify(struct hda_bus *bus, bool power_up)
103{
104 if (bus->ops.pm_notify)
105 bus->ops.pm_notify(bus, power_up);
106}
Takashi Iwaicb53c622007-08-10 17:21:45 +0200107#else
Takashi Iwaid846b172012-11-24 11:58:24 +0100108#define codec_in_pm(codec) 0
Takashi Iwaicb53c622007-08-10 17:21:45 +0200109static inline void hda_keep_power_on(struct hda_codec *codec) {}
Takashi Iwaie581f3d2011-07-26 10:19:20 +0200110#define hda_codec_is_power_on(codec) 1
Takashi Iwai68467f52012-08-28 09:14:29 -0700111#define hda_call_pm_notify(bus, state) {}
Takashi Iwaicb53c622007-08-10 17:21:45 +0200112#endif
113
Takashi Iwaid5191e52009-11-16 14:58:17 +0100114/**
115 * snd_hda_get_jack_location - Give a location string of the jack
116 * @cfg: pin default config value
117 *
118 * Parse the pin default config value and returns the string of the
119 * jack location, e.g. "Rear", "Front", etc.
120 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400121const char *snd_hda_get_jack_location(u32 cfg)
122{
123 static char *bases[7] = {
124 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
125 };
126 static unsigned char specials_idx[] = {
127 0x07, 0x08,
128 0x17, 0x18, 0x19,
129 0x37, 0x38
130 };
131 static char *specials[] = {
132 "Rear Panel", "Drive Bar",
133 "Riser", "HDMI", "ATAPI",
134 "Mobile-In", "Mobile-Out"
135 };
136 int i;
137 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
138 if ((cfg & 0x0f) < 7)
139 return bases[cfg & 0x0f];
140 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
141 if (cfg == specials_idx[i])
142 return specials[i];
143 }
144 return "UNKNOWN";
145}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100146EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400147
Takashi Iwaid5191e52009-11-16 14:58:17 +0100148/**
149 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
150 * @cfg: pin default config value
151 *
152 * Parse the pin default config value and returns the string of the
153 * jack connectivity, i.e. external or internal connection.
154 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400155const char *snd_hda_get_jack_connectivity(u32 cfg)
156{
157 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
158
159 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
160}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100161EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400162
Takashi Iwaid5191e52009-11-16 14:58:17 +0100163/**
164 * snd_hda_get_jack_type - Give a type string of the jack
165 * @cfg: pin default config value
166 *
167 * Parse the pin default config value and returns the string of the
168 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
169 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400170const char *snd_hda_get_jack_type(u32 cfg)
171{
172 static char *jack_types[16] = {
173 "Line Out", "Speaker", "HP Out", "CD",
174 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
175 "Line In", "Aux", "Mic", "Telephony",
David Henningssonaeb3a972013-04-04 11:47:13 +0200176 "SPDIF In", "Digital In", "Reserved", "Other"
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400177 };
178
179 return jack_types[(cfg & AC_DEFCFG_DEVICE)
180 >> AC_DEFCFG_DEVICE_SHIFT];
181}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100182EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400183
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100184/*
185 * Compose a 32bit command word to be sent to the HD-audio controller
186 */
187static inline unsigned int
188make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
189 unsigned int verb, unsigned int parm)
190{
191 u32 val;
192
Takashi Iwai82e1b802009-07-17 12:47:34 +0200193 if ((codec->addr & ~0xf) || (direct & ~1) || (nid & ~0x7f) ||
194 (verb & ~0xfff) || (parm & ~0xffff)) {
Wu Fengguang6430aee2009-07-17 16:49:19 +0800195 printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
196 codec->addr, direct, nid, verb, parm);
197 return ~0;
198 }
199
200 val = (u32)codec->addr << 28;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100201 val |= (u32)direct << 27;
202 val |= (u32)nid << 20;
203 val |= verb << 8;
204 val |= parm;
205 return val;
206}
207
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200208/*
209 * Send and receive a verb
210 */
211static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
212 unsigned int *res)
213{
214 struct hda_bus *bus = codec->bus;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200215 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200216
Wu Fengguang6430aee2009-07-17 16:49:19 +0800217 if (cmd == ~0)
218 return -1;
219
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200220 if (res)
221 *res = -1;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200222 again:
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200223 snd_hda_power_up(codec);
224 mutex_lock(&bus->cmd_mutex);
Takashi Iwai3bcce5c2012-12-20 11:17:17 +0100225 for (;;) {
226 trace_hda_send_cmd(codec, cmd);
227 err = bus->ops.command(bus, cmd);
228 if (err != -EAGAIN)
229 break;
230 /* process pending verbs */
231 bus->ops.get_response(bus, codec->addr);
232 }
Takashi Iwaid66fee52011-08-02 15:39:31 +0200233 if (!err && res) {
Wu Fengguangdeadff12009-08-01 18:45:16 +0800234 *res = bus->ops.get_response(bus, codec->addr);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200235 trace_hda_get_response(codec, *res);
236 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200237 mutex_unlock(&bus->cmd_mutex);
238 snd_hda_power_down(codec);
Takashi Iwaid846b172012-11-24 11:58:24 +0100239 if (!codec_in_pm(codec) && res && *res == -1 && bus->rirb_error) {
Takashi Iwai8dd78332009-06-02 01:16:07 +0200240 if (bus->response_reset) {
241 snd_printd("hda_codec: resetting BUS due to "
242 "fatal communication error\n");
Takashi Iwaid66fee52011-08-02 15:39:31 +0200243 trace_hda_bus_reset(bus);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200244 bus->ops.bus_reset(bus);
245 }
246 goto again;
247 }
248 /* clear reset-flag when the communication gets recovered */
Takashi Iwaid846b172012-11-24 11:58:24 +0100249 if (!err || codec_in_pm(codec))
Takashi Iwai8dd78332009-06-02 01:16:07 +0200250 bus->response_reset = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200251 return err;
252}
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254/**
255 * snd_hda_codec_read - send a command and get the response
256 * @codec: the HDA codec
257 * @nid: NID to send the command
258 * @direct: direct flag
259 * @verb: the verb to send
260 * @parm: the parameter for the verb
261 *
262 * Send a single command and read the corresponding response.
263 *
264 * Returns the obtained response value, or -1 for an error.
265 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200266unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
267 int direct,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 unsigned int verb, unsigned int parm)
269{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200270 unsigned cmd = make_codec_cmd(codec, nid, direct, verb, parm);
271 unsigned int res;
Greg Thelen9857edf2011-06-13 07:45:45 -0700272 if (codec_exec_verb(codec, cmd, &res))
273 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 return res;
275}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100276EXPORT_SYMBOL_HDA(snd_hda_codec_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278/**
279 * snd_hda_codec_write - send a single command without waiting for response
280 * @codec: the HDA codec
281 * @nid: NID to send the command
282 * @direct: direct flag
283 * @verb: the verb to send
284 * @parm: the parameter for the verb
285 *
286 * Send a single command without waiting for response.
287 *
288 * Returns 0 if successful, or a negative error code.
289 */
290int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
291 unsigned int verb, unsigned int parm)
292{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200293 unsigned int cmd = make_codec_cmd(codec, nid, direct, verb, parm);
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100294 unsigned int res;
Takashi Iwaib20f3b82009-06-02 01:20:22 +0200295 return codec_exec_verb(codec, cmd,
296 codec->bus->sync_write ? &res : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100298EXPORT_SYMBOL_HDA(snd_hda_codec_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300/**
301 * snd_hda_sequence_write - sequence writes
302 * @codec: the HDA codec
303 * @seq: VERB array to send
304 *
305 * Send the commands sequentially from the given array.
306 * The array must be terminated with NID=0.
307 */
308void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
309{
310 for (; seq->nid; seq++)
311 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
312}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100313EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315/**
316 * snd_hda_get_sub_nodes - get the range of sub nodes
317 * @codec: the HDA codec
318 * @nid: NID to parse
319 * @start_id: the pointer to store the start NID
320 *
321 * Parse the NID and store the start NID of its sub-nodes.
322 * Returns the number of sub-nodes.
323 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200324int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
325 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
327 unsigned int parm;
328
329 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
Danny Tholene8a7f132007-09-11 21:41:56 +0200330 if (parm == -1)
331 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 *start_id = (parm >> 16) & 0x7fff;
333 return (int)(parm & 0x7fff);
334}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100335EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100337/* connection list element */
338struct hda_conn_list {
339 struct list_head list;
340 int len;
341 hda_nid_t nid;
342 hda_nid_t conns[0];
343};
344
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200345/* look up the cached results */
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100346static struct hda_conn_list *
347lookup_conn_list(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200348{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100349 struct hda_conn_list *p;
350 list_for_each_entry(p, &codec->conn_list, list) {
351 if (p->nid == nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200352 return p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200353 }
354 return NULL;
355}
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200356
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100357static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
358 const hda_nid_t *list)
359{
360 struct hda_conn_list *p;
361
362 p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL);
363 if (!p)
364 return -ENOMEM;
365 p->len = len;
366 p->nid = nid;
367 memcpy(p->conns, list, len * sizeof(hda_nid_t));
368 list_add(&p->list, &codec->conn_list);
369 return 0;
370}
371
372static void remove_conn_list(struct hda_codec *codec)
373{
374 while (!list_empty(&codec->conn_list)) {
375 struct hda_conn_list *p;
376 p = list_first_entry(&codec->conn_list, typeof(*p), list);
377 list_del(&p->list);
378 kfree(p);
379 }
380}
381
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200382/* read the connection and add to the cache */
383static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200384{
Takashi Iwai4eea3092013-02-07 18:18:19 +0100385 hda_nid_t list[32];
386 hda_nid_t *result = list;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200387 int len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200388
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200389 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
Takashi Iwai4eea3092013-02-07 18:18:19 +0100390 if (len == -ENOSPC) {
391 len = snd_hda_get_num_raw_conns(codec, nid);
392 result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL);
393 if (!result)
394 return -ENOMEM;
395 len = snd_hda_get_raw_connections(codec, nid, result, len);
396 }
397 if (len >= 0)
398 len = snd_hda_override_conn_list(codec, nid, len, result);
399 if (result != list)
400 kfree(result);
401 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200402}
Takashi Iwaidce20792011-06-24 14:10:28 +0200403
404/**
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100405 * snd_hda_get_conn_list - get connection list
406 * @codec: the HDA codec
407 * @nid: NID to parse
408 * @len: number of connection list entries
409 * @listp: the pointer to store NID list
410 *
411 * Parses the connection list of the given widget and stores the pointer
412 * to the list of NIDs.
413 *
414 * Returns the number of connections, or a negative error code.
415 *
416 * Note that the returned pointer isn't protected against the list
417 * modification. If snd_hda_override_conn_list() might be called
418 * concurrently, protect with a mutex appropriately.
419 */
420int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
421 const hda_nid_t **listp)
422{
423 bool added = false;
424
425 for (;;) {
426 int err;
427 const struct hda_conn_list *p;
428
429 /* if the connection-list is already cached, read it */
430 p = lookup_conn_list(codec, nid);
431 if (p) {
432 if (listp)
433 *listp = p->conns;
434 return p->len;
435 }
436 if (snd_BUG_ON(added))
437 return -EINVAL;
438
439 err = read_and_add_raw_conns(codec, nid);
440 if (err < 0)
441 return err;
442 added = true;
443 }
444}
445EXPORT_SYMBOL_HDA(snd_hda_get_conn_list);
446
447/**
Takashi Iwaidce20792011-06-24 14:10:28 +0200448 * snd_hda_get_connections - copy connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 * @codec: the HDA codec
450 * @nid: NID to parse
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200451 * @conn_list: connection list array; when NULL, checks only the size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 * @max_conns: max. number of connections to store
453 *
454 * Parses the connection list of the given widget and stores the list
455 * of NIDs.
456 *
457 * Returns the number of connections, or a negative error code.
458 */
459int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200460 hda_nid_t *conn_list, int max_conns)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200461{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100462 const hda_nid_t *list;
463 int len = snd_hda_get_conn_list(codec, nid, &list);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200464
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100465 if (len > 0 && conn_list) {
466 if (len > max_conns) {
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200467 snd_printk(KERN_ERR "hda_codec: "
468 "Too many connections %d for NID 0x%x\n",
469 len, nid);
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200470 return -EINVAL;
471 }
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100472 memcpy(conn_list, list, len * sizeof(hda_nid_t));
Takashi Iwaidce20792011-06-24 14:10:28 +0200473 }
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200474
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100475 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200476}
477EXPORT_SYMBOL_HDA(snd_hda_get_connections);
478
Takashi Iwai4eea3092013-02-07 18:18:19 +0100479/* return CONNLIST_LEN parameter of the given widget */
480static unsigned int get_num_conns(struct hda_codec *codec, hda_nid_t nid)
481{
482 unsigned int wcaps = get_wcaps(codec, nid);
483 unsigned int parm;
484
485 if (!(wcaps & AC_WCAP_CONN_LIST) &&
486 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
487 return 0;
488
489 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
490 if (parm == -1)
491 parm = 0;
492 return parm;
493}
494
495int snd_hda_get_num_raw_conns(struct hda_codec *codec, hda_nid_t nid)
496{
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100497 return snd_hda_get_raw_connections(codec, nid, NULL, 0);
Takashi Iwai4eea3092013-02-07 18:18:19 +0100498}
499
Takashi Iwai9e7717c2011-07-11 15:42:52 +0200500/**
501 * snd_hda_get_raw_connections - copy connection list without cache
502 * @codec: the HDA codec
503 * @nid: NID to parse
504 * @conn_list: connection list array
505 * @max_conns: max. number of connections to store
506 *
507 * Like snd_hda_get_connections(), copy the connection list but without
508 * checking through the connection-list cache.
509 * Currently called only from hda_proc.c, so not exported.
510 */
511int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
512 hda_nid_t *conn_list, int max_conns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
514 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100515 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 unsigned int shift, num_elems, mask;
Takashi Iwai54d17402005-11-21 16:33:22 +0100517 hda_nid_t prev_nid;
Takashi Iwai5fdaecd2012-12-20 10:45:55 +0100518 int null_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Takashi Iwai4eea3092013-02-07 18:18:19 +0100520 parm = get_num_conns(codec, nid);
521 if (!parm)
Takashi Iwai8d087c72011-06-28 12:45:47 +0200522 return 0;
Jaroslav Kysela16a433d2009-07-22 16:20:40 +0200523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 if (parm & AC_CLIST_LONG) {
525 /* long form */
526 shift = 16;
527 num_elems = 2;
528 } else {
529 /* short form */
530 shift = 8;
531 num_elems = 4;
532 }
533 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 mask = (1 << (shift-1)) - 1;
535
Takashi Iwai0ba21762007-04-16 11:29:14 +0200536 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 return 0; /* no connection */
538
539 if (conn_len == 1) {
540 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200541 parm = snd_hda_codec_read(codec, nid, 0,
542 AC_VERB_GET_CONNECT_LIST, 0);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200543 if (parm == -1 && codec->bus->rirb_error)
544 return -EIO;
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100545 if (conn_list)
546 conn_list[0] = parm & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 return 1;
548 }
549
550 /* multi connection */
551 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100552 prev_nid = 0;
553 for (i = 0; i < conn_len; i++) {
554 int range_val;
555 hda_nid_t val, n;
556
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200557 if (i % num_elems == 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100558 parm = snd_hda_codec_read(codec, nid, 0,
559 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200560 if (parm == -1 && codec->bus->rirb_error)
561 return -EIO;
562 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200563 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100564 val = parm & mask;
Takashi Iwai5fdaecd2012-12-20 10:45:55 +0100565 if (val == 0 && null_count++) { /* no second chance */
Jaroslav Kysela2e9bf242009-07-18 11:48:19 +0200566 snd_printk(KERN_WARNING "hda_codec: "
567 "invalid CONNECT_LIST verb %x[%i]:%x\n",
568 nid, i, parm);
569 return 0;
570 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100571 parm >>= shift;
572 if (range_val) {
573 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200574 if (!prev_nid || prev_nid >= val) {
575 snd_printk(KERN_WARNING "hda_codec: "
576 "invalid dep_range_val %x:%x\n",
577 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100578 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100580 for (n = prev_nid + 1; n <= val; n++) {
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100581 if (conn_list) {
582 if (conns >= max_conns)
583 return -ENOSPC;
584 conn_list[conns] = n;
585 }
586 conns++;
Takashi Iwai54d17402005-11-21 16:33:22 +0100587 }
588 } else {
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100589 if (conn_list) {
590 if (conns >= max_conns)
591 return -ENOSPC;
592 conn_list[conns] = val;
593 }
594 conns++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100596 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 }
598 return conns;
599}
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200602 * snd_hda_override_conn_list - add/modify the connection-list to cache
603 * @codec: the HDA codec
604 * @nid: NID to parse
605 * @len: number of connection list entries
606 * @list: the list of connection entries
607 *
608 * Add or modify the given connection-list to the cache. If the corresponding
609 * cache already exists, invalidate it and append a new one.
610 *
611 * Returns zero or a negative error code.
612 */
613int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
614 const hda_nid_t *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100616 struct hda_conn_list *p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200617
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100618 p = lookup_conn_list(codec, nid);
619 if (p) {
620 list_del(&p->list);
621 kfree(p);
622 }
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200623
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100624 return add_conn_list(codec, nid, len, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625}
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200626EXPORT_SYMBOL_HDA(snd_hda_override_conn_list);
627
628/**
Takashi Iwai8d087c72011-06-28 12:45:47 +0200629 * snd_hda_get_conn_index - get the connection index of the given NID
630 * @codec: the HDA codec
631 * @mux: NID containing the list
632 * @nid: NID to select
633 * @recursive: 1 when searching NID recursively, otherwise 0
634 *
635 * Parses the connection list of the widget @mux and checks whether the
636 * widget @nid is present. If it is, return the connection index.
637 * Otherwise it returns -1.
638 */
639int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
640 hda_nid_t nid, int recursive)
641{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100642 const hda_nid_t *conn;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200643 int i, nums;
644
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100645 nums = snd_hda_get_conn_list(codec, mux, &conn);
Takashi Iwai8d087c72011-06-28 12:45:47 +0200646 for (i = 0; i < nums; i++)
647 if (conn[i] == nid)
648 return i;
649 if (!recursive)
650 return -1;
Takashi Iwaid94ddd82012-12-20 14:42:42 +0100651 if (recursive > 10) {
Takashi Iwai8d087c72011-06-28 12:45:47 +0200652 snd_printd("hda_codec: too deep connection for 0x%x\n", nid);
653 return -1;
654 }
655 recursive++;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200656 for (i = 0; i < nums; i++) {
657 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
658 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
659 continue;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200660 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
661 return i;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200662 }
Takashi Iwai8d087c72011-06-28 12:45:47 +0200663 return -1;
664}
665EXPORT_SYMBOL_HDA(snd_hda_get_conn_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667/**
668 * snd_hda_queue_unsol_event - add an unsolicited event to queue
669 * @bus: the BUS
670 * @res: unsolicited event (lower 32bit of RIRB entry)
671 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
672 *
673 * Adds the given event to the queue. The events are processed in
674 * the workqueue asynchronously. Call this function in the interrupt
675 * hanlder when RIRB receives an unsolicited event.
676 *
677 * Returns 0 if successful, or a negative error code.
678 */
679int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
680{
681 struct hda_bus_unsolicited *unsol;
682 unsigned int wp;
683
Wang YanQing2195b062013-05-07 11:27:33 +0800684 if (!bus || !bus->workq)
685 return 0;
686
Takashi Iwaiecf726f2011-08-09 14:22:44 +0200687 trace_hda_unsol_event(bus, res, res_ex);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200688 unsol = bus->unsol;
689 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 return 0;
691
692 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
693 unsol->wp = wp;
694
695 wp <<= 1;
696 unsol->queue[wp] = res;
697 unsol->queue[wp + 1] = res_ex;
698
Takashi Iwai6acaed32009-01-12 10:09:24 +0100699 queue_work(bus->workq, &unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701 return 0;
702}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100703EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800706 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 */
David Howellsc4028952006-11-22 14:57:56 +0000708static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
David Howellsc4028952006-11-22 14:57:56 +0000710 struct hda_bus_unsolicited *unsol =
711 container_of(work, struct hda_bus_unsolicited, work);
712 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 struct hda_codec *codec;
714 unsigned int rp, caddr, res;
715
716 while (unsol->rp != unsol->wp) {
717 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
718 unsol->rp = rp;
719 rp <<= 1;
720 res = unsol->queue[rp];
721 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200722 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 continue;
724 codec = bus->caddr_tbl[caddr & 0x0f];
725 if (codec && codec->patch_ops.unsol_event)
726 codec->patch_ops.unsol_event(codec, res);
727 }
728}
729
730/*
731 * initialize unsolicited queue
732 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200733static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
735 struct hda_bus_unsolicited *unsol;
736
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100737 if (bus->unsol) /* already initialized */
738 return 0;
739
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200740 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200741 if (!unsol) {
742 snd_printk(KERN_ERR "hda_codec: "
743 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 return -ENOMEM;
745 }
David Howellsc4028952006-11-22 14:57:56 +0000746 INIT_WORK(&unsol->work, process_unsol_events);
747 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 bus->unsol = unsol;
749 return 0;
750}
751
752/*
753 * destructor
754 */
755static void snd_hda_codec_free(struct hda_codec *codec);
756
757static int snd_hda_bus_free(struct hda_bus *bus)
758{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200759 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Takashi Iwai0ba21762007-04-16 11:29:14 +0200761 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 return 0;
Takashi Iwai6acaed32009-01-12 10:09:24 +0100763 if (bus->workq)
764 flush_workqueue(bus->workq);
765 if (bus->unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 kfree(bus->unsol);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200767 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 snd_hda_codec_free(codec);
769 }
770 if (bus->ops.private_free)
771 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100772 if (bus->workq)
773 destroy_workqueue(bus->workq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 kfree(bus);
775 return 0;
776}
777
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100778static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
780 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100781 bus->shutdown = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 return snd_hda_bus_free(bus);
783}
784
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200785#ifdef CONFIG_SND_HDA_HWDEP
786static int snd_hda_bus_dev_register(struct snd_device *device)
787{
788 struct hda_bus *bus = device->device_data;
789 struct hda_codec *codec;
790 list_for_each_entry(codec, &bus->codec_list, list) {
791 snd_hda_hwdep_add_sysfs(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +0100792 snd_hda_hwdep_add_power_sysfs(codec);
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200793 }
794 return 0;
795}
796#else
797#define snd_hda_bus_dev_register NULL
798#endif
799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800/**
801 * snd_hda_bus_new - create a HDA bus
802 * @card: the card entry
803 * @temp: the template for hda_bus information
804 * @busp: the pointer to store the created bus instance
805 *
806 * Returns 0 if successful, or a negative error code.
807 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +0100808int snd_hda_bus_new(struct snd_card *card,
Takashi Iwai756e2b02007-04-16 11:27:07 +0200809 const struct hda_bus_template *temp,
810 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
812 struct hda_bus *bus;
813 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100814 static struct snd_device_ops dev_ops = {
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200815 .dev_register = snd_hda_bus_dev_register,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 .dev_free = snd_hda_bus_dev_free,
817 };
818
Takashi Iwaida3cec32008-08-08 17:12:14 +0200819 if (snd_BUG_ON(!temp))
820 return -EINVAL;
821 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
822 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
824 if (busp)
825 *busp = NULL;
826
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200827 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 if (bus == NULL) {
829 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
830 return -ENOMEM;
831 }
832
833 bus->card = card;
834 bus->private_data = temp->private_data;
835 bus->pci = temp->pci;
836 bus->modelname = temp->modelname;
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100837 bus->power_save = temp->power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 bus->ops = temp->ops;
839
Ingo Molnar62932df2006-01-16 16:34:20 +0100840 mutex_init(&bus->cmd_mutex);
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200841 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 INIT_LIST_HEAD(&bus->codec_list);
843
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100844 snprintf(bus->workq_name, sizeof(bus->workq_name),
845 "hd-audio%d", card->number);
846 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100847 if (!bus->workq) {
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100848 snd_printk(KERN_ERR "cannot create workqueue %s\n",
849 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100850 kfree(bus);
851 return -ENOMEM;
852 }
853
Takashi Iwai0ba21762007-04-16 11:29:14 +0200854 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
855 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 snd_hda_bus_free(bus);
857 return err;
858 }
859 if (busp)
860 *busp = bus;
861 return 0;
862}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100863EXPORT_SYMBOL_HDA(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Takashi Iwai82467612007-07-27 19:15:54 +0200865#ifdef CONFIG_SND_HDA_GENERIC
866#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200867 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200868#else
869#define is_generic_config(codec) 0
870#endif
871
Takashi Iwai645f10c2008-11-28 15:07:37 +0100872#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100873#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
874#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100875#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100876#endif
877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878/*
879 * find a matching codec preset
880 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200881static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200882find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100884 struct hda_codec_preset_list *tbl;
885 const struct hda_codec_preset *preset;
Takashi Iwai5d908ab2012-08-24 18:40:10 +0200886 unsigned int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Takashi Iwai82467612007-07-27 19:15:54 +0200888 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100889 return NULL; /* use the generic parser */
890
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100891 again:
892 mutex_lock(&preset_mutex);
893 list_for_each_entry(tbl, &hda_preset_tables, list) {
894 if (!try_module_get(tbl->owner)) {
895 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
896 continue;
897 }
898 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100900 if (preset->afg && preset->afg != codec->afg)
901 continue;
902 if (preset->mfg && preset->mfg != codec->mfg)
903 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200904 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200906 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200907 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100908 preset->rev == codec->revision_id)) {
909 mutex_unlock(&preset_mutex);
910 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100914 module_put(tbl->owner);
915 }
916 mutex_unlock(&preset_mutex);
917
918 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
919 char name[32];
920 if (!mod_requested)
921 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
922 codec->vendor_id);
923 else
924 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
925 (codec->vendor_id >> 16) & 0xffff);
926 request_module(name);
927 mod_requested++;
928 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 }
930 return NULL;
931}
932
933/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200934 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200936static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
938 const struct hda_vendor_id *c;
939 const char *vendor = NULL;
940 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200941 char tmp[16];
942
943 if (codec->vendor_name)
944 goto get_chip_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
946 for (c = hda_vendor_ids; c->id; c++) {
947 if (c->id == vendor_id) {
948 vendor = c->name;
949 break;
950 }
951 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200952 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 sprintf(tmp, "Generic %04x", vendor_id);
954 vendor = tmp;
955 }
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200956 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
957 if (!codec->vendor_name)
958 return -ENOMEM;
959
960 get_chip_name:
961 if (codec->chip_name)
962 return 0;
963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 if (codec->preset && codec->preset->name)
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200965 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
966 else {
967 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
968 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
969 }
970 if (!codec->chip_name)
Takashi Iwaif44ac832008-07-30 15:01:45 +0200971 return -ENOMEM;
972 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973}
974
975/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200976 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +0100978static void setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200980 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 hda_nid_t nid;
982
983 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
984 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200985 function_id = snd_hda_param_read(codec, nid,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200986 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +0200987 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200988 case AC_GRP_AUDIO_FUNCTION:
989 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200990 codec->afg_function_id = function_id & 0xff;
991 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200992 break;
993 case AC_GRP_MODEM_FUNCTION:
994 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200995 codec->mfg_function_id = function_id & 0xff;
996 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200997 break;
998 default:
999 break;
1000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002}
1003
1004/*
Takashi Iwai54d17402005-11-21 16:33:22 +01001005 * read widget caps for each widget and store in cache
1006 */
1007static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
1008{
1009 int i;
1010 hda_nid_t nid;
1011
1012 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
1013 &codec->start_nid);
1014 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001015 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +01001016 return -ENOMEM;
1017 nid = codec->start_nid;
1018 for (i = 0; i < codec->num_nodes; i++, nid++)
1019 codec->wcaps[i] = snd_hda_param_read(codec, nid,
1020 AC_PAR_AUDIO_WIDGET_CAP);
1021 return 0;
1022}
1023
Takashi Iwai3be14142009-02-20 14:11:16 +01001024/* read all pin default configurations and save codec->init_pins */
1025static int read_pin_defaults(struct hda_codec *codec)
1026{
1027 int i;
1028 hda_nid_t nid = codec->start_nid;
1029
1030 for (i = 0; i < codec->num_nodes; i++, nid++) {
1031 struct hda_pincfg *pin;
1032 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +02001033 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +01001034 if (wid_type != AC_WID_PIN)
1035 continue;
1036 pin = snd_array_new(&codec->init_pins);
1037 if (!pin)
1038 return -ENOMEM;
1039 pin->nid = nid;
1040 pin->cfg = snd_hda_codec_read(codec, nid, 0,
1041 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001042 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
1043 AC_VERB_GET_PIN_WIDGET_CONTROL,
1044 0);
Takashi Iwai3be14142009-02-20 14:11:16 +01001045 }
1046 return 0;
1047}
1048
1049/* look up the given pin config list and return the item matching with NID */
1050static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
1051 struct snd_array *array,
1052 hda_nid_t nid)
1053{
1054 int i;
1055 for (i = 0; i < array->used; i++) {
1056 struct hda_pincfg *pin = snd_array_elem(array, i);
1057 if (pin->nid == nid)
1058 return pin;
1059 }
1060 return NULL;
1061}
1062
Takashi Iwai3be14142009-02-20 14:11:16 +01001063/* set the current pin config value for the given NID.
1064 * the value is cached, and read via snd_hda_codec_get_pincfg()
1065 */
1066int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
1067 hda_nid_t nid, unsigned int cfg)
1068{
1069 struct hda_pincfg *pin;
1070
Takashi Iwaid5657ec2013-04-18 09:59:28 +02001071 /* the check below may be invalid when pins are added by a fixup
1072 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
1073 * for now
1074 */
1075 /*
Takashi Iwaib82855a2009-12-27 11:24:56 +01001076 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1077 return -EINVAL;
Takashi Iwaid5657ec2013-04-18 09:59:28 +02001078 */
Takashi Iwaib82855a2009-12-27 11:24:56 +01001079
Takashi Iwai3be14142009-02-20 14:11:16 +01001080 pin = look_up_pincfg(codec, list, nid);
1081 if (!pin) {
1082 pin = snd_array_new(list);
1083 if (!pin)
1084 return -ENOMEM;
1085 pin->nid = nid;
1086 }
1087 pin->cfg = cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001088 return 0;
1089}
1090
Takashi Iwaid5191e52009-11-16 14:58:17 +01001091/**
1092 * snd_hda_codec_set_pincfg - Override a pin default configuration
1093 * @codec: the HDA codec
1094 * @nid: NID to set the pin config
1095 * @cfg: the pin default config value
1096 *
1097 * Override a pin default configuration value in the cache.
1098 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1099 * priority than the real hardware value.
1100 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001101int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1102 hda_nid_t nid, unsigned int cfg)
1103{
Takashi Iwai346ff702009-02-23 09:42:57 +01001104 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001105}
1106EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
1107
Takashi Iwaid5191e52009-11-16 14:58:17 +01001108/**
1109 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1110 * @codec: the HDA codec
1111 * @nid: NID to get the pin config
1112 *
1113 * Get the current pin config value of the given pin NID.
1114 * If the pincfg value is cached or overridden via sysfs or driver,
1115 * returns the cached value.
1116 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001117unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1118{
1119 struct hda_pincfg *pin;
1120
Takashi Iwai3be14142009-02-20 14:11:16 +01001121#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai09b70e82013-01-10 18:21:56 +01001122 {
1123 unsigned int cfg = 0;
1124 mutex_lock(&codec->user_mutex);
1125 pin = look_up_pincfg(codec, &codec->user_pins, nid);
1126 if (pin)
1127 cfg = pin->cfg;
1128 mutex_unlock(&codec->user_mutex);
1129 if (cfg)
1130 return cfg;
1131 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001132#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001133 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1134 if (pin)
1135 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001136 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1137 if (pin)
1138 return pin->cfg;
1139 return 0;
1140}
1141EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
1142
Takashi Iwaid7fdc002013-01-10 08:38:04 +01001143/* remember the current pinctl target value */
1144int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
1145 unsigned int val)
1146{
1147 struct hda_pincfg *pin;
1148
1149 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1150 if (!pin)
1151 return -EINVAL;
1152 pin->target = val;
1153 return 0;
1154}
1155EXPORT_SYMBOL_HDA(snd_hda_codec_set_pin_target);
1156
1157/* return the current pinctl target value */
1158int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
1159{
1160 struct hda_pincfg *pin;
1161
1162 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1163 if (!pin)
1164 return 0;
1165 return pin->target;
1166}
1167EXPORT_SYMBOL_HDA(snd_hda_codec_get_pin_target);
1168
Takashi Iwai92ee6162009-12-27 11:18:59 +01001169/**
1170 * snd_hda_shutup_pins - Shut up all pins
1171 * @codec: the HDA codec
1172 *
1173 * Clear all pin controls to shup up before suspend for avoiding click noise.
1174 * The controls aren't cached so that they can be resumed properly.
1175 */
1176void snd_hda_shutup_pins(struct hda_codec *codec)
1177{
1178 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001179 /* don't shut up pins when unloading the driver; otherwise it breaks
1180 * the default pin setup at the next load of the driver
1181 */
1182 if (codec->bus->shutdown)
1183 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001184 for (i = 0; i < codec->init_pins.used; i++) {
1185 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1186 /* use read here for syncing after issuing each verb */
1187 snd_hda_codec_read(codec, pin->nid, 0,
1188 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1189 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001190 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001191}
1192EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
1193
Takashi Iwai2a439522011-07-26 09:52:50 +02001194#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001195/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1196static void restore_shutup_pins(struct hda_codec *codec)
1197{
1198 int i;
1199 if (!codec->pins_shutup)
1200 return;
1201 if (codec->bus->shutdown)
1202 return;
1203 for (i = 0; i < codec->init_pins.used; i++) {
1204 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1205 snd_hda_codec_write(codec, pin->nid, 0,
1206 AC_VERB_SET_PIN_WIDGET_CONTROL,
1207 pin->ctrl);
1208 }
1209 codec->pins_shutup = 0;
1210}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001211#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001212
David Henningsson26a6cb62012-10-09 15:04:21 +02001213static void hda_jackpoll_work(struct work_struct *work)
1214{
1215 struct hda_codec *codec =
1216 container_of(work, struct hda_codec, jackpoll_work.work);
1217 if (!codec->jackpoll_interval)
1218 return;
1219
1220 snd_hda_jack_set_dirty_all(codec);
1221 snd_hda_jack_poll_all(codec);
1222 queue_delayed_work(codec->bus->workq, &codec->jackpoll_work,
1223 codec->jackpoll_interval);
1224}
1225
Takashi Iwai01751f52007-08-10 16:59:39 +02001226static void init_hda_cache(struct hda_cache_rec *cache,
1227 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001228static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001229
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001230/* release all pincfg lists */
1231static void free_init_pincfgs(struct hda_codec *codec)
Takashi Iwai3be14142009-02-20 14:11:16 +01001232{
Takashi Iwai346ff702009-02-23 09:42:57 +01001233 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001234#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001235 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001236#endif
Takashi Iwai3be14142009-02-20 14:11:16 +01001237 snd_array_free(&codec->init_pins);
1238}
1239
Takashi Iwai54d17402005-11-21 16:33:22 +01001240/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001241 * audio-converter setup caches
1242 */
1243struct hda_cvt_setup {
1244 hda_nid_t nid;
1245 u8 stream_tag;
1246 u8 channel_id;
1247 u16 format_id;
1248 unsigned char active; /* cvt is currently used */
1249 unsigned char dirty; /* setups should be cleared */
1250};
1251
1252/* get or create a cache entry for the given audio converter NID */
1253static struct hda_cvt_setup *
1254get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1255{
1256 struct hda_cvt_setup *p;
1257 int i;
1258
1259 for (i = 0; i < codec->cvt_setups.used; i++) {
1260 p = snd_array_elem(&codec->cvt_setups, i);
1261 if (p->nid == nid)
1262 return p;
1263 }
1264 p = snd_array_new(&codec->cvt_setups);
1265 if (p)
1266 p->nid = nid;
1267 return p;
1268}
1269
1270/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 * codec destructor
1272 */
1273static void snd_hda_codec_free(struct hda_codec *codec)
1274{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001275 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 return;
David Henningsson26a6cb62012-10-09 15:04:21 +02001277 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai59cad162012-06-26 15:00:20 +02001278 snd_hda_jack_tbl_clear(codec);
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001279 free_init_pincfgs(codec);
Takashi Iwai83012a72012-08-24 18:38:08 +02001280#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02001281 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001282 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001283#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001285 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001286 snd_array_free(&codec->nids);
Takashi Iwai59cad162012-06-26 15:00:20 +02001287 snd_array_free(&codec->cvt_setups);
Stephen Warren7c935972011-06-01 11:14:17 -06001288 snd_array_free(&codec->spdif_out);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001289 remove_conn_list(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 codec->bus->caddr_tbl[codec->addr] = NULL;
1291 if (codec->patch_ops.free)
1292 codec->patch_ops.free(codec);
Takashi Iwai83012a72012-08-24 18:38:08 +02001293#ifdef CONFIG_PM
Takashi Iwai08fa20a2012-08-31 07:46:56 -07001294 if (!codec->pm_down_notified) /* cancel leftover refcounts */
Takashi Iwai68467f52012-08-28 09:14:29 -07001295 hda_call_pm_notify(codec->bus, false);
1296#endif
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001297 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +02001298 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001299 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001300 kfree(codec->vendor_name);
1301 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001302 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001303 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 kfree(codec);
1305}
1306
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001307static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec,
1308 hda_nid_t fg, unsigned int power_state);
1309
Takashi Iwaid8193872012-08-31 07:54:38 -07001310static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001311 unsigned int power_state);
1312
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313/**
1314 * snd_hda_codec_new - create a HDA codec
1315 * @bus: the bus to assign
1316 * @codec_addr: the codec address
1317 * @codecp: the pointer to store the generated codec
1318 *
1319 * Returns 0 if successful, or a negative error code.
1320 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001321int snd_hda_codec_new(struct hda_bus *bus,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001322 unsigned int codec_addr,
1323 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324{
1325 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001326 char component[31];
Takashi Iwaid8193872012-08-31 07:54:38 -07001327 hda_nid_t fg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 int err;
1329
Takashi Iwaida3cec32008-08-08 17:12:14 +02001330 if (snd_BUG_ON(!bus))
1331 return -EINVAL;
1332 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1333 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
1335 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001336 snd_printk(KERN_ERR "hda_codec: "
1337 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 return -EBUSY;
1339 }
1340
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001341 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 if (codec == NULL) {
1343 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1344 return -ENOMEM;
1345 }
1346
1347 codec->bus = bus;
1348 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001349 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001350 mutex_init(&codec->control_mutex);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001351 mutex_init(&codec->hash_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001352 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001353 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001354 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1355 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001356 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001357 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001358 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Stephen Warren7c935972011-06-01 11:14:17 -06001359 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai361dab32012-05-09 14:35:27 +02001360 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01001361 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001362 INIT_LIST_HEAD(&codec->conn_list);
1363
David Henningsson26a6cb62012-10-09 15:04:21 +02001364 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Takashi Iwai83012a72012-08-24 18:38:08 +02001366#ifdef CONFIG_PM
Takashi Iwai5536c6d2012-05-08 17:08:10 +02001367 spin_lock_init(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001368 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1369 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1370 * the caller has to power down appropriatley after initialization
1371 * phase.
1372 */
1373 hda_keep_power_on(codec);
Takashi Iwai68467f52012-08-28 09:14:29 -07001374 hda_call_pm_notify(bus, true);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001375#endif
1376
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001377 if (codec->bus->modelname) {
1378 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1379 if (!codec->modelname) {
1380 snd_hda_codec_free(codec);
1381 return -ENODEV;
1382 }
1383 }
1384
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 list_add_tail(&codec->list, &bus->codec_list);
1386 bus->caddr_tbl[codec_addr] = codec;
1387
Takashi Iwai0ba21762007-04-16 11:29:14 +02001388 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1389 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001390 if (codec->vendor_id == -1)
1391 /* read again, hopefully the access method was corrected
1392 * in the last read...
1393 */
1394 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1395 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001396 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1397 AC_PAR_SUBSYSTEM_ID);
1398 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1399 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001401 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001402 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001403 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001404 err = -ENODEV;
1405 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 }
1407
Takashi Iwaid8193872012-08-31 07:54:38 -07001408 fg = codec->afg ? codec->afg : codec->mfg;
1409 err = read_widget_caps(codec, fg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001410 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001411 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001412 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001413 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001414 err = read_pin_defaults(codec);
1415 if (err < 0)
1416 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001417
Takashi Iwai0ba21762007-04-16 11:29:14 +02001418 if (!codec->subsystem_id) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001419 codec->subsystem_id =
Takashi Iwaid8193872012-08-31 07:54:38 -07001420 snd_hda_codec_read(codec, fg, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001421 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001422 }
1423
Takashi Iwai83012a72012-08-24 18:38:08 +02001424#ifdef CONFIG_PM
Takashi Iwaid8193872012-08-31 07:54:38 -07001425 codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg,
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001426 AC_PWRST_CLKSTOP);
1427 if (!codec->d3_stop_clk)
1428 bus->power_keep_link_on = 1;
Mengdong Lin5d6147f2012-08-24 12:06:30 +08001429#endif
Takashi Iwaid8193872012-08-31 07:54:38 -07001430 codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
Takashi Iwai983f6b92012-08-28 09:18:01 -07001431 AC_PWRST_EPSS);
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001432
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001433 /* power-up all before initialization */
Takashi Iwaid8193872012-08-31 07:54:38 -07001434 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001435
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001436 snd_hda_codec_proc_new(codec);
1437
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001438 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001439
1440 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1441 codec->subsystem_id, codec->revision_id);
1442 snd_component_add(codec->bus->card, component);
1443
1444 if (codecp)
1445 *codecp = codec;
1446 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001447
1448 error:
1449 snd_hda_codec_free(codec);
1450 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001451}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001452EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001453
Mengdong Lina15d05d2013-02-08 17:09:31 -05001454int snd_hda_codec_update_widgets(struct hda_codec *codec)
1455{
1456 hda_nid_t fg;
1457 int err;
1458
1459 /* Assume the function group node does not change,
1460 * only the widget nodes may change.
1461 */
1462 kfree(codec->wcaps);
1463 fg = codec->afg ? codec->afg : codec->mfg;
1464 err = read_widget_caps(codec, fg);
1465 if (err < 0) {
1466 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
1467 return err;
1468 }
1469
1470 snd_array_free(&codec->init_pins);
1471 err = read_pin_defaults(codec);
1472
1473 return err;
1474}
1475EXPORT_SYMBOL_HDA(snd_hda_codec_update_widgets);
1476
1477
Takashi Iwaid5191e52009-11-16 14:58:17 +01001478/**
1479 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1480 * @codec: the HDA codec
1481 *
1482 * Start parsing of the given codec tree and (re-)initialize the whole
1483 * patch instance.
1484 *
1485 * Returns 0 if successful or a negative error code.
1486 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001487int snd_hda_codec_configure(struct hda_codec *codec)
1488{
1489 int err;
1490
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001491 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001492 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001493 err = get_codec_name(codec);
1494 if (err < 0)
1495 return err;
1496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
Takashi Iwai82467612007-07-27 19:15:54 +02001498 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001500 goto patched;
1501 }
Takashi Iwai82467612007-07-27 19:15:54 +02001502 if (codec->preset && codec->preset->patch) {
1503 err = codec->preset->patch(codec);
1504 goto patched;
1505 }
1506
1507 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001508 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001509 if (err < 0)
1510 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001511
1512 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001513 if (!err && codec->patch_ops.unsol_event)
1514 err = init_unsol_queue(codec->bus);
Takashi Iwaif62faed2009-12-23 09:27:51 +01001515 /* audio codec should override the mixer name */
1516 if (!err && (codec->afg || !*codec->bus->card->mixername))
1517 snprintf(codec->bus->card->mixername,
1518 sizeof(codec->bus->card->mixername),
1519 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001520 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001522EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
Takashi Iwaied360812012-08-08 17:12:52 +02001524/* update the stream-id if changed */
1525static void update_pcm_stream_id(struct hda_codec *codec,
1526 struct hda_cvt_setup *p, hda_nid_t nid,
1527 u32 stream_tag, int channel_id)
1528{
1529 unsigned int oldval, newval;
1530
1531 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1532 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1533 newval = (stream_tag << 4) | channel_id;
1534 if (oldval != newval)
1535 snd_hda_codec_write(codec, nid, 0,
1536 AC_VERB_SET_CHANNEL_STREAMID,
1537 newval);
1538 p->stream_tag = stream_tag;
1539 p->channel_id = channel_id;
1540 }
1541}
1542
1543/* update the format-id if changed */
1544static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1545 hda_nid_t nid, int format)
1546{
1547 unsigned int oldval;
1548
1549 if (p->format_id != format) {
1550 oldval = snd_hda_codec_read(codec, nid, 0,
1551 AC_VERB_GET_STREAM_FORMAT, 0);
1552 if (oldval != format) {
1553 msleep(1);
1554 snd_hda_codec_write(codec, nid, 0,
1555 AC_VERB_SET_STREAM_FORMAT,
1556 format);
1557 }
1558 p->format_id = format;
1559 }
1560}
1561
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562/**
1563 * snd_hda_codec_setup_stream - set up the codec for streaming
1564 * @codec: the CODEC to set up
1565 * @nid: the NID to set up
1566 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1567 * @channel_id: channel id to pass, zero based.
1568 * @format: stream format.
1569 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001570void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1571 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 int channel_id, int format)
1573{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001574 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001575 struct hda_cvt_setup *p;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001576 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001577 int i;
1578
Takashi Iwai0ba21762007-04-16 11:29:14 +02001579 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001580 return;
1581
Takashi Iwai0ba21762007-04-16 11:29:14 +02001582 snd_printdd("hda_codec_setup_stream: "
1583 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001585 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001586 if (!p)
Takashi Iwaieb541332010-08-06 13:48:11 +02001587 return;
Takashi Iwaied360812012-08-08 17:12:52 +02001588
1589 if (codec->pcm_format_first)
1590 update_pcm_format(codec, p, nid, format);
1591 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1592 if (!codec->pcm_format_first)
1593 update_pcm_format(codec, p, nid, format);
1594
Takashi Iwaieb541332010-08-06 13:48:11 +02001595 p->active = 1;
1596 p->dirty = 0;
1597
1598 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001599 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001600 list_for_each_entry(c, &codec->bus->codec_list, list) {
1601 for (i = 0; i < c->cvt_setups.used; i++) {
1602 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001603 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001604 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001605 p->dirty = 1;
1606 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001609EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
Takashi Iwaif0cea792010-08-13 11:56:53 +02001611static void really_cleanup_stream(struct hda_codec *codec,
1612 struct hda_cvt_setup *q);
1613
Takashi Iwaid5191e52009-11-16 14:58:17 +01001614/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001615 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001616 * @codec: the CODEC to clean up
1617 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001618 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001619 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001620void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1621 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001622{
Takashi Iwaieb541332010-08-06 13:48:11 +02001623 struct hda_cvt_setup *p;
1624
Takashi Iwai888afa12008-03-18 09:57:50 +01001625 if (!nid)
1626 return;
1627
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001628 if (codec->no_sticky_stream)
1629 do_now = 1;
1630
Takashi Iwai888afa12008-03-18 09:57:50 +01001631 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001632 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001633 if (p) {
Takashi Iwaif0cea792010-08-13 11:56:53 +02001634 /* here we just clear the active flag when do_now isn't set;
1635 * actual clean-ups will be done later in
1636 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1637 */
1638 if (do_now)
1639 really_cleanup_stream(codec, p);
1640 else
1641 p->active = 0;
1642 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001643}
Takashi Iwaif0cea792010-08-13 11:56:53 +02001644EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001645
Takashi Iwaieb541332010-08-06 13:48:11 +02001646static void really_cleanup_stream(struct hda_codec *codec,
1647 struct hda_cvt_setup *q)
1648{
1649 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001650 if (q->stream_tag || q->channel_id)
1651 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1652 if (q->format_id)
1653 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1654);
Takashi Iwaieb541332010-08-06 13:48:11 +02001655 memset(q, 0, sizeof(*q));
1656 q->nid = nid;
1657}
1658
1659/* clean up the all conflicting obsolete streams */
1660static void purify_inactive_streams(struct hda_codec *codec)
1661{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001662 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001663 int i;
1664
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001665 list_for_each_entry(c, &codec->bus->codec_list, list) {
1666 for (i = 0; i < c->cvt_setups.used; i++) {
1667 struct hda_cvt_setup *p;
1668 p = snd_array_elem(&c->cvt_setups, i);
1669 if (p->dirty)
1670 really_cleanup_stream(c, p);
1671 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001672 }
1673}
1674
Takashi Iwai2a439522011-07-26 09:52:50 +02001675#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001676/* clean up all streams; called from suspend */
1677static void hda_cleanup_all_streams(struct hda_codec *codec)
1678{
1679 int i;
1680
1681 for (i = 0; i < codec->cvt_setups.used; i++) {
1682 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1683 if (p->stream_tag)
1684 really_cleanup_stream(codec, p);
1685 }
1686}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001687#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001688
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689/*
1690 * amp access functions
1691 */
1692
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001693/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001694#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001695#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001696#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1697#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001699#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
1701/* initialize the hash table */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001702static void init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001703 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704{
Takashi Iwai01751f52007-08-10 16:59:39 +02001705 memset(cache, 0, sizeof(*cache));
1706 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001707 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001708}
1709
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001710static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001711{
Takashi Iwai603c4012008-07-30 15:01:44 +02001712 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713}
1714
1715/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001716static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717{
Takashi Iwai01751f52007-08-10 16:59:39 +02001718 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1719 u16 cur = cache->hash[idx];
1720 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
1722 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001723 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 if (info->key == key)
1725 return info;
1726 cur = info->next;
1727 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001728 return NULL;
1729}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001731/* query the hash. allocate an entry if not found. */
1732static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1733 u32 key)
1734{
1735 struct hda_cache_head *info = get_hash(cache, key);
1736 if (!info) {
1737 u16 idx, cur;
1738 /* add a new hash entry */
1739 info = snd_array_new(&cache->buf);
1740 if (!info)
1741 return NULL;
1742 cur = snd_array_index(&cache->buf, info);
1743 info->key = key;
1744 info->val = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001745 info->dirty = 0;
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001746 idx = key % (u16)ARRAY_SIZE(cache->hash);
1747 info->next = cache->hash[idx];
1748 cache->hash[idx] = cur;
1749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 return info;
1751}
1752
Takashi Iwai01751f52007-08-10 16:59:39 +02001753/* query and allocate an amp hash entry */
1754static inline struct hda_amp_info *
1755get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1756{
1757 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1758}
1759
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001760/* overwrite the value with the key in the caps hash */
1761static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1762{
1763 struct hda_amp_info *info;
1764
1765 mutex_lock(&codec->hash_mutex);
1766 info = get_alloc_amp_hash(codec, key);
1767 if (!info) {
1768 mutex_unlock(&codec->hash_mutex);
1769 return -EINVAL;
1770 }
1771 info->amp_caps = val;
1772 info->head.val |= INFO_AMP_CAPS;
1773 mutex_unlock(&codec->hash_mutex);
1774 return 0;
1775}
1776
1777/* query the value from the caps hash; if not found, fetch the current
1778 * value from the given function and store in the hash
1779 */
1780static unsigned int
1781query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1782 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1783{
1784 struct hda_amp_info *info;
1785 unsigned int val;
1786
1787 mutex_lock(&codec->hash_mutex);
1788 info = get_alloc_amp_hash(codec, key);
1789 if (!info) {
1790 mutex_unlock(&codec->hash_mutex);
1791 return 0;
1792 }
1793 if (!(info->head.val & INFO_AMP_CAPS)) {
1794 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1795 val = func(codec, nid, dir);
1796 write_caps_hash(codec, key, val);
1797 } else {
1798 val = info->amp_caps;
1799 mutex_unlock(&codec->hash_mutex);
1800 }
1801 return val;
1802}
1803
1804static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1805 int direction)
1806{
1807 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1808 nid = codec->afg;
1809 return snd_hda_param_read(codec, nid,
1810 direction == HDA_OUTPUT ?
1811 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1812}
1813
Takashi Iwaid5191e52009-11-16 14:58:17 +01001814/**
1815 * query_amp_caps - query AMP capabilities
1816 * @codec: the HD-auio codec
1817 * @nid: the NID to query
1818 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1819 *
1820 * Query AMP capabilities for the given widget and direction.
1821 * Returns the obtained capability bits.
1822 *
1823 * When cap bits have been already read, this doesn't read again but
1824 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001826u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001828 return query_caps_hash(codec, nid, direction,
1829 HDA_HASH_KEY(nid, direction, 0),
1830 read_amp_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001832EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
Takashi Iwaid5191e52009-11-16 14:58:17 +01001834/**
1835 * snd_hda_override_amp_caps - Override the AMP capabilities
1836 * @codec: the CODEC to clean up
1837 * @nid: the NID to clean up
1838 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1839 * @caps: the capability bits to set
1840 *
1841 * Override the cached AMP caps bits value by the given one.
1842 * This function is useful if the driver needs to adjust the AMP ranges,
1843 * e.g. limit to 0dB, etc.
1844 *
1845 * Returns zero if successful or a negative error code.
1846 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001847int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1848 unsigned int caps)
1849{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001850 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001851}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001852EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001853
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001854static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
1855 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001856{
1857 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1858}
1859
Takashi Iwaid5191e52009-11-16 14:58:17 +01001860/**
1861 * snd_hda_query_pin_caps - Query PIN capabilities
1862 * @codec: the HD-auio codec
1863 * @nid: the NID to query
1864 *
1865 * Query PIN capabilities for the given widget.
1866 * Returns the obtained capability bits.
1867 *
1868 * When cap bits have been already read, this doesn't read again but
1869 * returns the cached value.
1870 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001871u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1872{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001873 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001874 read_pin_cap);
1875}
Takashi Iwai1327a322009-03-23 13:07:47 +01001876EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1877
Wu Fengguang864f92b2009-11-18 12:38:02 +08001878/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02001879 * snd_hda_override_pin_caps - Override the pin capabilities
1880 * @codec: the CODEC
1881 * @nid: the NID to override
1882 * @caps: the capability bits to set
1883 *
1884 * Override the cached PIN capabilitiy bits value by the given one.
1885 *
1886 * Returns zero if successful or a negative error code.
1887 */
1888int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1889 unsigned int caps)
1890{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001891 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02001892}
1893EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps);
1894
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001895/* read or sync the hash value with the current value;
1896 * call within hash_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 */
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001898static struct hda_amp_info *
1899update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
Takashi Iwai280e57d2012-12-14 10:32:21 +01001900 int direction, int index, bool init_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001902 struct hda_amp_info *info;
1903 unsigned int parm, val = 0;
1904 bool val_read = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001906 retry:
1907 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1908 if (!info)
1909 return NULL;
1910 if (!(info->head.val & INFO_AMP_VOL(ch))) {
1911 if (!val_read) {
1912 mutex_unlock(&codec->hash_mutex);
1913 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1914 parm |= direction == HDA_OUTPUT ?
1915 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1916 parm |= index;
1917 val = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001918 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001919 val &= 0xff;
1920 val_read = true;
1921 mutex_lock(&codec->hash_mutex);
1922 goto retry;
1923 }
1924 info->vol[ch] = val;
1925 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001926 } else if (init_only)
1927 return NULL;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001928 return info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929}
1930
1931/*
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001932 * write the current volume in info to the h/w
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 */
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001934static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001935 hda_nid_t nid, int ch, int direction, int index,
1936 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937{
1938 u32 parm;
1939
1940 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1941 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1942 parm |= index << AC_AMP_SET_INDEX_SHIFT;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001943 if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) &&
1944 (amp_caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwai38681372012-02-27 15:00:58 +01001945 ; /* set the zero value as a fake mute */
1946 else
1947 parm |= val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1949}
1950
Takashi Iwaid5191e52009-11-16 14:58:17 +01001951/**
1952 * snd_hda_codec_amp_read - Read AMP value
1953 * @codec: HD-audio codec
1954 * @nid: NID to read the AMP value
1955 * @ch: channel (left=0 or right=1)
1956 * @direction: #HDA_INPUT or #HDA_OUTPUT
1957 * @index: the index value (only for input direction)
1958 *
1959 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 */
Takashi Iwai834be882006-03-01 14:16:17 +01001961int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1962 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001964 struct hda_amp_info *info;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001965 unsigned int val = 0;
1966
1967 mutex_lock(&codec->hash_mutex);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001968 info = update_amp_hash(codec, nid, ch, direction, index, false);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001969 if (info)
1970 val = info->vol[ch];
1971 mutex_unlock(&codec->hash_mutex);
1972 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001974EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
Takashi Iwai280e57d2012-12-14 10:32:21 +01001976static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1977 int direction, int idx, int mask, int val,
1978 bool init_only)
1979{
1980 struct hda_amp_info *info;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001981 unsigned int caps;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01001982 unsigned int cache_only;
Takashi Iwai280e57d2012-12-14 10:32:21 +01001983
1984 if (snd_BUG_ON(mask & ~0xff))
1985 mask &= 0xff;
1986 val &= mask;
1987
1988 mutex_lock(&codec->hash_mutex);
1989 info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
1990 if (!info) {
1991 mutex_unlock(&codec->hash_mutex);
1992 return 0;
1993 }
1994 val |= info->vol[ch] & ~mask;
1995 if (info->vol[ch] == val) {
1996 mutex_unlock(&codec->hash_mutex);
1997 return 0;
1998 }
1999 info->vol[ch] = val;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01002000 cache_only = info->head.dirty = codec->cached_write;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002001 caps = info->amp_caps;
Takashi Iwai280e57d2012-12-14 10:32:21 +01002002 mutex_unlock(&codec->hash_mutex);
Takashi Iwaide1e37b2012-12-20 11:00:21 +01002003 if (!cache_only)
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002004 put_vol_mute(codec, caps, nid, ch, direction, idx, val);
Takashi Iwai280e57d2012-12-14 10:32:21 +01002005 return 1;
2006}
2007
Takashi Iwaid5191e52009-11-16 14:58:17 +01002008/**
2009 * snd_hda_codec_amp_update - update the AMP value
2010 * @codec: HD-audio codec
2011 * @nid: NID to read the AMP value
2012 * @ch: channel (left=0 or right=1)
2013 * @direction: #HDA_INPUT or #HDA_OUTPUT
2014 * @idx: the index value (only for input direction)
2015 * @mask: bit mask to set
2016 * @val: the bits value to set
2017 *
2018 * Update the AMP value with a bit mask.
2019 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002020 */
Takashi Iwai834be882006-03-01 14:16:17 +01002021int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
2022 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023{
Takashi Iwai280e57d2012-12-14 10:32:21 +01002024 return codec_amp_update(codec, nid, ch, direction, idx, mask, val, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002026EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
Takashi Iwaid5191e52009-11-16 14:58:17 +01002028/**
2029 * snd_hda_codec_amp_stereo - update the AMP stereo values
2030 * @codec: HD-audio codec
2031 * @nid: NID to read the AMP value
2032 * @direction: #HDA_INPUT or #HDA_OUTPUT
2033 * @idx: the index value (only for input direction)
2034 * @mask: bit mask to set
2035 * @val: the bits value to set
2036 *
2037 * Update the AMP values like snd_hda_codec_amp_update(), but for a
2038 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02002039 */
2040int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
2041 int direction, int idx, int mask, int val)
2042{
2043 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02002044
2045 if (snd_BUG_ON(mask & ~0xff))
2046 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02002047 for (ch = 0; ch < 2; ch++)
2048 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
2049 idx, mask, val);
2050 return ret;
2051}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002052EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02002053
Takashi Iwai280e57d2012-12-14 10:32:21 +01002054/* Works like snd_hda_codec_amp_update() but it writes the value only at
2055 * the first access. If the amp was already initialized / updated beforehand,
2056 * this does nothing.
2057 */
2058int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
2059 int dir, int idx, int mask, int val)
2060{
2061 return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true);
2062}
2063EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init);
2064
2065int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
2066 int dir, int idx, int mask, int val)
2067{
2068 int ch, ret = 0;
2069
2070 if (snd_BUG_ON(mask & ~0xff))
2071 mask &= 0xff;
2072 for (ch = 0; ch < 2; ch++)
2073 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
2074 idx, mask, val);
2075 return ret;
2076}
2077EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init_stereo);
2078
Takashi Iwaid5191e52009-11-16 14:58:17 +01002079/**
2080 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
2081 * @codec: HD-audio codec
2082 *
2083 * Resume the all amp commands from the cache.
2084 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002085void snd_hda_codec_resume_amp(struct hda_codec *codec)
2086{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002087 int i;
2088
Takashi Iwaic370dd62012-12-13 18:30:04 +01002089 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01002090 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002091 for (i = 0; i < codec->amp_cache.buf.used; i++) {
2092 struct hda_amp_info *buffer;
2093 u32 key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002094 hda_nid_t nid;
2095 unsigned int idx, dir, ch;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002096 struct hda_amp_info info;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002097
2098 buffer = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwai8565f052012-12-20 12:54:18 +01002099 if (!buffer->head.dirty)
2100 continue;
2101 buffer->head.dirty = 0;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002102 info = *buffer;
2103 key = info.head.key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002104 if (!key)
2105 continue;
2106 nid = key & 0xff;
2107 idx = (key >> 16) & 0xff;
2108 dir = (key >> 24) & 0xff;
2109 for (ch = 0; ch < 2; ch++) {
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002110 if (!(info.head.val & INFO_AMP_VOL(ch)))
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002111 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002112 mutex_unlock(&codec->hash_mutex);
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002113 put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx,
2114 info.vol[ch]);
Takashi Iwaic370dd62012-12-13 18:30:04 +01002115 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002116 }
2117 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01002118 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002119}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002120EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002122static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
2123 unsigned int ofs)
2124{
2125 u32 caps = query_amp_caps(codec, nid, dir);
2126 /* get num steps */
2127 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2128 if (ofs < caps)
2129 caps -= ofs;
2130 return caps;
2131}
2132
Takashi Iwaid5191e52009-11-16 14:58:17 +01002133/**
2134 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
2135 *
2136 * The control element is supposed to have the private_value field
2137 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2138 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002139int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
2140 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141{
2142 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2143 u16 nid = get_amp_nid(kcontrol);
2144 u8 chs = get_amp_channels(kcontrol);
2145 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002146 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002148 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2149 uinfo->count = chs == 3 ? 2 : 1;
2150 uinfo->value.integer.min = 0;
2151 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
2152 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002153 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01002154 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
2155 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 return -EINVAL;
2157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 return 0;
2159}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002160EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002162
2163static inline unsigned int
2164read_amp_value(struct hda_codec *codec, hda_nid_t nid,
2165 int ch, int dir, int idx, unsigned int ofs)
2166{
2167 unsigned int val;
2168 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
2169 val &= HDA_AMP_VOLMASK;
2170 if (val >= ofs)
2171 val -= ofs;
2172 else
2173 val = 0;
2174 return val;
2175}
2176
2177static inline int
2178update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2179 int ch, int dir, int idx, unsigned int ofs,
2180 unsigned int val)
2181{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002182 unsigned int maxval;
2183
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002184 if (val > 0)
2185 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02002186 /* ofs = 0: raw max value */
2187 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002188 if (val > maxval)
2189 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002190 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
2191 HDA_AMP_VOLMASK, val);
2192}
2193
Takashi Iwaid5191e52009-11-16 14:58:17 +01002194/**
2195 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
2196 *
2197 * The control element is supposed to have the private_value field
2198 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2199 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002200int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2201 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202{
2203 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2204 hda_nid_t nid = get_amp_nid(kcontrol);
2205 int chs = get_amp_channels(kcontrol);
2206 int dir = get_amp_direction(kcontrol);
2207 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002208 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 long *valp = ucontrol->value.integer.value;
2210
2211 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002212 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002214 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 return 0;
2216}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002217EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218
Takashi Iwaid5191e52009-11-16 14:58:17 +01002219/**
2220 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2221 *
2222 * The control element is supposed to have the private_value field
2223 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2224 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002225int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2226 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227{
2228 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2229 hda_nid_t nid = get_amp_nid(kcontrol);
2230 int chs = get_amp_channels(kcontrol);
2231 int dir = get_amp_direction(kcontrol);
2232 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002233 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 long *valp = ucontrol->value.integer.value;
2235 int change = 0;
2236
Takashi Iwaicb53c622007-08-10 17:21:45 +02002237 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002238 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002239 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002240 valp++;
2241 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002242 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002243 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002244 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 return change;
2246}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002247EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248
Takashi Iwaid5191e52009-11-16 14:58:17 +01002249/**
2250 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2251 *
2252 * The control element is supposed to have the private_value field
2253 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2254 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002255int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2256 unsigned int size, unsigned int __user *_tlv)
2257{
2258 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2259 hda_nid_t nid = get_amp_nid(kcontrol);
2260 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002261 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002262 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002263 u32 caps, val1, val2;
2264
2265 if (size < 4 * sizeof(unsigned int))
2266 return -ENOMEM;
2267 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002268 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2269 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002270 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002271 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002272 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01002273 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02002274 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002275 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2276 return -EFAULT;
2277 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2278 return -EFAULT;
2279 if (put_user(val1, _tlv + 2))
2280 return -EFAULT;
2281 if (put_user(val2, _tlv + 3))
2282 return -EFAULT;
2283 return 0;
2284}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002285EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002286
Takashi Iwaid5191e52009-11-16 14:58:17 +01002287/**
2288 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2289 * @codec: HD-audio codec
2290 * @nid: NID of a reference widget
2291 * @dir: #HDA_INPUT or #HDA_OUTPUT
2292 * @tlv: TLV data to be stored, at least 4 elements
2293 *
2294 * Set (static) TLV data for a virtual master volume using the AMP caps
2295 * obtained from the reference NID.
2296 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002297 */
2298void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2299 unsigned int *tlv)
2300{
2301 u32 caps;
2302 int nums, step;
2303
2304 caps = query_amp_caps(codec, nid, dir);
2305 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2306 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2307 step = (step + 1) * 25;
2308 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2309 tlv[1] = 2 * sizeof(unsigned int);
2310 tlv[2] = -nums * step;
2311 tlv[3] = step;
2312}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002313EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002314
2315/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002316static struct snd_kcontrol *
Takashi Iwaidcda5802012-10-12 17:24:51 +02002317find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002318{
2319 struct snd_ctl_elem_id id;
2320 memset(&id, 0, sizeof(id));
2321 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002322 id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01002323 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002324 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2325 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002326 strcpy(id.name, name);
2327 return snd_ctl_find_id(codec->bus->card, &id);
2328}
2329
Takashi Iwaid5191e52009-11-16 14:58:17 +01002330/**
2331 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2332 * @codec: HD-audio codec
2333 * @name: ctl id name string
2334 *
2335 * Get the control element with the given id string and IFACE_MIXER.
2336 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002337struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2338 const char *name)
2339{
Takashi Iwaidcda5802012-10-12 17:24:51 +02002340 return find_mixer_ctl(codec, name, 0, 0);
Takashi Iwai09f99702008-02-04 12:31:13 +01002341}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002342EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002343
Takashi Iwaidcda5802012-10-12 17:24:51 +02002344static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002345 int start_idx)
Takashi Iwai1afe2062010-12-23 10:17:52 +01002346{
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002347 int i, idx;
2348 /* 16 ctlrs should be large enough */
2349 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
2350 if (!find_mixer_ctl(codec, name, 0, idx))
Takashi Iwai1afe2062010-12-23 10:17:52 +01002351 return idx;
2352 }
2353 return -EBUSY;
2354}
2355
Takashi Iwaid5191e52009-11-16 14:58:17 +01002356/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002357 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002358 * @codec: HD-audio codec
2359 * @nid: corresponding NID (optional)
2360 * @kctl: the control element to assign
2361 *
2362 * Add the given control element to an array inside the codec instance.
2363 * All control elements belonging to a codec are supposed to be added
2364 * by this function so that a proper clean-up works at the free or
2365 * reconfiguration time.
2366 *
2367 * If non-zero @nid is passed, the NID is assigned to the control element.
2368 * The assignment is shown in the codec proc file.
2369 *
2370 * snd_hda_ctl_add() checks the control subdev id field whether
2371 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002372 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2373 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002374 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002375int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2376 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002377{
2378 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002379 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002380 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002381
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002382 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002383 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002384 if (nid == 0)
2385 nid = get_amp_nid_(kctl->private_value);
2386 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002387 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2388 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002389 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002390 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002391 err = snd_ctl_add(codec->bus->card, kctl);
2392 if (err < 0)
2393 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002394 item = snd_array_new(&codec->mixers);
2395 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002396 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002397 item->kctl = kctl;
2398 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002399 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002400 return 0;
2401}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002402EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002403
Takashi Iwaid5191e52009-11-16 14:58:17 +01002404/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002405 * snd_hda_add_nid - Assign a NID to a control element
2406 * @codec: HD-audio codec
2407 * @nid: corresponding NID (optional)
2408 * @kctl: the control element to assign
2409 * @index: index to kctl
2410 *
2411 * Add the given control element to an array inside the codec instance.
2412 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2413 * NID:KCTL mapping - for example "Capture Source" selector.
2414 */
2415int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2416 unsigned int index, hda_nid_t nid)
2417{
2418 struct hda_nid_item *item;
2419
2420 if (nid > 0) {
2421 item = snd_array_new(&codec->nids);
2422 if (!item)
2423 return -ENOMEM;
2424 item->kctl = kctl;
2425 item->index = index;
2426 item->nid = nid;
2427 return 0;
2428 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01002429 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2430 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002431 return -EINVAL;
2432}
2433EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2434
2435/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002436 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2437 * @codec: HD-audio codec
2438 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002439void snd_hda_ctls_clear(struct hda_codec *codec)
2440{
2441 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002442 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002443 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002444 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002445 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002446 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002447}
2448
Takashi Iwaia65d6292009-02-23 16:57:04 +01002449/* pseudo device locking
2450 * toggle card->shutdown to allow/disallow the device access (as a hack)
2451 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002452int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002453{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002454 struct snd_card *card = bus->card;
2455 struct hda_codec *codec;
2456
Takashi Iwaia65d6292009-02-23 16:57:04 +01002457 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002458 if (card->shutdown)
2459 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002460 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002461 if (!list_empty(&card->ctl_files))
2462 goto err_clear;
2463
2464 list_for_each_entry(codec, &bus->codec_list, list) {
2465 int pcm;
2466 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2467 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2468 if (!cpcm->pcm)
2469 continue;
2470 if (cpcm->pcm->streams[0].substream_opened ||
2471 cpcm->pcm->streams[1].substream_opened)
2472 goto err_clear;
2473 }
2474 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01002475 spin_unlock(&card->files_lock);
2476 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002477
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002478 err_clear:
2479 card->shutdown = 0;
2480 err_unlock:
2481 spin_unlock(&card->files_lock);
2482 return -EINVAL;
2483}
2484EXPORT_SYMBOL_HDA(snd_hda_lock_devices);
2485
2486void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002487{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002488 struct snd_card *card = bus->card;
2489
2490 card = bus->card;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002491 spin_lock(&card->files_lock);
2492 card->shutdown = 0;
2493 spin_unlock(&card->files_lock);
2494}
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002495EXPORT_SYMBOL_HDA(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002496
Takashi Iwaid5191e52009-11-16 14:58:17 +01002497/**
2498 * snd_hda_codec_reset - Clear all objects assigned to the codec
2499 * @codec: HD-audio codec
2500 *
2501 * This frees the all PCM and control elements assigned to the codec, and
2502 * clears the caches and restores the pin default configurations.
2503 *
2504 * When a device is being used, it returns -EBSY. If successfully freed,
2505 * returns zero.
2506 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002507int snd_hda_codec_reset(struct hda_codec *codec)
2508{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002509 struct hda_bus *bus = codec->bus;
2510 struct snd_card *card = bus->card;
2511 int i;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002512
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002513 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002514 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002515
2516 /* OK, let it free */
David Henningsson26a6cb62012-10-09 15:04:21 +02002517 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai83012a72012-08-24 18:38:08 +02002518#ifdef CONFIG_PM
Takashi Iwaia2d96e72012-05-09 12:36:22 +02002519 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwai339876d2012-05-08 16:57:12 +02002520 codec->power_on = 0;
2521 codec->power_transition = 0;
2522 codec->power_jiffies = jiffies;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002523 flush_workqueue(bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002524#endif
2525 snd_hda_ctls_clear(codec);
2526 /* relase PCMs */
2527 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002528 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002529 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002530 clear_bit(codec->pcm_info[i].device,
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002531 bus->pcm_dev_bits);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002532 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002533 }
2534 if (codec->patch_ops.free)
2535 codec->patch_ops.free(codec);
Takashi Iwai07dc59f2012-09-10 09:39:31 +02002536 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002537 snd_hda_jack_tbl_clear(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002538 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002539 codec->spec = NULL;
2540 free_hda_cache(&codec->amp_cache);
2541 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002542 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2543 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002544 /* free only driver_pins so that init_pins + user_pins are restored */
2545 snd_array_free(&codec->driver_pins);
Takashi Iwai09a60712012-06-26 15:01:33 +02002546 snd_array_free(&codec->cvt_setups);
2547 snd_array_free(&codec->spdif_out);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01002548 snd_array_free(&codec->verbs);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002549 codec->num_pcms = 0;
2550 codec->pcm_info = NULL;
2551 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002552 codec->slave_dig_outs = NULL;
2553 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002554 module_put(codec->owner);
2555 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002556
2557 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002558 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002559 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002560}
2561
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002562typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2563
2564/* apply the function to all matching slave ctls in the mixer list */
2565static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002566 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002567{
2568 struct hda_nid_item *items;
2569 const char * const *s;
2570 int i, err;
2571
2572 items = codec->mixers.list;
2573 for (i = 0; i < codec->mixers.used; i++) {
2574 struct snd_kcontrol *sctl = items[i].kctl;
2575 if (!sctl || !sctl->id.name ||
2576 sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2577 continue;
2578 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002579 char tmpname[sizeof(sctl->id.name)];
2580 const char *name = *s;
2581 if (suffix) {
2582 snprintf(tmpname, sizeof(tmpname), "%s %s",
2583 name, suffix);
2584 name = tmpname;
2585 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002586 if (!strcmp(sctl->id.name, name)) {
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002587 err = func(data, sctl);
2588 if (err)
2589 return err;
2590 break;
2591 }
2592 }
2593 }
2594 return 0;
2595}
2596
2597static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2598{
2599 return 1;
2600}
2601
Takashi Iwai18478e82012-03-09 17:51:10 +01002602/* guess the value corresponding to 0dB */
2603static int get_kctl_0dB_offset(struct snd_kcontrol *kctl)
2604{
2605 int _tlv[4];
2606 const int *tlv = NULL;
2607 int val = -1;
2608
2609 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2610 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2611 mm_segment_t fs = get_fs();
2612 set_fs(get_ds());
2613 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2614 tlv = _tlv;
2615 set_fs(fs);
2616 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2617 tlv = kctl->tlv.p;
2618 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE)
2619 val = -tlv[2] / tlv[3];
2620 return val;
2621}
2622
2623/* call kctl->put with the given value(s) */
2624static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2625{
2626 struct snd_ctl_elem_value *ucontrol;
2627 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2628 if (!ucontrol)
2629 return -ENOMEM;
2630 ucontrol->value.integer.value[0] = val;
2631 ucontrol->value.integer.value[1] = val;
2632 kctl->put(kctl, ucontrol);
2633 kfree(ucontrol);
2634 return 0;
2635}
2636
2637/* initialize the slave volume with 0dB */
2638static int init_slave_0dB(void *data, struct snd_kcontrol *slave)
2639{
2640 int offset = get_kctl_0dB_offset(slave);
2641 if (offset > 0)
2642 put_kctl_with_value(slave, offset);
2643 return 0;
2644}
2645
2646/* unmute the slave */
2647static int init_slave_unmute(void *data, struct snd_kcontrol *slave)
2648{
2649 return put_kctl_with_value(slave, 1);
2650}
2651
Takashi Iwaid5191e52009-11-16 14:58:17 +01002652/**
2653 * snd_hda_add_vmaster - create a virtual master control and add slaves
2654 * @codec: HD-audio codec
2655 * @name: vmaster control name
2656 * @tlv: TLV data (optional)
2657 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002658 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002659 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002660 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002661 *
2662 * Create a virtual master control with the given name. The TLV data
2663 * must be either NULL or a valid data.
2664 *
2665 * @slaves is a NULL-terminated array of strings, each of which is a
2666 * slave control name. All controls with these names are assigned to
2667 * the new virtual master control.
2668 *
2669 * This function returns zero if successful or a negative error code.
2670 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002671int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002672 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002673 const char *suffix, bool init_slave_vol,
2674 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002675{
2676 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002677 int err;
2678
Takashi Iwai29e58532012-03-12 12:25:03 +01002679 if (ctl_ret)
2680 *ctl_ret = NULL;
2681
Takashi Iwai9322ca52012-02-03 14:28:01 +01002682 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002683 if (err != 1) {
Takashi Iwai2f085542008-02-22 18:43:50 +01002684 snd_printdd("No slave found for %s\n", name);
2685 return 0;
2686 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002687 kctl = snd_ctl_make_virtual_master(name, tlv);
2688 if (!kctl)
2689 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002690 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002691 if (err < 0)
2692 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002693
Takashi Iwai9322ca52012-02-03 14:28:01 +01002694 err = map_slaves(codec, slaves, suffix,
2695 (map_slave_func_t)snd_ctl_add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002696 if (err < 0)
2697 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002698
2699 /* init with master mute & zero volume */
2700 put_kctl_with_value(kctl, 0);
2701 if (init_slave_vol)
2702 map_slaves(codec, slaves, suffix,
2703 tlv ? init_slave_0dB : init_slave_unmute, kctl);
2704
Takashi Iwai29e58532012-03-12 12:25:03 +01002705 if (ctl_ret)
2706 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002707 return 0;
2708}
Takashi Iwai18478e82012-03-09 17:51:10 +01002709EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002710
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002711/*
2712 * mute-LED control using vmaster
2713 */
2714static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2715 struct snd_ctl_elem_info *uinfo)
2716{
2717 static const char * const texts[] = {
David Henningssonc86c2d42013-01-03 14:12:29 +01002718 "On", "Off", "Follow Master"
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002719 };
2720 unsigned int index;
2721
2722 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2723 uinfo->count = 1;
2724 uinfo->value.enumerated.items = 3;
2725 index = uinfo->value.enumerated.item;
2726 if (index >= 3)
2727 index = 2;
2728 strcpy(uinfo->value.enumerated.name, texts[index]);
2729 return 0;
2730}
2731
2732static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2733 struct snd_ctl_elem_value *ucontrol)
2734{
2735 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2736 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2737 return 0;
2738}
2739
2740static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2741 struct snd_ctl_elem_value *ucontrol)
2742{
2743 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2744 unsigned int old_mode = hook->mute_mode;
2745
2746 hook->mute_mode = ucontrol->value.enumerated.item[0];
2747 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2748 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2749 if (old_mode == hook->mute_mode)
2750 return 0;
2751 snd_hda_sync_vmaster_hook(hook);
2752 return 1;
2753}
2754
2755static struct snd_kcontrol_new vmaster_mute_mode = {
2756 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2757 .name = "Mute-LED Mode",
2758 .info = vmaster_mute_mode_info,
2759 .get = vmaster_mute_mode_get,
2760 .put = vmaster_mute_mode_put,
2761};
2762
2763/*
2764 * Add a mute-LED hook with the given vmaster switch kctl
2765 * "Mute-LED Mode" control is automatically created and associated with
2766 * the given hook.
2767 */
2768int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735c2012-03-13 07:55:10 +01002769 struct hda_vmaster_mute_hook *hook,
2770 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002771{
2772 struct snd_kcontrol *kctl;
2773
2774 if (!hook->hook || !hook->sw_kctl)
2775 return 0;
2776 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2777 hook->codec = codec;
2778 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735c2012-03-13 07:55:10 +01002779 if (!expose_enum_ctl)
2780 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002781 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2782 if (!kctl)
2783 return -ENOMEM;
2784 return snd_hda_ctl_add(codec, 0, kctl);
2785}
2786EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook);
2787
2788/*
2789 * Call the hook with the current value for synchronization
2790 * Should be called in init callback
2791 */
2792void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2793{
2794 if (!hook->hook || !hook->codec)
2795 return;
Takashi Iwai594813f2013-04-17 18:16:05 +02002796 /* don't call vmaster hook in the destructor since it might have
2797 * been already destroyed
2798 */
2799 if (hook->codec->bus->shutdown)
2800 return;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002801 switch (hook->mute_mode) {
2802 case HDA_VMUTE_FOLLOW_MASTER:
2803 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2804 break;
2805 default:
2806 hook->hook(hook->codec, hook->mute_mode);
2807 break;
2808 }
2809}
2810EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook);
2811
2812
Takashi Iwaid5191e52009-11-16 14:58:17 +01002813/**
2814 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2815 *
2816 * The control element is supposed to have the private_value field
2817 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2818 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002819int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2820 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821{
2822 int chs = get_amp_channels(kcontrol);
2823
2824 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2825 uinfo->count = chs == 3 ? 2 : 1;
2826 uinfo->value.integer.min = 0;
2827 uinfo->value.integer.max = 1;
2828 return 0;
2829}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002830EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831
Takashi Iwaid5191e52009-11-16 14:58:17 +01002832/**
2833 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2834 *
2835 * The control element is supposed to have the private_value field
2836 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2837 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002838int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2839 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840{
2841 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2842 hda_nid_t nid = get_amp_nid(kcontrol);
2843 int chs = get_amp_channels(kcontrol);
2844 int dir = get_amp_direction(kcontrol);
2845 int idx = get_amp_index(kcontrol);
2846 long *valp = ucontrol->value.integer.value;
2847
2848 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002849 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002850 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002852 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002853 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 return 0;
2855}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002856EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857
Takashi Iwaid5191e52009-11-16 14:58:17 +01002858/**
2859 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2860 *
2861 * The control element is supposed to have the private_value field
2862 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2863 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002864int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2865 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866{
2867 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2868 hda_nid_t nid = get_amp_nid(kcontrol);
2869 int chs = get_amp_channels(kcontrol);
2870 int dir = get_amp_direction(kcontrol);
2871 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 long *valp = ucontrol->value.integer.value;
2873 int change = 0;
2874
Takashi Iwaicb53c622007-08-10 17:21:45 +02002875 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002876 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002877 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002878 HDA_AMP_MUTE,
2879 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002880 valp++;
2881 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002882 if (chs & 2)
2883 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002884 HDA_AMP_MUTE,
2885 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002886 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002887 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 return change;
2889}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002890EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891
2892/*
Takashi Iwai985be542005-11-02 18:26:49 +01002893 * bound volume controls
2894 *
2895 * bind multiple volumes (# indices, from 0)
2896 */
2897
2898#define AMP_VAL_IDX_SHIFT 19
2899#define AMP_VAL_IDX_MASK (0x0f<<19)
2900
Takashi Iwaid5191e52009-11-16 14:58:17 +01002901/**
2902 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2903 *
2904 * The control element is supposed to have the private_value field
2905 * set up via HDA_BIND_MUTE*() macros.
2906 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002907int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2908 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002909{
2910 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2911 unsigned long pval;
2912 int err;
2913
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002914 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002915 pval = kcontrol->private_value;
2916 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2917 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2918 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002919 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002920 return err;
2921}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002922EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002923
Takashi Iwaid5191e52009-11-16 14:58:17 +01002924/**
2925 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2926 *
2927 * The control element is supposed to have the private_value field
2928 * set up via HDA_BIND_MUTE*() macros.
2929 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002930int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2931 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002932{
2933 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2934 unsigned long pval;
2935 int i, indices, err = 0, change = 0;
2936
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002937 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002938 pval = kcontrol->private_value;
2939 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2940 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002941 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2942 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002943 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2944 if (err < 0)
2945 break;
2946 change |= err;
2947 }
2948 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002949 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002950 return err < 0 ? err : change;
2951}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002952EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002953
Takashi Iwaid5191e52009-11-16 14:58:17 +01002954/**
2955 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2956 *
2957 * The control element is supposed to have the private_value field
2958 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002959 */
2960int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2961 struct snd_ctl_elem_info *uinfo)
2962{
2963 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2964 struct hda_bind_ctls *c;
2965 int err;
2966
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002967 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002968 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002969 kcontrol->private_value = *c->values;
2970 err = c->ops->info(kcontrol, uinfo);
2971 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002972 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002973 return err;
2974}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002975EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002976
Takashi Iwaid5191e52009-11-16 14:58:17 +01002977/**
2978 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2979 *
2980 * The control element is supposed to have the private_value field
2981 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2982 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002983int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2984 struct snd_ctl_elem_value *ucontrol)
2985{
2986 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2987 struct hda_bind_ctls *c;
2988 int err;
2989
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002990 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002991 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002992 kcontrol->private_value = *c->values;
2993 err = c->ops->get(kcontrol, ucontrol);
2994 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002995 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002996 return err;
2997}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002998EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002999
Takashi Iwaid5191e52009-11-16 14:58:17 +01003000/**
3001 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
3002 *
3003 * The control element is supposed to have the private_value field
3004 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3005 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003006int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
3007 struct snd_ctl_elem_value *ucontrol)
3008{
3009 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3010 struct hda_bind_ctls *c;
3011 unsigned long *vals;
3012 int err = 0, change = 0;
3013
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003014 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003015 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003016 for (vals = c->values; *vals; vals++) {
3017 kcontrol->private_value = *vals;
3018 err = c->ops->put(kcontrol, ucontrol);
3019 if (err < 0)
3020 break;
3021 change |= err;
3022 }
3023 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003024 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003025 return err < 0 ? err : change;
3026}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003027EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02003028
Takashi Iwaid5191e52009-11-16 14:58:17 +01003029/**
3030 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
3031 *
3032 * The control element is supposed to have the private_value field
3033 * set up via HDA_BIND_VOL() macro.
3034 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003035int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
3036 unsigned int size, unsigned int __user *tlv)
3037{
3038 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3039 struct hda_bind_ctls *c;
3040 int err;
3041
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003042 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003043 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003044 kcontrol->private_value = *c->values;
3045 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
3046 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003047 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003048 return err;
3049}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003050EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02003051
3052struct hda_ctl_ops snd_hda_bind_vol = {
3053 .info = snd_hda_mixer_amp_volume_info,
3054 .get = snd_hda_mixer_amp_volume_get,
3055 .put = snd_hda_mixer_amp_volume_put,
3056 .tlv = snd_hda_mixer_amp_tlv
3057};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003058EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02003059
3060struct hda_ctl_ops snd_hda_bind_sw = {
3061 .info = snd_hda_mixer_amp_switch_info,
3062 .get = snd_hda_mixer_amp_switch_get,
3063 .put = snd_hda_mixer_amp_switch_put,
3064 .tlv = snd_hda_mixer_amp_tlv
3065};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003066EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02003067
3068/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 * SPDIF out controls
3070 */
3071
Takashi Iwai0ba21762007-04-16 11:29:14 +02003072static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
3073 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074{
3075 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
3076 uinfo->count = 1;
3077 return 0;
3078}
3079
Takashi Iwai0ba21762007-04-16 11:29:14 +02003080static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
3081 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082{
3083 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3084 IEC958_AES0_NONAUDIO |
3085 IEC958_AES0_CON_EMPHASIS_5015 |
3086 IEC958_AES0_CON_NOT_COPYRIGHT;
3087 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
3088 IEC958_AES1_CON_ORIGINAL;
3089 return 0;
3090}
3091
Takashi Iwai0ba21762007-04-16 11:29:14 +02003092static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
3093 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094{
3095 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3096 IEC958_AES0_NONAUDIO |
3097 IEC958_AES0_PRO_EMPHASIS_5015;
3098 return 0;
3099}
3100
Takashi Iwai0ba21762007-04-16 11:29:14 +02003101static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
3102 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103{
3104 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003105 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003106 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003108 mutex_lock(&codec->spdif_mutex);
3109 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003110 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
3111 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
3112 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
3113 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003114 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115
3116 return 0;
3117}
3118
3119/* convert from SPDIF status bits to HDA SPDIF bits
3120 * bit 0 (DigEn) is always set zero (to be filled later)
3121 */
3122static unsigned short convert_from_spdif_status(unsigned int sbits)
3123{
3124 unsigned short val = 0;
3125
3126 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003127 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003129 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003131 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
3132 IEC958_AES0_PRO_EMPHASIS_5015)
3133 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003135 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
3136 IEC958_AES0_CON_EMPHASIS_5015)
3137 val |= AC_DIG1_EMPHASIS;
3138 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
3139 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02003141 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
3143 }
3144 return val;
3145}
3146
3147/* convert to SPDIF status bits from HDA SPDIF bits
3148 */
3149static unsigned int convert_to_spdif_status(unsigned short val)
3150{
3151 unsigned int sbits = 0;
3152
Takashi Iwai0ba21762007-04-16 11:29:14 +02003153 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003155 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 sbits |= IEC958_AES0_PROFESSIONAL;
3157 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwaia686fd12013-03-20 15:42:00 +01003158 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
3160 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003161 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003163 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003165 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3167 sbits |= val & (0x7f << 8);
3168 }
3169 return sbits;
3170}
3171
Takashi Iwai2f728532008-09-25 16:32:41 +02003172/* set digital convert verbs both for the given NID and its slaves */
3173static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3174 int verb, int val)
3175{
Takashi Iwaidda14412011-05-02 11:29:30 +02003176 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02003177
Takashi Iwai9e976972008-11-25 08:17:20 +01003178 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003179 d = codec->slave_dig_outs;
3180 if (!d)
3181 return;
3182 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01003183 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003184}
3185
3186static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3187 int dig1, int dig2)
3188{
3189 if (dig1 != -1)
3190 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3191 if (dig2 != -1)
3192 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3193}
3194
Takashi Iwai0ba21762007-04-16 11:29:14 +02003195static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3196 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197{
3198 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003199 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003200 struct hda_spdif_out *spdif;
3201 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 unsigned short val;
3203 int change;
3204
Ingo Molnar62932df2006-01-16 16:34:20 +01003205 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003206 spdif = snd_array_elem(&codec->spdif_out, idx);
3207 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003208 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3210 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3211 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c935972011-06-01 11:14:17 -06003212 val = convert_from_spdif_status(spdif->status);
3213 val |= spdif->ctls & 1;
3214 change = spdif->ctls != val;
3215 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003216 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02003217 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01003218 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 return change;
3220}
3221
Takashi Iwaia5ce8892007-07-23 15:42:26 +02003222#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223
Takashi Iwai0ba21762007-04-16 11:29:14 +02003224static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3225 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226{
3227 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003228 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003229 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003231 mutex_lock(&codec->spdif_mutex);
3232 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003233 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003234 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 return 0;
3236}
3237
Stephen Warren74b654c2011-06-01 11:14:18 -06003238static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3239 int dig1, int dig2)
3240{
3241 set_dig_out_convert(codec, nid, dig1, dig2);
3242 /* unmute amp switch (if any) */
3243 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3244 (dig1 & AC_DIG1_ENABLE))
3245 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3246 HDA_AMP_MUTE, 0);
3247}
3248
Takashi Iwai0ba21762007-04-16 11:29:14 +02003249static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3250 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251{
3252 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003253 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003254 struct hda_spdif_out *spdif;
3255 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 unsigned short val;
3257 int change;
3258
Ingo Molnar62932df2006-01-16 16:34:20 +01003259 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003260 spdif = snd_array_elem(&codec->spdif_out, idx);
3261 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003262 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003264 val |= AC_DIG1_ENABLE;
Stephen Warren7c935972011-06-01 11:14:17 -06003265 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003266 spdif->ctls = val;
3267 if (change && nid != (u16)-1)
3268 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003269 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 return change;
3271}
3272
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003273static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 {
3275 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3276 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003277 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 .info = snd_hda_spdif_mask_info,
3279 .get = snd_hda_spdif_cmask_get,
3280 },
3281 {
3282 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3283 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003284 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 .info = snd_hda_spdif_mask_info,
3286 .get = snd_hda_spdif_pmask_get,
3287 },
3288 {
3289 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003290 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 .info = snd_hda_spdif_mask_info,
3292 .get = snd_hda_spdif_default_get,
3293 .put = snd_hda_spdif_default_put,
3294 },
3295 {
3296 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003297 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 .info = snd_hda_spdif_out_switch_info,
3299 .get = snd_hda_spdif_out_switch_get,
3300 .put = snd_hda_spdif_out_switch_put,
3301 },
3302 { } /* end */
3303};
3304
3305/**
Takashi Iwaidcda5802012-10-12 17:24:51 +02003306 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 * @codec: the HDA codec
Takashi Iwaidcda5802012-10-12 17:24:51 +02003308 * @associated_nid: NID that new ctls associated with
3309 * @cvt_nid: converter NID
3310 * @type: HDA_PCM_TYPE_*
3311 * Creates controls related with the digital output.
3312 * Called from each patch supporting the digital out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 *
3314 * Returns 0 if successful, or a negative error code.
3315 */
Takashi Iwaidcda5802012-10-12 17:24:51 +02003316int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3317 hda_nid_t associated_nid,
3318 hda_nid_t cvt_nid,
3319 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320{
3321 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003322 struct snd_kcontrol *kctl;
3323 struct snd_kcontrol_new *dig_mix;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003324 int idx = 0;
3325 const int spdif_index = 16;
Stephen Warren7c935972011-06-01 11:14:17 -06003326 struct hda_spdif_out *spdif;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003327 struct hda_bus *bus = codec->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003329 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003330 type == HDA_PCM_TYPE_SPDIF) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003331 idx = spdif_index;
3332 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003333 type == HDA_PCM_TYPE_HDMI) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003334 /* suppose a single SPDIF device */
3335 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3336 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
3337 if (!kctl)
3338 break;
3339 kctl->id.index = spdif_index;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003340 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003341 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003342 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003343 if (!bus->primary_dig_out_type)
3344 bus->primary_dig_out_type = type;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003345
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003346 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003347 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003348 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
3349 return -EBUSY;
3350 }
Stephen Warren7c935972011-06-01 11:14:17 -06003351 spdif = snd_array_new(&codec->spdif_out);
Mengdong Lin25336e82013-03-07 14:10:25 -05003352 if (!spdif)
3353 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3355 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003356 if (!kctl)
3357 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01003358 kctl->id.index = idx;
Stephen Warren7c935972011-06-01 11:14:17 -06003359 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003360 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003361 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 return err;
3363 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003364 spdif->nid = cvt_nid;
3365 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c935972011-06-01 11:14:17 -06003366 AC_VERB_GET_DIGI_CONVERT_1, 0);
3367 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 return 0;
3369}
Takashi Iwaidcda5802012-10-12 17:24:51 +02003370EXPORT_SYMBOL_HDA(snd_hda_create_dig_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003372/* get the hda_spdif_out entry from the given NID
3373 * call within spdif_mutex lock
3374 */
Stephen Warren7c935972011-06-01 11:14:17 -06003375struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3376 hda_nid_t nid)
3377{
3378 int i;
3379 for (i = 0; i < codec->spdif_out.used; i++) {
3380 struct hda_spdif_out *spdif =
3381 snd_array_elem(&codec->spdif_out, i);
3382 if (spdif->nid == nid)
3383 return spdif;
3384 }
3385 return NULL;
3386}
3387EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
3388
Stephen Warren74b654c2011-06-01 11:14:18 -06003389void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3390{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003391 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003392
3393 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003394 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003395 spdif->nid = (u16)-1;
3396 mutex_unlock(&codec->spdif_mutex);
3397}
3398EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
3399
3400void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3401{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003402 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003403 unsigned short val;
3404
3405 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003406 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003407 if (spdif->nid != nid) {
3408 spdif->nid = nid;
3409 val = spdif->ctls;
3410 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3411 }
3412 mutex_unlock(&codec->spdif_mutex);
3413}
3414EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
3415
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003417 * SPDIF sharing with analog output
3418 */
3419static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3420 struct snd_ctl_elem_value *ucontrol)
3421{
3422 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3423 ucontrol->value.integer.value[0] = mout->share_spdif;
3424 return 0;
3425}
3426
3427static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3428 struct snd_ctl_elem_value *ucontrol)
3429{
3430 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3431 mout->share_spdif = !!ucontrol->value.integer.value[0];
3432 return 0;
3433}
3434
3435static struct snd_kcontrol_new spdif_share_sw = {
3436 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3437 .name = "IEC958 Default PCM Playback Switch",
3438 .info = snd_ctl_boolean_mono_info,
3439 .get = spdif_share_sw_get,
3440 .put = spdif_share_sw_put,
3441};
3442
Takashi Iwaid5191e52009-11-16 14:58:17 +01003443/**
3444 * snd_hda_create_spdif_share_sw - create Default PCM switch
3445 * @codec: the HDA codec
3446 * @mout: multi-out instance
3447 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003448int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3449 struct hda_multi_out *mout)
3450{
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003451 struct snd_kcontrol *kctl;
3452
Takashi Iwai9a081602008-02-12 18:37:26 +01003453 if (!mout->dig_out_nid)
3454 return 0;
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003455
3456 kctl = snd_ctl_new1(&spdif_share_sw, mout);
3457 if (!kctl)
3458 return -ENOMEM;
Takashi Iwai9a081602008-02-12 18:37:26 +01003459 /* ATTENTION: here mout is passed as private_data, instead of codec */
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003460 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
Takashi Iwai9a081602008-02-12 18:37:26 +01003461}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003462EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003463
3464/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 * SPDIF input
3466 */
3467
3468#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3469
Takashi Iwai0ba21762007-04-16 11:29:14 +02003470static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3471 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472{
3473 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3474
3475 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3476 return 0;
3477}
3478
Takashi Iwai0ba21762007-04-16 11:29:14 +02003479static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3480 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481{
3482 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3483 hda_nid_t nid = kcontrol->private_value;
3484 unsigned int val = !!ucontrol->value.integer.value[0];
3485 int change;
3486
Ingo Molnar62932df2006-01-16 16:34:20 +01003487 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003489 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003491 snd_hda_codec_write_cache(codec, nid, 0,
3492 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003494 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 return change;
3496}
3497
Takashi Iwai0ba21762007-04-16 11:29:14 +02003498static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3499 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500{
3501 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3502 hda_nid_t nid = kcontrol->private_value;
3503 unsigned short val;
3504 unsigned int sbits;
3505
Andrew Paprocki3982d172007-12-19 12:13:44 +01003506 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 sbits = convert_to_spdif_status(val);
3508 ucontrol->value.iec958.status[0] = sbits;
3509 ucontrol->value.iec958.status[1] = sbits >> 8;
3510 ucontrol->value.iec958.status[2] = sbits >> 16;
3511 ucontrol->value.iec958.status[3] = sbits >> 24;
3512 return 0;
3513}
3514
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003515static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 {
3517 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003518 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 .info = snd_hda_spdif_in_switch_info,
3520 .get = snd_hda_spdif_in_switch_get,
3521 .put = snd_hda_spdif_in_switch_put,
3522 },
3523 {
3524 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3525 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003526 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 .info = snd_hda_spdif_mask_info,
3528 .get = snd_hda_spdif_in_status_get,
3529 },
3530 { } /* end */
3531};
3532
3533/**
3534 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3535 * @codec: the HDA codec
3536 * @nid: audio in widget NID
3537 *
3538 * Creates controls related with the SPDIF input.
3539 * Called from each patch supporting the SPDIF in.
3540 *
3541 * Returns 0 if successful, or a negative error code.
3542 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003543int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544{
3545 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003546 struct snd_kcontrol *kctl;
3547 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003548 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549
Takashi Iwaidcda5802012-10-12 17:24:51 +02003550 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003551 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003552 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3553 return -EBUSY;
3554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3556 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003557 if (!kctl)
3558 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003560 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003561 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 return err;
3563 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003564 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003565 snd_hda_codec_read(codec, nid, 0,
3566 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003567 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 return 0;
3569}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003570EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003572/*
3573 * command cache
3574 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575
Takashi Iwaic370dd62012-12-13 18:30:04 +01003576/* build a 31bit cache key with the widget id and the command parameter */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003577#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3578#define get_cmd_cache_nid(key) ((key) & 0xff)
3579#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3580
3581/**
3582 * snd_hda_codec_write_cache - send a single command with caching
3583 * @codec: the HDA codec
3584 * @nid: NID to send the command
3585 * @direct: direct flag
3586 * @verb: the verb to send
3587 * @parm: the parameter for the verb
3588 *
3589 * Send a single command without waiting for response.
3590 *
3591 * Returns 0 if successful, or a negative error code.
3592 */
3593int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3594 int direct, unsigned int verb, unsigned int parm)
3595{
Takashi Iwaic370dd62012-12-13 18:30:04 +01003596 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003597 struct hda_cache_head *c;
3598 u32 key;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003599 unsigned int cache_only;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003600
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003601 cache_only = codec->cached_write;
3602 if (!cache_only) {
Takashi Iwaic370dd62012-12-13 18:30:04 +01003603 err = snd_hda_codec_write(codec, nid, direct, verb, parm);
3604 if (err < 0)
3605 return err;
3606 }
3607
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003608 /* parm may contain the verb stuff for get/set amp */
3609 verb = verb | (parm >> 8);
3610 parm &= 0xff;
3611 key = build_cmd_cache_key(nid, verb);
3612 mutex_lock(&codec->bus->cmd_mutex);
3613 c = get_alloc_hash(&codec->cmd_cache, key);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003614 if (c) {
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003615 c->val = parm;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003616 c->dirty = cache_only;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003617 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003618 mutex_unlock(&codec->bus->cmd_mutex);
3619 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003620}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003621EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003622
Takashi Iwaid5191e52009-11-16 14:58:17 +01003623/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003624 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3625 * @codec: the HDA codec
3626 * @nid: NID to send the command
3627 * @direct: direct flag
3628 * @verb: the verb to send
3629 * @parm: the parameter for the verb
3630 *
3631 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3632 * command if the parameter is already identical with the cached value.
3633 * If not, it sends the command and refreshes the cache.
3634 *
3635 * Returns 0 if successful, or a negative error code.
3636 */
3637int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3638 int direct, unsigned int verb, unsigned int parm)
3639{
3640 struct hda_cache_head *c;
3641 u32 key;
3642
3643 /* parm may contain the verb stuff for get/set amp */
3644 verb = verb | (parm >> 8);
3645 parm &= 0xff;
3646 key = build_cmd_cache_key(nid, verb);
3647 mutex_lock(&codec->bus->cmd_mutex);
3648 c = get_hash(&codec->cmd_cache, key);
3649 if (c && c->val == parm) {
3650 mutex_unlock(&codec->bus->cmd_mutex);
3651 return 0;
3652 }
3653 mutex_unlock(&codec->bus->cmd_mutex);
3654 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3655}
3656EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3657
3658/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003659 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3660 * @codec: HD-audio codec
3661 *
3662 * Execute all verbs recorded in the command caches to resume.
3663 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003664void snd_hda_codec_resume_cache(struct hda_codec *codec)
3665{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003666 int i;
3667
Takashi Iwaic370dd62012-12-13 18:30:04 +01003668 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01003669 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003670 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3671 struct hda_cache_head *buffer;
3672 u32 key;
3673
3674 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3675 key = buffer->key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003676 if (!key)
3677 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003678 if (!buffer->dirty)
3679 continue;
3680 buffer->dirty = 0;
3681 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003682 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3683 get_cmd_cache_cmd(key), buffer->val);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003684 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003685 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01003686 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003687}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003688EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003689
3690/**
3691 * snd_hda_sequence_write_cache - sequence writes with caching
3692 * @codec: the HDA codec
3693 * @seq: VERB array to send
3694 *
3695 * Send the commands sequentially from the given array.
3696 * Thte commands are recorded on cache for power-save and resume.
3697 * The array must be terminated with NID=0.
3698 */
3699void snd_hda_sequence_write_cache(struct hda_codec *codec,
3700 const struct hda_verb *seq)
3701{
3702 for (; seq->nid; seq++)
3703 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3704 seq->param);
3705}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003706EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003707
Takashi Iwaidc870f32013-01-22 15:24:30 +01003708/**
3709 * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
3710 * @codec: HD-audio codec
3711 */
3712void snd_hda_codec_flush_cache(struct hda_codec *codec)
3713{
3714 snd_hda_codec_resume_amp(codec);
3715 snd_hda_codec_resume_cache(codec);
3716}
3717EXPORT_SYMBOL_HDA(snd_hda_codec_flush_cache);
3718
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003719void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003720 unsigned int power_state)
Takashi Iwai54d17402005-11-21 16:33:22 +01003721{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003722 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003723 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003724
Takashi Iwaicb53c622007-08-10 17:21:45 +02003725 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003726 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003727 unsigned int state = power_state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003728 if (!(wcaps & AC_WCAP_POWER))
3729 continue;
Takashi Iwai9419ab62013-01-24 17:23:35 +01003730 if (codec->power_filter) {
3731 state = codec->power_filter(codec, nid, power_state);
3732 if (state != power_state && power_state == AC_PWRST_D3)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003733 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003734 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003735 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003736 state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003737 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003738}
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003739EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3740
3741/*
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003742 * supported power states check
3743 */
3744static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
3745 unsigned int power_state)
3746{
3747 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
3748
Mengdong Line037cb42012-08-10 14:11:58 +02003749 if (sup == -1)
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003750 return false;
3751 if (sup & power_state)
3752 return true;
3753 else
3754 return false;
3755}
3756
3757/*
Takashi Iwai432c6412012-08-28 09:59:20 -07003758 * wait until the state is reached, returns the current state
3759 */
3760static unsigned int hda_sync_power_state(struct hda_codec *codec,
3761 hda_nid_t fg,
3762 unsigned int power_state)
3763{
3764 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3765 unsigned int state, actual_state;
3766
3767 for (;;) {
3768 state = snd_hda_codec_read(codec, fg, 0,
3769 AC_VERB_GET_POWER_STATE, 0);
3770 if (state & AC_PWRST_ERROR)
3771 break;
3772 actual_state = (state >> 4) & 0x0f;
3773 if (actual_state == power_state)
3774 break;
3775 if (time_after_eq(jiffies, end_time))
3776 break;
3777 /* wait until the codec reachs to the target state */
3778 msleep(1);
3779 }
3780 return state;
3781}
3782
Takashi Iwai9419ab62013-01-24 17:23:35 +01003783/* don't power down the widget if it controls eapd and EAPD_BTLENABLE is set */
Takashi Iwaiba615b82013-03-13 14:47:21 +01003784unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
3785 hda_nid_t nid,
3786 unsigned int power_state)
Takashi Iwai9419ab62013-01-24 17:23:35 +01003787{
3788 if (power_state == AC_PWRST_D3 &&
3789 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
3790 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3791 int eapd = snd_hda_codec_read(codec, nid, 0,
3792 AC_VERB_GET_EAPD_BTLENABLE, 0);
3793 if (eapd & 0x02)
3794 return AC_PWRST_D0;
3795 }
3796 return power_state;
3797}
Takashi Iwaiba615b82013-03-13 14:47:21 +01003798EXPORT_SYMBOL_HDA(snd_hda_codec_eapd_power_filter);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003799
Takashi Iwai432c6412012-08-28 09:59:20 -07003800/*
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003801 * set power state of the codec, and return the power state
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003802 */
Takashi Iwaid8193872012-08-31 07:54:38 -07003803static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003804 unsigned int power_state)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003805{
Takashi Iwaid8193872012-08-31 07:54:38 -07003806 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003807 int count;
3808 unsigned int state;
3809
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003810 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003811 if (power_state == AC_PWRST_D3) {
3812 /* transition time less than 10ms for power down */
Takashi Iwai983f6b92012-08-28 09:18:01 -07003813 msleep(codec->epss ? 10 : 100);
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003814 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08003815
3816 /* repeat power states setting at most 10 times*/
3817 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07003818 if (codec->patch_ops.set_power_state)
3819 codec->patch_ops.set_power_state(codec, fg,
3820 power_state);
3821 else {
3822 snd_hda_codec_read(codec, fg, 0,
3823 AC_VERB_SET_POWER_STATE,
3824 power_state);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003825 snd_hda_codec_set_power_to_all(codec, fg, power_state);
Takashi Iwai432c6412012-08-28 09:59:20 -07003826 }
3827 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08003828 if (!(state & AC_PWRST_ERROR))
3829 break;
3830 }
Mengdong Linb8dfc4622012-08-23 17:32:30 +08003831
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003832 return state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003833}
Takashi Iwai54d17402005-11-21 16:33:22 +01003834
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003835/* sync power states of all widgets;
3836 * this is called at the end of codec parsing
3837 */
3838static void sync_power_up_states(struct hda_codec *codec)
3839{
3840 hda_nid_t nid = codec->start_nid;
3841 int i;
3842
Takashi Iwaiba615b82013-03-13 14:47:21 +01003843 /* don't care if no filter is used */
3844 if (!codec->power_filter)
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003845 return;
3846
3847 for (i = 0; i < codec->num_nodes; i++, nid++) {
3848 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9040d102013-01-24 17:47:17 +01003849 unsigned int target;
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003850 if (!(wcaps & AC_WCAP_POWER))
3851 continue;
3852 target = codec->power_filter(codec, nid, AC_PWRST_D0);
3853 if (target == AC_PWRST_D0)
3854 continue;
Takashi Iwai9040d102013-01-24 17:47:17 +01003855 if (!snd_hda_check_power_state(codec, nid, target))
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003856 snd_hda_codec_write(codec, nid, 0,
3857 AC_VERB_SET_POWER_STATE, target);
3858 }
3859}
3860
Takashi Iwai11aeff02008-07-30 15:01:46 +02003861#ifdef CONFIG_SND_HDA_HWDEP
3862/* execute additional init verbs */
3863static void hda_exec_init_verbs(struct hda_codec *codec)
3864{
3865 if (codec->init_verbs.list)
3866 snd_hda_sequence_write(codec, codec->init_verbs.list);
3867}
3868#else
3869static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3870#endif
3871
Takashi Iwai2a439522011-07-26 09:52:50 +02003872#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003873/*
3874 * call suspend and power-down; used both from PM and power-save
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003875 * this function returns the power state in the end
Takashi Iwaicb53c622007-08-10 17:21:45 +02003876 */
Dylan Reidd17344b2012-09-28 15:57:01 -07003877static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003878{
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003879 unsigned int state;
3880
Takashi Iwai989c3182012-11-19 14:14:58 +01003881 codec->in_pm = 1;
3882
Takashi Iwaicb53c622007-08-10 17:21:45 +02003883 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02003884 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02003885 hda_cleanup_all_streams(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003886 state = hda_set_power_state(codec, AC_PWRST_D3);
Dylan Reidd17344b2012-09-28 15:57:01 -07003887 /* Cancel delayed work if we aren't currently running from it. */
3888 if (!in_wq)
3889 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003890 spin_lock(&codec->power_lock);
3891 snd_hda_update_power_acct(codec);
3892 trace_hda_power_down(codec);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003893 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003894 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003895 codec->power_jiffies = jiffies;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003896 spin_unlock(&codec->power_lock);
Takashi Iwai989c3182012-11-19 14:14:58 +01003897 codec->in_pm = 0;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003898 return state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003899}
3900
Takashi Iwaic370dd62012-12-13 18:30:04 +01003901/* mark all entries of cmd and amp caches dirty */
3902static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
3903{
3904 int i;
3905 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3906 struct hda_cache_head *cmd;
3907 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
3908 cmd->dirty = 1;
3909 }
3910 for (i = 0; i < codec->amp_cache.buf.used; i++) {
3911 struct hda_amp_info *amp;
David Henningssonf038fca2013-01-15 15:27:19 +01003912 amp = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003913 amp->head.dirty = 1;
3914 }
3915}
3916
Takashi Iwaicb53c622007-08-10 17:21:45 +02003917/*
3918 * kick up codec; used both from PM and power-save
3919 */
3920static void hda_call_codec_resume(struct hda_codec *codec)
3921{
Takashi Iwai989c3182012-11-19 14:14:58 +01003922 codec->in_pm = 1;
3923
Takashi Iwaic370dd62012-12-13 18:30:04 +01003924 hda_mark_cmd_cache_dirty(codec);
3925
Takashi Iwai7f308302012-05-08 16:52:23 +02003926 /* set as if powered on for avoiding re-entering the resume
3927 * in the resume / power-save sequence
3928 */
3929 hda_keep_power_on(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003930 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003931 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003932 hda_exec_init_verbs(codec);
Takashi Iwai31614bb2013-01-23 15:58:40 +01003933 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003934 if (codec->patch_ops.resume)
3935 codec->patch_ops.resume(codec);
3936 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003937 if (codec->patch_ops.init)
3938 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003939 snd_hda_codec_resume_amp(codec);
3940 snd_hda_codec_resume_cache(codec);
3941 }
David Henningsson26a6cb62012-10-09 15:04:21 +02003942
3943 if (codec->jackpoll_interval)
3944 hda_jackpoll_work(&codec->jackpoll_work.work);
Takashi Iwai31614bb2013-01-23 15:58:40 +01003945 else
David Henningsson26a6cb62012-10-09 15:04:21 +02003946 snd_hda_jack_report_sync(codec);
Takashi Iwai989c3182012-11-19 14:14:58 +01003947
3948 codec->in_pm = 0;
Takashi Iwai7f308302012-05-08 16:52:23 +02003949 snd_hda_power_down(codec); /* flag down before returning */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003950}
Takashi Iwai2a439522011-07-26 09:52:50 +02003951#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003952
Takashi Iwai54d17402005-11-21 16:33:22 +01003953
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954/**
3955 * snd_hda_build_controls - build mixer controls
3956 * @bus: the BUS
3957 *
3958 * Creates mixer controls for each codec included in the bus.
3959 *
3960 * Returns 0 if successful, otherwise a negative error code.
3961 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01003962int snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003964 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965
Takashi Iwai0ba21762007-04-16 11:29:14 +02003966 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003967 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003968 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01003969 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003970 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003971 err = snd_hda_codec_reset(codec);
3972 if (err < 0) {
3973 printk(KERN_ERR
3974 "hda_codec: cannot revert codec\n");
3975 return err;
3976 }
3977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003979 return 0;
3980}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003981EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003983/*
3984 * add standard channel maps if not specified
3985 */
3986static int add_std_chmaps(struct hda_codec *codec)
3987{
3988 int i, str, err;
3989
3990 for (i = 0; i < codec->num_pcms; i++) {
3991 for (str = 0; str < 2; str++) {
3992 struct snd_pcm *pcm = codec->pcm_info[i].pcm;
3993 struct hda_pcm_stream *hinfo =
3994 &codec->pcm_info[i].stream[str];
3995 struct snd_pcm_chmap *chmap;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003996 const struct snd_pcm_chmap_elem *elem;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003997
3998 if (codec->pcm_info[i].own_chmap)
3999 continue;
4000 if (!pcm || !hinfo->substreams)
4001 continue;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004002 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
4003 err = snd_pcm_add_chmap_ctls(pcm, str, elem,
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004004 hinfo->channels_max,
4005 0, &chmap);
4006 if (err < 0)
4007 return err;
4008 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
4009 }
4010 }
4011 return 0;
4012}
4013
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004014/* default channel maps for 2.1 speakers;
4015 * since HD-audio supports only stereo, odd number channels are omitted
4016 */
4017const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
4018 { .channels = 2,
4019 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
4020 { .channels = 4,
4021 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
4022 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
4023 { }
4024};
4025EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
4026
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004027int snd_hda_codec_build_controls(struct hda_codec *codec)
4028{
4029 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02004030 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004031 /* continue to initialize... */
4032 if (codec->patch_ops.init)
4033 err = codec->patch_ops.init(codec);
4034 if (!err && codec->patch_ops.build_controls)
4035 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004036 if (err < 0)
4037 return err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004038
4039 /* we create chmaps here instead of build_pcms */
4040 err = add_std_chmaps(codec);
4041 if (err < 0)
4042 return err;
4043
David Henningsson26a6cb62012-10-09 15:04:21 +02004044 if (codec->jackpoll_interval)
4045 hda_jackpoll_work(&codec->jackpoll_work.work);
4046 else
4047 snd_hda_jack_report_sync(codec); /* call at the last init point */
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004048 sync_power_up_states(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 return 0;
4050}
4051
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052/*
4053 * stream formats
4054 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02004055struct hda_rate_tbl {
4056 unsigned int hz;
4057 unsigned int alsa_bits;
4058 unsigned int hda_fmt;
4059};
4060
Takashi Iwai92f10b32010-08-03 14:21:00 +02004061/* rate = base * mult / div */
4062#define HDA_RATE(base, mult, div) \
4063 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
4064 (((div) - 1) << AC_FMT_DIV_SHIFT))
4065
Takashi Iwaibefdf312005-08-22 13:57:55 +02004066static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004068
4069 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004070 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
4071 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
4072 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
4073 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
4074 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
4075 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
4076 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
4077 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
4078 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
4079 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
4080 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004081#define AC_PAR_PCM_RATE_BITS 11
4082 /* up to bits 10, 384kHZ isn't supported properly */
4083
4084 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004085 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004086
Takashi Iwaibefdf312005-08-22 13:57:55 +02004087 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088};
4089
4090/**
4091 * snd_hda_calc_stream_format - calculate format bitset
4092 * @rate: the sample rate
4093 * @channels: the number of channels
4094 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
4095 * @maxbps: the max. bps
4096 *
4097 * Calculate the format bitset from the given rate, channels and th PCM format.
4098 *
4099 * Return zero if invalid.
4100 */
4101unsigned int snd_hda_calc_stream_format(unsigned int rate,
4102 unsigned int channels,
4103 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03004104 unsigned int maxbps,
4105 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106{
4107 int i;
4108 unsigned int val = 0;
4109
Takashi Iwaibefdf312005-08-22 13:57:55 +02004110 for (i = 0; rate_bits[i].hz; i++)
4111 if (rate_bits[i].hz == rate) {
4112 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 break;
4114 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02004115 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 snd_printdd("invalid rate %d\n", rate);
4117 return 0;
4118 }
4119
4120 if (channels == 0 || channels > 8) {
4121 snd_printdd("invalid channels %d\n", channels);
4122 return 0;
4123 }
4124 val |= channels - 1;
4125
4126 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004127 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004128 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004129 break;
4130 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004131 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004132 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 case 20:
4134 case 24:
4135 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004136 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004137 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004139 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02004141 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 break;
4143 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004144 snd_printdd("invalid format width %d\n",
4145 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 return 0;
4147 }
4148
Anssi Hannula32c168c2010-08-03 13:28:57 +03004149 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004150 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03004151
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 return val;
4153}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004154EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004156static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
4157 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004158{
4159 unsigned int val = 0;
4160 if (nid != codec->afg &&
4161 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
4162 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
4163 if (!val || val == -1)
4164 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
4165 if (!val || val == -1)
4166 return 0;
4167 return val;
4168}
4169
4170static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
4171{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004172 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004173 get_pcm_param);
4174}
4175
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004176static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
4177 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004178{
4179 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
4180 if (!streams || streams == -1)
4181 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
4182 if (!streams || streams == -1)
4183 return 0;
4184 return streams;
4185}
4186
4187static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
4188{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004189 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004190 get_stream_param);
4191}
4192
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193/**
4194 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4195 * @codec: the HDA codec
4196 * @nid: NID to query
4197 * @ratesp: the pointer to store the detected rate bitflags
4198 * @formatsp: the pointer to store the detected formats
4199 * @bpsp: the pointer to store the detected format widths
4200 *
4201 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4202 * or @bsps argument is ignored.
4203 *
4204 * Returns 0 if successful, otherwise a negative error code.
4205 */
Stephen Warren384a48d2011-06-01 11:14:21 -06004206int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
4208{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004209 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004211 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004212 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213
4214 if (ratesp) {
4215 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004216 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02004218 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004220 if (rates == 0) {
4221 snd_printk(KERN_ERR "hda_codec: rates == 0 "
4222 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
4223 nid, val,
4224 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
4225 return -EIO;
4226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 *ratesp = rates;
4228 }
4229
4230 if (formatsp || bpsp) {
4231 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004232 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004234 streams = query_stream_param(codec, nid);
4235 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237
4238 bps = 0;
4239 if (streams & AC_SUPFMT_PCM) {
4240 if (val & AC_SUPPCM_BITS_8) {
4241 formats |= SNDRV_PCM_FMTBIT_U8;
4242 bps = 8;
4243 }
4244 if (val & AC_SUPPCM_BITS_16) {
4245 formats |= SNDRV_PCM_FMTBIT_S16_LE;
4246 bps = 16;
4247 }
4248 if (wcaps & AC_WCAP_DIGITAL) {
4249 if (val & AC_SUPPCM_BITS_32)
4250 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4251 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4252 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4253 if (val & AC_SUPPCM_BITS_24)
4254 bps = 24;
4255 else if (val & AC_SUPPCM_BITS_20)
4256 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004257 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4258 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4260 if (val & AC_SUPPCM_BITS_32)
4261 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 else if (val & AC_SUPPCM_BITS_24)
4263 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02004264 else if (val & AC_SUPPCM_BITS_20)
4265 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 }
4267 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004268#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02004269 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004271 if (!bps)
4272 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02004273 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004274#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02004275 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02004276 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 /* temporary hack: we have still no proper support
4278 * for the direct AC3 stream...
4279 */
4280 formats |= SNDRV_PCM_FMTBIT_U8;
4281 bps = 8;
4282 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004283 if (formats == 0) {
4284 snd_printk(KERN_ERR "hda_codec: formats == 0 "
4285 "(nid=0x%x, val=0x%x, ovrd=%i, "
4286 "streams=0x%x)\n",
4287 nid, val,
4288 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4289 streams);
4290 return -EIO;
4291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 if (formatsp)
4293 *formatsp = formats;
4294 if (bpsp)
4295 *bpsp = bps;
4296 }
4297
4298 return 0;
4299}
Stephen Warren384a48d2011-06-01 11:14:21 -06004300EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301
4302/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004303 * snd_hda_is_supported_format - Check the validity of the format
4304 * @codec: HD-audio codec
4305 * @nid: NID to check
4306 * @format: the HD-audio format value to check
4307 *
4308 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 *
4310 * Returns 1 if supported, 0 if not.
4311 */
4312int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4313 unsigned int format)
4314{
4315 int i;
4316 unsigned int val = 0, rate, stream;
4317
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004318 val = query_pcm_param(codec, nid);
4319 if (!val)
4320 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321
4322 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004323 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02004324 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 if (val & (1 << i))
4326 break;
4327 return 0;
4328 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004329 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 return 0;
4331
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004332 stream = query_stream_param(codec, nid);
4333 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 return 0;
4335
4336 if (stream & AC_SUPFMT_PCM) {
4337 switch (format & 0xf0) {
4338 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004339 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 return 0;
4341 break;
4342 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004343 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 return 0;
4345 break;
4346 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004347 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348 return 0;
4349 break;
4350 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004351 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 return 0;
4353 break;
4354 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004355 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356 return 0;
4357 break;
4358 default:
4359 return 0;
4360 }
4361 } else {
4362 /* FIXME: check for float32 and AC3? */
4363 }
4364
4365 return 1;
4366}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004367EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368
4369/*
4370 * PCM stuff
4371 */
4372static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4373 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004374 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375{
4376 return 0;
4377}
4378
4379static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4380 struct hda_codec *codec,
4381 unsigned int stream_tag,
4382 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004383 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384{
4385 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4386 return 0;
4387}
4388
4389static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4390 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004391 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392{
Takashi Iwai888afa12008-03-18 09:57:50 +01004393 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 return 0;
4395}
4396
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004397static int set_pcm_default_values(struct hda_codec *codec,
4398 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004400 int err;
4401
Takashi Iwai0ba21762007-04-16 11:29:14 +02004402 /* query support PCM information from the given NID */
4403 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004404 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02004405 info->rates ? NULL : &info->rates,
4406 info->formats ? NULL : &info->formats,
4407 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004408 if (err < 0)
4409 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 }
4411 if (info->ops.open == NULL)
4412 info->ops.open = hda_pcm_default_open_close;
4413 if (info->ops.close == NULL)
4414 info->ops.close = hda_pcm_default_open_close;
4415 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004416 if (snd_BUG_ON(!info->nid))
4417 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 info->ops.prepare = hda_pcm_default_prepare;
4419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004421 if (snd_BUG_ON(!info->nid))
4422 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 info->ops.cleanup = hda_pcm_default_cleanup;
4424 }
4425 return 0;
4426}
4427
Takashi Iwaieb541332010-08-06 13:48:11 +02004428/*
4429 * codec prepare/cleanup entries
4430 */
4431int snd_hda_codec_prepare(struct hda_codec *codec,
4432 struct hda_pcm_stream *hinfo,
4433 unsigned int stream,
4434 unsigned int format,
4435 struct snd_pcm_substream *substream)
4436{
4437 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004438 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004439 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4440 if (ret >= 0)
4441 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004442 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004443 return ret;
4444}
4445EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
4446
4447void snd_hda_codec_cleanup(struct hda_codec *codec,
4448 struct hda_pcm_stream *hinfo,
4449 struct snd_pcm_substream *substream)
4450{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004451 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004452 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004453 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004454}
4455EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
4456
Takashi Iwaid5191e52009-11-16 14:58:17 +01004457/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01004458const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4459 "Audio", "SPDIF", "HDMI", "Modem"
4460};
4461
Takashi Iwai176d5332008-07-30 15:01:44 +02004462/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004463 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01004464 *
4465 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004466 */
4467static int get_empty_pcm_device(struct hda_bus *bus, int type)
4468{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004469 /* audio device indices; not linear to keep compatibility */
4470 static int audio_idx[HDA_PCM_NTYPES][5] = {
4471 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4472 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01004473 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004474 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004475 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004476 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004477
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004478 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004479 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
4480 return -EINVAL;
4481 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004482
4483 for (i = 0; audio_idx[type][i] >= 0 ; i++)
4484 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4485 return audio_idx[type][i];
4486
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004487 /* non-fixed slots starting from 10 */
4488 for (i = 10; i < 32; i++) {
4489 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4490 return i;
4491 }
4492
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004493 snd_printk(KERN_WARNING "Too many %s devices\n",
4494 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004495 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004496}
4497
4498/*
Takashi Iwai176d5332008-07-30 15:01:44 +02004499 * attach a new PCM stream
4500 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004501static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02004502{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004503 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02004504 struct hda_pcm_stream *info;
4505 int stream, err;
4506
Takashi Iwaib91f0802008-11-04 08:43:08 +01004507 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02004508 return -EINVAL;
4509 for (stream = 0; stream < 2; stream++) {
4510 info = &pcm->stream[stream];
4511 if (info->substreams) {
4512 err = set_pcm_default_values(codec, info);
4513 if (err < 0)
4514 return err;
4515 }
4516 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004517 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02004518}
4519
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004520/* assign all PCMs of the given codec */
4521int snd_hda_codec_build_pcms(struct hda_codec *codec)
4522{
4523 unsigned int pcm;
4524 int err;
4525
4526 if (!codec->num_pcms) {
4527 if (!codec->patch_ops.build_pcms)
4528 return 0;
4529 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004530 if (err < 0) {
4531 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004532 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004533 err = snd_hda_codec_reset(codec);
4534 if (err < 0) {
4535 printk(KERN_ERR
4536 "hda_codec: cannot revert codec\n");
4537 return err;
4538 }
4539 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004540 }
4541 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4542 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4543 int dev;
4544
4545 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004546 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004547
4548 if (!cpcm->pcm) {
4549 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4550 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004551 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004552 cpcm->device = dev;
4553 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004554 if (err < 0) {
4555 printk(KERN_ERR "hda_codec: cannot attach "
4556 "PCM stream %d for codec #%d\n",
4557 dev, codec->addr);
4558 continue; /* no fatal error */
4559 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004560 }
4561 }
4562 return 0;
4563}
4564
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565/**
4566 * snd_hda_build_pcms - build PCM information
4567 * @bus: the BUS
4568 *
4569 * Create PCM information for each codec included in the bus.
4570 *
4571 * The build_pcms codec patch is requested to set up codec->num_pcms and
4572 * codec->pcm_info properly. The array is referred by the top-level driver
4573 * to create its PCM instances.
4574 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4575 * callback.
4576 *
4577 * At least, substreams, channels_min and channels_max must be filled for
4578 * each stream. substreams = 0 indicates that the stream doesn't exist.
4579 * When rates and/or formats are zero, the supported values are queried
4580 * from the given nid. The nid is used also by the default ops.prepare
4581 * and ops.cleanup callbacks.
4582 *
4583 * The driver needs to call ops.open in its open callback. Similarly,
4584 * ops.close is supposed to be called in the close callback.
4585 * ops.prepare should be called in the prepare or hw_params callback
4586 * with the proper parameters for set up.
4587 * ops.cleanup should be called in hw_free for clean up of streams.
4588 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004589 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 */
Takashi Iwai5cb543d2012-08-09 13:49:23 +02004591int snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004593 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594
Takashi Iwai0ba21762007-04-16 11:29:14 +02004595 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004596 int err = snd_hda_codec_build_pcms(codec);
4597 if (err < 0)
4598 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599 }
4600 return 0;
4601}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004602EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604/**
4605 * snd_hda_check_board_config - compare the current codec with the config table
4606 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004607 * @num_configs: number of config enums
4608 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 * @tbl: configuration table, terminated by null entries
4610 *
4611 * Compares the modelname or PCI subsystem id of the current codec with the
4612 * given configuration table. If a matching entry is found, returns its
4613 * config value (supposed to be 0 or positive).
4614 *
4615 * If no entries are matching, the function returns a negative value.
4616 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004617int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004618 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02004619 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620{
Takashi Iwaif44ac832008-07-30 15:01:45 +02004621 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004622 int i;
4623 for (i = 0; i < num_configs; i++) {
4624 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02004625 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004626 snd_printd(KERN_INFO "hda_codec: model '%s' is "
4627 "selected\n", models[i]);
4628 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629 }
4630 }
4631 }
4632
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004633 if (!codec->bus->pci || !tbl)
4634 return -1;
4635
4636 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
4637 if (!tbl)
4638 return -1;
4639 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02004640#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004641 char tmp[10];
4642 const char *model = NULL;
4643 if (models)
4644 model = models[tbl->value];
4645 if (!model) {
4646 sprintf(tmp, "#%d", tbl->value);
4647 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004649 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4650 "for config %x:%x (%s)\n",
4651 model, tbl->subvendor, tbl->subdevice,
4652 (tbl->name ? tbl->name : "Unknown device"));
4653#endif
4654 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 }
4656 return -1;
4657}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004658EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659
4660/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004661 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004662 subsystem ID with the
4663 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004664
4665 This is important for Gateway notebooks with SB450 HDA Audio
4666 where the vendor ID of the PCI device is:
4667 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4668 and the vendor/subvendor are found only at the codec.
4669
4670 * @codec: the HDA codec
4671 * @num_configs: number of config enums
4672 * @models: array of model name strings
4673 * @tbl: configuration table, terminated by null entries
4674 *
4675 * Compares the modelname or PCI subsystem id of the current codec with the
4676 * given configuration table. If a matching entry is found, returns its
4677 * config value (supposed to be 0 or positive).
4678 *
4679 * If no entries are matching, the function returns a negative value.
4680 */
4681int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004682 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004683 const struct snd_pci_quirk *tbl)
4684{
4685 const struct snd_pci_quirk *q;
4686
4687 /* Search for codec ID */
4688 for (q = tbl; q->subvendor; q++) {
Takashi Iwaie2301a42011-11-22 19:58:56 +01004689 unsigned int mask = 0xffff0000 | q->subdevice_mask;
4690 unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
4691 if ((codec->subsystem_id & mask) == id)
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004692 break;
4693 }
4694
4695 if (!q->subvendor)
4696 return -1;
4697
4698 tbl = q;
4699
4700 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02004701#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004702 char tmp[10];
4703 const char *model = NULL;
4704 if (models)
4705 model = models[tbl->value];
4706 if (!model) {
4707 sprintf(tmp, "#%d", tbl->value);
4708 model = tmp;
4709 }
4710 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4711 "for config %x:%x (%s)\n",
4712 model, tbl->subvendor, tbl->subdevice,
4713 (tbl->name ? tbl->name : "Unknown device"));
4714#endif
4715 return tbl->value;
4716 }
4717 return -1;
4718}
4719EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4720
4721/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 * snd_hda_add_new_ctls - create controls from the array
4723 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004724 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725 *
4726 * This helper function creates and add new controls in the given array.
4727 * The array must be terminated with an empty entry as terminator.
4728 *
4729 * Returns 0 if successful, or a negative error code.
4730 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004731int snd_hda_add_new_ctls(struct hda_codec *codec,
4732 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004734 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735
4736 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004737 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004738 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004739 if (knew->iface == -1) /* skip this codec private value */
4740 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004741 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004742 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004743 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004744 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004745 if (addr > 0)
4746 kctl->id.device = addr;
4747 if (idx > 0)
4748 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004749 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004750 if (!err)
4751 break;
4752 /* try first with another device index corresponding to
4753 * the codec addr; if it still fails (or it's the
4754 * primary codec), then try another control index
4755 */
4756 if (!addr && codec->addr)
4757 addr = codec->addr;
4758 else if (!idx && !knew->index) {
4759 idx = find_empty_mixer_ctl_idx(codec,
Takashi Iwaidcda5802012-10-12 17:24:51 +02004760 knew->name, 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004761 if (idx <= 0)
4762 return err;
4763 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004764 return err;
4765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 }
4767 return 0;
4768}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004769EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770
Takashi Iwai83012a72012-08-24 18:38:08 +02004771#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02004772static void hda_power_work(struct work_struct *work)
4773{
4774 struct hda_codec *codec =
4775 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004776 struct hda_bus *bus = codec->bus;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004777 unsigned int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004778
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004779 spin_lock(&codec->power_lock);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004780 if (codec->power_transition > 0) { /* during power-up sequence? */
4781 spin_unlock(&codec->power_lock);
4782 return;
4783 }
Maxim Levitsky2e492462007-09-03 15:26:57 +02004784 if (!codec->power_on || codec->power_count) {
4785 codec->power_transition = 0;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004786 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004787 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02004788 }
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004789 spin_unlock(&codec->power_lock);
4790
Dylan Reidd17344b2012-09-28 15:57:01 -07004791 state = hda_call_codec_suspend(codec, true);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004792 codec->pm_down_notified = 0;
4793 if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK)) {
4794 codec->pm_down_notified = 1;
Takashi Iwai68467f52012-08-28 09:14:29 -07004795 hda_call_pm_notify(bus, false);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004796 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004797}
4798
4799static void hda_keep_power_on(struct hda_codec *codec)
4800{
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004801 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004802 codec->power_count++;
4803 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004804 codec->power_jiffies = jiffies;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004805 spin_unlock(&codec->power_lock);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004806}
4807
Takashi Iwaid5191e52009-11-16 14:58:17 +01004808/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01004809void snd_hda_update_power_acct(struct hda_codec *codec)
4810{
4811 unsigned long delta = jiffies - codec->power_jiffies;
4812 if (codec->power_on)
4813 codec->power_on_acct += delta;
4814 else
4815 codec->power_off_acct += delta;
4816 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004817}
4818
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004819/* Transition to powered up, if wait_power_down then wait for a pending
4820 * transition to D3 to complete. A pending D3 transition is indicated
4821 * with power_transition == -1. */
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004822/* call this with codec->power_lock held! */
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004823static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004824{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004825 struct hda_bus *bus = codec->bus;
4826
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004827 /* Return if power_on or transitioning to power_on, unless currently
4828 * powering down. */
4829 if ((codec->power_on || codec->power_transition > 0) &&
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004830 !(wait_power_down && codec->power_transition < 0))
Takashi Iwaicb53c622007-08-10 17:21:45 +02004831 return;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004832 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004833
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004834 cancel_delayed_work_sync(&codec->power_work);
4835
4836 spin_lock(&codec->power_lock);
Dylan Reidb43d2242012-06-21 21:51:22 -07004837 /* If the power down delayed work was cancelled above before starting,
4838 * then there is no need to go through power up here.
4839 */
4840 if (codec->power_on) {
Takashi Iwai535b6c52012-08-20 21:25:22 +02004841 if (codec->power_transition < 0)
4842 codec->power_transition = 0;
Dylan Reidb43d2242012-06-21 21:51:22 -07004843 return;
4844 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004845
Takashi Iwaid66fee52011-08-02 15:39:31 +02004846 trace_hda_power_up(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004847 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004848 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004849 codec->power_jiffies = jiffies;
Takashi Iwai7f308302012-05-08 16:52:23 +02004850 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004851 spin_unlock(&codec->power_lock);
4852
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004853 if (codec->pm_down_notified) {
4854 codec->pm_down_notified = 0;
Takashi Iwai68467f52012-08-28 09:14:29 -07004855 hda_call_pm_notify(bus, true);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004856 }
4857
Takashi Iwaicb53c622007-08-10 17:21:45 +02004858 hda_call_codec_resume(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004859
4860 spin_lock(&codec->power_lock);
Takashi Iwaia221e282007-08-16 16:35:33 +02004861 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004862}
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004863
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004864#define power_save(codec) \
4865 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004866
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004867/* Transition to powered down */
4868static void __snd_hda_power_down(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004869{
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004870 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004871 return;
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004872
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004873 if (power_save(codec)) {
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004874 codec->power_transition = -1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01004875 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004876 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02004877 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004878}
4879
4880/**
4881 * snd_hda_power_save - Power-up/down/sync the codec
4882 * @codec: HD-audio codec
4883 * @delta: the counter delta to change
4884 *
4885 * Change the power-up counter via @delta, and power up or down the hardware
4886 * appropriately. For the power-down, queue to the delayed action.
4887 * Passing zero to @delta means to synchronize the power state.
4888 */
4889void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
4890{
4891 spin_lock(&codec->power_lock);
4892 codec->power_count += delta;
4893 trace_hda_power_count(codec);
4894 if (delta > 0)
4895 __snd_hda_power_up(codec, d3wait);
4896 else
4897 __snd_hda_power_down(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004898 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004899}
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004900EXPORT_SYMBOL_HDA(snd_hda_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004901
Takashi Iwaid5191e52009-11-16 14:58:17 +01004902/**
4903 * snd_hda_check_amp_list_power - Check the amp list and update the power
4904 * @codec: HD-audio codec
4905 * @check: the object containing an AMP list and the status
4906 * @nid: NID to check / update
4907 *
4908 * Check whether the given NID is in the amp list. If it's in the list,
4909 * check the current AMP status, and update the the power-status according
4910 * to the mute status.
4911 *
4912 * This function is supposed to be set or called from the check_power_status
4913 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004914 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004915int snd_hda_check_amp_list_power(struct hda_codec *codec,
4916 struct hda_loopback_check *check,
4917 hda_nid_t nid)
4918{
Takashi Iwai031024e2011-05-02 11:29:30 +02004919 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004920 int ch, v;
4921
4922 if (!check->amplist)
4923 return 0;
4924 for (p = check->amplist; p->nid; p++) {
4925 if (p->nid == nid)
4926 break;
4927 }
4928 if (!p->nid)
4929 return 0; /* nothing changed */
4930
4931 for (p = check->amplist; p->nid; p++) {
4932 for (ch = 0; ch < 2; ch++) {
4933 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4934 p->idx);
4935 if (!(v & HDA_AMP_MUTE) && v > 0) {
4936 if (!check->power_on) {
4937 check->power_on = 1;
4938 snd_hda_power_up(codec);
4939 }
4940 return 1;
4941 }
4942 }
4943 }
4944 if (check->power_on) {
4945 check->power_on = 0;
4946 snd_hda_power_down(codec);
4947 }
4948 return 0;
4949}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004950EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004951#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004953/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004954 * Channel mode helper
4955 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004956
4957/**
4958 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4959 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004960int snd_hda_ch_mode_info(struct hda_codec *codec,
4961 struct snd_ctl_elem_info *uinfo,
4962 const struct hda_channel_mode *chmode,
4963 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004964{
4965 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4966 uinfo->count = 1;
4967 uinfo->value.enumerated.items = num_chmodes;
4968 if (uinfo->value.enumerated.item >= num_chmodes)
4969 uinfo->value.enumerated.item = num_chmodes - 1;
4970 sprintf(uinfo->value.enumerated.name, "%dch",
4971 chmode[uinfo->value.enumerated.item].channels);
4972 return 0;
4973}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004974EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004975
Takashi Iwaid5191e52009-11-16 14:58:17 +01004976/**
4977 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4978 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004979int snd_hda_ch_mode_get(struct hda_codec *codec,
4980 struct snd_ctl_elem_value *ucontrol,
4981 const struct hda_channel_mode *chmode,
4982 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004983 int max_channels)
4984{
4985 int i;
4986
4987 for (i = 0; i < num_chmodes; i++) {
4988 if (max_channels == chmode[i].channels) {
4989 ucontrol->value.enumerated.item[0] = i;
4990 break;
4991 }
4992 }
4993 return 0;
4994}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004995EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004996
Takashi Iwaid5191e52009-11-16 14:58:17 +01004997/**
4998 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4999 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005000int snd_hda_ch_mode_put(struct hda_codec *codec,
5001 struct snd_ctl_elem_value *ucontrol,
5002 const struct hda_channel_mode *chmode,
5003 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005004 int *max_channelsp)
5005{
5006 unsigned int mode;
5007
5008 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01005009 if (mode >= num_chmodes)
5010 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005011 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005012 return 0;
5013 /* change the current channel setting */
5014 *max_channelsp = chmode[mode].channels;
5015 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005016 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005017 return 1;
5018}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005019EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005020
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021/*
5022 * input MUX helper
5023 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01005024
5025/**
5026 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
5027 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005028int snd_hda_input_mux_info(const struct hda_input_mux *imux,
5029 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030{
5031 unsigned int index;
5032
5033 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5034 uinfo->count = 1;
5035 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02005036 if (!imux->num_items)
5037 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038 index = uinfo->value.enumerated.item;
5039 if (index >= imux->num_items)
5040 index = imux->num_items - 1;
5041 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
5042 return 0;
5043}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005044EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045
Takashi Iwaid5191e52009-11-16 14:58:17 +01005046/**
5047 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
5048 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005049int snd_hda_input_mux_put(struct hda_codec *codec,
5050 const struct hda_input_mux *imux,
5051 struct snd_ctl_elem_value *ucontrol,
5052 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053 unsigned int *cur_val)
5054{
5055 unsigned int idx;
5056
Takashi Iwai5513b0c2007-10-09 11:58:41 +02005057 if (!imux->num_items)
5058 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059 idx = ucontrol->value.enumerated.item[0];
5060 if (idx >= imux->num_items)
5061 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005062 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005064 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
5065 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066 *cur_val = idx;
5067 return 1;
5068}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005069EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070
5071
5072/*
Takashi Iwaidda415d2012-11-30 18:34:38 +01005073 * process kcontrol info callback of a simple string enum array
5074 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
5075 */
5076int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
5077 struct snd_ctl_elem_info *uinfo,
5078 int num_items, const char * const *texts)
5079{
5080 static const char * const texts_default[] = {
5081 "Disabled", "Enabled"
5082 };
5083
5084 if (!texts || !num_items) {
5085 num_items = 2;
5086 texts = texts_default;
5087 }
5088
5089 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5090 uinfo->count = 1;
5091 uinfo->value.enumerated.items = num_items;
5092 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
5093 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
5094 strcpy(uinfo->value.enumerated.name,
5095 texts[uinfo->value.enumerated.item]);
5096 return 0;
5097}
5098EXPORT_SYMBOL_HDA(snd_hda_enum_helper_info);
5099
5100/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101 * Multi-channel / digital-out PCM helper functions
5102 */
5103
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005104/* setup SPDIF output stream */
5105static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
5106 unsigned int stream_tag, unsigned int format)
5107{
Laurence Darby3bef1c32012-11-03 17:00:06 +00005108 struct hda_spdif_out *spdif;
5109 unsigned int curr_fmt;
5110 bool reset;
Stephen Warren7c935972011-06-01 11:14:17 -06005111
Laurence Darby3bef1c32012-11-03 17:00:06 +00005112 spdif = snd_hda_spdif_out_of_nid(codec, nid);
5113 curr_fmt = snd_hda_codec_read(codec, nid, 0,
5114 AC_VERB_GET_STREAM_FORMAT, 0);
5115 reset = codec->spdif_status_reset &&
5116 (spdif->ctls & AC_DIG1_ENABLE) &&
5117 curr_fmt != format;
5118
5119 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
5120 updated */
5121 if (reset)
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005122 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06005123 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02005124 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005125 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02005126 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02005127 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02005128 for (d = codec->slave_dig_outs; *d; d++)
5129 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
5130 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005131 }
Takashi Iwai2f728532008-09-25 16:32:41 +02005132 /* turn on again (if needed) */
Laurence Darby3bef1c32012-11-03 17:00:06 +00005133 if (reset)
Takashi Iwai2f728532008-09-25 16:32:41 +02005134 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06005135 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02005136}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005137
Takashi Iwai2f728532008-09-25 16:32:41 +02005138static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
5139{
5140 snd_hda_codec_cleanup_stream(codec, nid);
5141 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02005142 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02005143 for (d = codec->slave_dig_outs; *d; d++)
5144 snd_hda_codec_cleanup_stream(codec, *d);
5145 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005146}
5147
Takashi Iwaid5191e52009-11-16 14:58:17 +01005148/**
5149 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
5150 * @bus: HD-audio bus
5151 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005152void snd_hda_bus_reboot_notify(struct hda_bus *bus)
5153{
5154 struct hda_codec *codec;
5155
5156 if (!bus)
5157 return;
5158 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005159 if (hda_codec_is_power_on(codec) &&
5160 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005161 codec->patch_ops.reboot_notify(codec);
5162 }
5163}
Takashi Iwai8f217a22009-11-10 18:26:12 +01005164EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005165
Takashi Iwaid5191e52009-11-16 14:58:17 +01005166/**
5167 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005169int snd_hda_multi_out_dig_open(struct hda_codec *codec,
5170 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171{
Ingo Molnar62932df2006-01-16 16:34:20 +01005172 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02005173 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
5174 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02005175 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01005177 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178 return 0;
5179}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005180EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181
Takashi Iwaid5191e52009-11-16 14:58:17 +01005182/**
5183 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
5184 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005185int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
5186 struct hda_multi_out *mout,
5187 unsigned int stream_tag,
5188 unsigned int format,
5189 struct snd_pcm_substream *substream)
5190{
5191 mutex_lock(&codec->spdif_mutex);
5192 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
5193 mutex_unlock(&codec->spdif_mutex);
5194 return 0;
5195}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005196EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005197
Takashi Iwaid5191e52009-11-16 14:58:17 +01005198/**
5199 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
5200 */
Takashi Iwai9411e212009-02-13 11:32:28 +01005201int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
5202 struct hda_multi_out *mout)
5203{
5204 mutex_lock(&codec->spdif_mutex);
5205 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5206 mutex_unlock(&codec->spdif_mutex);
5207 return 0;
5208}
5209EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
5210
Takashi Iwaid5191e52009-11-16 14:58:17 +01005211/**
5212 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005214int snd_hda_multi_out_dig_close(struct hda_codec *codec,
5215 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216{
Ingo Molnar62932df2006-01-16 16:34:20 +01005217 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01005219 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220 return 0;
5221}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005222EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223
Takashi Iwaid5191e52009-11-16 14:58:17 +01005224/**
5225 * snd_hda_multi_out_analog_open - open analog outputs
5226 *
5227 * Open analog outputs and set up the hw-constraints.
5228 * If the digital outputs can be opened as slave, open the digital
5229 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005231int snd_hda_multi_out_analog_open(struct hda_codec *codec,
5232 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01005233 struct snd_pcm_substream *substream,
5234 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235{
Takashi Iwai9a081602008-02-12 18:37:26 +01005236 struct snd_pcm_runtime *runtime = substream->runtime;
5237 runtime->hw.channels_max = mout->max_channels;
5238 if (mout->dig_out_nid) {
5239 if (!mout->analog_rates) {
5240 mout->analog_rates = hinfo->rates;
5241 mout->analog_formats = hinfo->formats;
5242 mout->analog_maxbps = hinfo->maxbps;
5243 } else {
5244 runtime->hw.rates = mout->analog_rates;
5245 runtime->hw.formats = mout->analog_formats;
5246 hinfo->maxbps = mout->analog_maxbps;
5247 }
5248 if (!mout->spdif_rates) {
5249 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
5250 &mout->spdif_rates,
5251 &mout->spdif_formats,
5252 &mout->spdif_maxbps);
5253 }
5254 mutex_lock(&codec->spdif_mutex);
5255 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02005256 if ((runtime->hw.rates & mout->spdif_rates) &&
5257 (runtime->hw.formats & mout->spdif_formats)) {
5258 runtime->hw.rates &= mout->spdif_rates;
5259 runtime->hw.formats &= mout->spdif_formats;
5260 if (mout->spdif_maxbps < hinfo->maxbps)
5261 hinfo->maxbps = mout->spdif_maxbps;
5262 } else {
5263 mout->share_spdif = 0;
5264 /* FIXME: need notify? */
5265 }
Takashi Iwai9a081602008-02-12 18:37:26 +01005266 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02005267 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01005268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269 return snd_pcm_hw_constraint_step(substream->runtime, 0,
5270 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
5271}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005272EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273
Takashi Iwaid5191e52009-11-16 14:58:17 +01005274/**
5275 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
5276 *
5277 * Set up the i/o for analog out.
5278 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005280int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5281 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282 unsigned int stream_tag,
5283 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01005284 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285{
Takashi Iwaidda14412011-05-02 11:29:30 +02005286 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005288 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289 int i;
5290
Ingo Molnar62932df2006-01-16 16:34:20 +01005291 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005292 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01005293 if (mout->dig_out_nid && mout->share_spdif &&
5294 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02005296 snd_hda_is_supported_format(codec, mout->dig_out_nid,
5297 format) &&
Stephen Warren7c935972011-06-01 11:14:17 -06005298 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005300 setup_dig_out_stream(codec, mout->dig_out_nid,
5301 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302 } else {
5303 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02005304 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305 }
5306 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005307 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308
5309 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005310 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5311 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005312 if (!mout->no_share_stream &&
5313 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005315 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5316 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005317 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005318 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5319 if (!mout->no_share_stream && mout->hp_out_nid[i])
5320 snd_hda_codec_setup_stream(codec,
5321 mout->hp_out_nid[i],
5322 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005323 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02005324 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01005325 snd_hda_codec_setup_stream(codec,
5326 mout->extra_out_nid[i],
5327 stream_tag, 0, format);
5328
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329 /* surrounds */
5330 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02005331 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005332 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5333 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005334 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005335 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5336 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337 }
5338 return 0;
5339}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005340EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341
Takashi Iwaid5191e52009-11-16 14:58:17 +01005342/**
5343 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005345int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5346 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347{
Takashi Iwaidda14412011-05-02 11:29:30 +02005348 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349 int i;
5350
5351 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01005352 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01005354 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005355 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5356 if (mout->hp_out_nid[i])
5357 snd_hda_codec_cleanup_stream(codec,
5358 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005359 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5360 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01005361 snd_hda_codec_cleanup_stream(codec,
5362 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01005363 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02005365 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366 mout->dig_out_used = 0;
5367 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005368 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369 return 0;
5370}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005371EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372
Takashi Iwai47408602012-04-20 13:06:53 +02005373/**
5374 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
5375 *
5376 * Guess the suitable VREF pin bits to be set as the pin-control value.
5377 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5378 */
5379unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5380{
5381 unsigned int pincap;
5382 unsigned int oldval;
5383 oldval = snd_hda_codec_read(codec, pin, 0,
5384 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5385 pincap = snd_hda_query_pin_caps(codec, pin);
5386 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5387 /* Exception: if the default pin setup is vref50, we give it priority */
5388 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5389 return AC_PINCTL_VREF_80;
5390 else if (pincap & AC_PINCAP_VREF_50)
5391 return AC_PINCTL_VREF_50;
5392 else if (pincap & AC_PINCAP_VREF_100)
5393 return AC_PINCTL_VREF_100;
5394 else if (pincap & AC_PINCAP_VREF_GRD)
5395 return AC_PINCTL_VREF_GRD;
5396 return AC_PINCTL_VREF_HIZ;
5397}
5398EXPORT_SYMBOL_HDA(snd_hda_get_default_vref);
5399
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005400/* correct the pin ctl value for matching with the pin cap */
5401unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
5402 hda_nid_t pin, unsigned int val)
5403{
5404 static unsigned int cap_lists[][2] = {
5405 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
5406 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
5407 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
5408 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
5409 };
5410 unsigned int cap;
5411
5412 if (!val)
5413 return 0;
5414 cap = snd_hda_query_pin_caps(codec, pin);
5415 if (!cap)
5416 return val; /* don't know what to do... */
5417
5418 if (val & AC_PINCTL_OUT_EN) {
5419 if (!(cap & AC_PINCAP_OUT))
5420 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5421 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
5422 val &= ~AC_PINCTL_HP_EN;
5423 }
5424
5425 if (val & AC_PINCTL_IN_EN) {
5426 if (!(cap & AC_PINCAP_IN))
5427 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5428 else {
5429 unsigned int vcap, vref;
5430 int i;
5431 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5432 vref = val & AC_PINCTL_VREFEN;
5433 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
5434 if (vref == cap_lists[i][0] &&
5435 !(vcap & cap_lists[i][1])) {
5436 if (i == ARRAY_SIZE(cap_lists) - 1)
5437 vref = AC_PINCTL_VREF_HIZ;
5438 else
5439 vref = cap_lists[i + 1][0];
5440 }
5441 }
5442 val &= ~AC_PINCTL_VREFEN;
5443 val |= vref;
5444 }
5445 }
5446
5447 return val;
5448}
5449EXPORT_SYMBOL_HDA(snd_hda_correct_pin_ctl);
5450
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005451int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5452 unsigned int val, bool cached)
5453{
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005454 val = snd_hda_correct_pin_ctl(codec, pin, val);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01005455 snd_hda_codec_set_pin_target(codec, pin, val);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005456 if (cached)
5457 return snd_hda_codec_update_cache(codec, pin, 0,
5458 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5459 else
5460 return snd_hda_codec_write(codec, pin, 0,
5461 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5462}
5463EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl);
5464
Takashi Iwai990061c2010-09-09 22:08:44 +02005465/**
5466 * snd_hda_add_imux_item - Add an item to input_mux
5467 *
5468 * When the same label is used already in the existing items, the number
5469 * suffix is appended to the label. This label index number is stored
5470 * to type_idx when non-NULL pointer is given.
5471 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005472int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5473 int index, int *type_idx)
5474{
5475 int i, label_idx = 0;
5476 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5477 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5478 return -EINVAL;
5479 }
5480 for (i = 0; i < imux->num_items; i++) {
5481 if (!strncmp(label, imux->items[i].label, strlen(label)))
5482 label_idx++;
5483 }
5484 if (type_idx)
5485 *type_idx = label_idx;
5486 if (label_idx > 0)
5487 snprintf(imux->items[imux->num_items].label,
5488 sizeof(imux->items[imux->num_items].label),
5489 "%s %d", label, label_idx);
5490 else
5491 strlcpy(imux->items[imux->num_items].label, label,
5492 sizeof(imux->items[imux->num_items].label));
5493 imux->items[imux->num_items].index = index;
5494 imux->num_items++;
5495 return 0;
5496}
5497EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005498
Takashi Iwai4a471b72005-12-07 13:56:29 +01005499
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500#ifdef CONFIG_PM
5501/*
5502 * power management
5503 */
5504
5505/**
5506 * snd_hda_suspend - suspend the codecs
5507 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508 *
5509 * Returns 0 if successful.
5510 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005511int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005513 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514
Takashi Iwai0ba21762007-04-16 11:29:14 +02005515 list_for_each_entry(codec, &bus->codec_list, list) {
David Henningsson26a6cb62012-10-09 15:04:21 +02005516 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005517 if (hda_codec_is_power_on(codec))
Dylan Reidd17344b2012-09-28 15:57:01 -07005518 hda_call_codec_suspend(codec, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519 }
5520 return 0;
5521}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005522EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523
5524/**
5525 * snd_hda_resume - resume the codecs
5526 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527 *
5528 * Returns 0 if successful.
5529 */
5530int snd_hda_resume(struct hda_bus *bus)
5531{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005532 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533
Takashi Iwai0ba21762007-04-16 11:29:14 +02005534 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai7f308302012-05-08 16:52:23 +02005535 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537 return 0;
5538}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005539EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005540#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005541
5542/*
5543 * generic arrays
5544 */
5545
Takashi Iwaid5191e52009-11-16 14:58:17 +01005546/**
5547 * snd_array_new - get a new element from the given array
5548 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005549 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005550 * Get a new element from the given array. If it exceeds the
5551 * pre-allocated array size, re-allocate the array.
5552 *
5553 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005554 */
5555void *snd_array_new(struct snd_array *array)
5556{
Takashi Iwai12f17712012-10-10 08:59:14 +02005557 if (snd_BUG_ON(!array->elem_size))
5558 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005559 if (array->used >= array->alloced) {
5560 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005561 int size = (num + 1) * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005562 void *nlist;
5563 if (snd_BUG_ON(num >= 4096))
5564 return NULL;
Takashi Iwai5265fd92013-03-13 12:15:28 +01005565 nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005566 if (!nlist)
5567 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005568 array->list = nlist;
5569 array->alloced = num;
5570 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005571 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005572}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005573EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005574
Takashi Iwaid5191e52009-11-16 14:58:17 +01005575/**
5576 * snd_array_free - free the given array elements
5577 * @array: the array object
5578 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005579void snd_array_free(struct snd_array *array)
5580{
5581 kfree(array->list);
5582 array->used = 0;
5583 array->alloced = 0;
5584 array->list = NULL;
5585}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005586EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005587
Takashi Iwaid5191e52009-11-16 14:58:17 +01005588/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005589 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5590 * @pcm: PCM caps bits
5591 * @buf: the string buffer to write
5592 * @buflen: the max buffer length
5593 *
5594 * used by hda_proc.c and hda_eld.c
5595 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005596void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5597{
5598 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5599 int i, j;
5600
5601 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5602 if (pcm & (AC_SUPPCM_BITS_8 << i))
5603 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5604
5605 buf[j] = '\0'; /* necessary when j == 0 */
5606}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005607EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005608
5609MODULE_DESCRIPTION("HDA codec core");
5610MODULE_LICENSE("GPL");