blob: 4aba7646dd9c4999dafa488c7482ee4c65740167 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Universal Interface for Intel High Definition Audio Codec
3 *
4 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
5 *
6 *
7 * This driver is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This driver is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
Takashi Iwai18478e82012-03-09 17:51:10 +010022#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/init.h>
24#include <linux/delay.h>
25#include <linux/slab.h>
26#include <linux/pci.h>
Ingo Molnar62932df2006-01-16 16:34:20 +010027#include <linux/mutex.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040028#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <sound/core.h>
30#include "hda_codec.h"
31#include <sound/asoundef.h>
Jaroslav Kysela302e9c52006-07-05 17:39:49 +020032#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <sound/initval.h>
Takashi Iwaicd372fb2011-03-03 14:40:14 +010034#include <sound/jack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "hda_local.h"
Jaroslav Kysela123c07a2009-10-21 14:48:23 +020036#include "hda_beep.h"
Takashi Iwai1835a0f2011-10-27 22:12:46 +020037#include "hda_jack.h"
Takashi Iwai28073142007-07-27 18:58:06 +020038#include <sound/hda_hwdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Takashi Iwaid66fee52011-08-02 15:39:31 +020040#define CREATE_TRACE_POINTS
41#include "hda_trace.h"
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/*
44 * vendor / preset table
45 */
46
47struct hda_vendor_id {
48 unsigned int id;
49 const char *name;
50};
51
52/* codec vendor labels */
53static struct hda_vendor_id hda_vendor_ids[] = {
Takashi Iwaic8cd1282008-02-13 16:59:29 +010054 { 0x1002, "ATI" },
Takashi Iwaie5f14242009-07-01 18:11:44 +020055 { 0x1013, "Cirrus Logic" },
Takashi Iwaia9226252006-09-17 22:05:54 +020056 { 0x1057, "Motorola" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010057 { 0x1095, "Silicon Image" },
Takashi Iwai31117b72008-12-16 14:43:21 +010058 { 0x10de, "Nvidia" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010059 { 0x10ec, "Realtek" },
Takashi Iwai4e01f542009-04-16 08:53:34 +020060 { 0x1102, "Creative" },
Joseph Chanc577b8a2006-11-29 15:29:40 +010061 { 0x1106, "VIA" },
Matthew Ranostay7f168592007-10-18 17:38:17 +020062 { 0x111d, "IDT" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010063 { 0x11c1, "LSI" },
Takashi Iwai54b903e2005-05-15 14:30:10 +020064 { 0x11d4, "Analog Devices" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 { 0x13f6, "C-Media" },
Takashi Iwaia9226252006-09-17 22:05:54 +020066 { 0x14f1, "Conexant" },
Takashi Iwaic8cd1282008-02-13 16:59:29 +010067 { 0x17e8, "Chrontel" },
68 { 0x1854, "LG" },
Mark Brown8199de32008-10-28 14:50:13 +000069 { 0x1aec, "Wolfson Microelectronics" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 { 0x434d, "C-Media" },
Takashi Iwai74c61132008-12-18 09:11:33 +010071 { 0x8086, "Intel" },
Matt2f2f4252005-04-13 14:45:30 +020072 { 0x8384, "SigmaTel" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 {} /* terminator */
74};
75
Takashi Iwai1289e9e2008-11-27 15:47:11 +010076static DEFINE_MUTEX(preset_mutex);
77static LIST_HEAD(hda_preset_tables);
78
79int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
80{
81 mutex_lock(&preset_mutex);
82 list_add_tail(&preset->list, &hda_preset_tables);
83 mutex_unlock(&preset_mutex);
84 return 0;
85}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010086EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset);
Takashi Iwai1289e9e2008-11-27 15:47:11 +010087
88int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
89{
90 mutex_lock(&preset_mutex);
91 list_del(&preset->list);
92 mutex_unlock(&preset_mutex);
93 return 0;
94}
Takashi Iwaiff7a3262008-11-28 15:17:06 +010095EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Takashi Iwai83012a72012-08-24 18:38:08 +020097#ifdef CONFIG_PM
Takashi Iwaid846b172012-11-24 11:58:24 +010098#define codec_in_pm(codec) ((codec)->in_pm)
Takashi Iwaicb53c622007-08-10 17:21:45 +020099static void hda_power_work(struct work_struct *work);
100static void hda_keep_power_on(struct hda_codec *codec);
Takashi Iwaie581f3d2011-07-26 10:19:20 +0200101#define hda_codec_is_power_on(codec) ((codec)->power_on)
Takashi Iwai68467f52012-08-28 09:14:29 -0700102static inline void hda_call_pm_notify(struct hda_bus *bus, bool power_up)
103{
104 if (bus->ops.pm_notify)
105 bus->ops.pm_notify(bus, power_up);
106}
Takashi Iwaicb53c622007-08-10 17:21:45 +0200107#else
Takashi Iwaid846b172012-11-24 11:58:24 +0100108#define codec_in_pm(codec) 0
Takashi Iwaicb53c622007-08-10 17:21:45 +0200109static inline void hda_keep_power_on(struct hda_codec *codec) {}
Takashi Iwaie581f3d2011-07-26 10:19:20 +0200110#define hda_codec_is_power_on(codec) 1
Takashi Iwai68467f52012-08-28 09:14:29 -0700111#define hda_call_pm_notify(bus, state) {}
Takashi Iwaicb53c622007-08-10 17:21:45 +0200112#endif
113
Takashi Iwaid5191e52009-11-16 14:58:17 +0100114/**
115 * snd_hda_get_jack_location - Give a location string of the jack
116 * @cfg: pin default config value
117 *
118 * Parse the pin default config value and returns the string of the
119 * jack location, e.g. "Rear", "Front", etc.
120 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400121const char *snd_hda_get_jack_location(u32 cfg)
122{
123 static char *bases[7] = {
124 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
125 };
126 static unsigned char specials_idx[] = {
127 0x07, 0x08,
128 0x17, 0x18, 0x19,
129 0x37, 0x38
130 };
131 static char *specials[] = {
132 "Rear Panel", "Drive Bar",
133 "Riser", "HDMI", "ATAPI",
134 "Mobile-In", "Mobile-Out"
135 };
136 int i;
137 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
138 if ((cfg & 0x0f) < 7)
139 return bases[cfg & 0x0f];
140 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
141 if (cfg == specials_idx[i])
142 return specials[i];
143 }
144 return "UNKNOWN";
145}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100146EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400147
Takashi Iwaid5191e52009-11-16 14:58:17 +0100148/**
149 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
150 * @cfg: pin default config value
151 *
152 * Parse the pin default config value and returns the string of the
153 * jack connectivity, i.e. external or internal connection.
154 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400155const char *snd_hda_get_jack_connectivity(u32 cfg)
156{
157 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
158
159 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
160}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100161EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400162
Takashi Iwaid5191e52009-11-16 14:58:17 +0100163/**
164 * snd_hda_get_jack_type - Give a type string of the jack
165 * @cfg: pin default config value
166 *
167 * Parse the pin default config value and returns the string of the
168 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
169 */
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400170const char *snd_hda_get_jack_type(u32 cfg)
171{
172 static char *jack_types[16] = {
173 "Line Out", "Speaker", "HP Out", "CD",
174 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
175 "Line In", "Aux", "Mic", "Telephony",
David Henningssonaeb3a972013-04-04 11:47:13 +0200176 "SPDIF In", "Digital In", "Reserved", "Other"
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400177 };
178
179 return jack_types[(cfg & AC_DEFCFG_DEVICE)
180 >> AC_DEFCFG_DEVICE_SHIFT];
181}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100182EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
Matthew Ranostay50a9f792008-10-25 01:05:45 -0400183
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100184/*
185 * Compose a 32bit command word to be sent to the HD-audio controller
186 */
187static inline unsigned int
188make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
189 unsigned int verb, unsigned int parm)
190{
191 u32 val;
192
Takashi Iwai82e1b802009-07-17 12:47:34 +0200193 if ((codec->addr & ~0xf) || (direct & ~1) || (nid & ~0x7f) ||
194 (verb & ~0xfff) || (parm & ~0xffff)) {
Wu Fengguang6430aee2009-07-17 16:49:19 +0800195 printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
196 codec->addr, direct, nid, verb, parm);
197 return ~0;
198 }
199
200 val = (u32)codec->addr << 28;
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100201 val |= (u32)direct << 27;
202 val |= (u32)nid << 20;
203 val |= verb << 8;
204 val |= parm;
205 return val;
206}
207
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200208/*
209 * Send and receive a verb
210 */
211static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
212 unsigned int *res)
213{
214 struct hda_bus *bus = codec->bus;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200215 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200216
Wu Fengguang6430aee2009-07-17 16:49:19 +0800217 if (cmd == ~0)
218 return -1;
219
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200220 if (res)
221 *res = -1;
Takashi Iwai8dd78332009-06-02 01:16:07 +0200222 again:
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200223 snd_hda_power_up(codec);
224 mutex_lock(&bus->cmd_mutex);
Takashi Iwai3bcce5c2012-12-20 11:17:17 +0100225 for (;;) {
226 trace_hda_send_cmd(codec, cmd);
227 err = bus->ops.command(bus, cmd);
228 if (err != -EAGAIN)
229 break;
230 /* process pending verbs */
231 bus->ops.get_response(bus, codec->addr);
232 }
Takashi Iwaid66fee52011-08-02 15:39:31 +0200233 if (!err && res) {
Wu Fengguangdeadff12009-08-01 18:45:16 +0800234 *res = bus->ops.get_response(bus, codec->addr);
Takashi Iwaid66fee52011-08-02 15:39:31 +0200235 trace_hda_get_response(codec, *res);
236 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200237 mutex_unlock(&bus->cmd_mutex);
238 snd_hda_power_down(codec);
Takashi Iwaid846b172012-11-24 11:58:24 +0100239 if (!codec_in_pm(codec) && res && *res == -1 && bus->rirb_error) {
Takashi Iwai8dd78332009-06-02 01:16:07 +0200240 if (bus->response_reset) {
241 snd_printd("hda_codec: resetting BUS due to "
242 "fatal communication error\n");
Takashi Iwaid66fee52011-08-02 15:39:31 +0200243 trace_hda_bus_reset(bus);
Takashi Iwai8dd78332009-06-02 01:16:07 +0200244 bus->ops.bus_reset(bus);
245 }
246 goto again;
247 }
248 /* clear reset-flag when the communication gets recovered */
Takashi Iwaid846b172012-11-24 11:58:24 +0100249 if (!err || codec_in_pm(codec))
Takashi Iwai8dd78332009-06-02 01:16:07 +0200250 bus->response_reset = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200251 return err;
252}
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254/**
255 * snd_hda_codec_read - send a command and get the response
256 * @codec: the HDA codec
257 * @nid: NID to send the command
258 * @direct: direct flag
259 * @verb: the verb to send
260 * @parm: the parameter for the verb
261 *
262 * Send a single command and read the corresponding response.
263 *
264 * Returns the obtained response value, or -1 for an error.
265 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200266unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
267 int direct,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 unsigned int verb, unsigned int parm)
269{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200270 unsigned cmd = make_codec_cmd(codec, nid, direct, verb, parm);
271 unsigned int res;
Greg Thelen9857edf2011-06-13 07:45:45 -0700272 if (codec_exec_verb(codec, cmd, &res))
273 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 return res;
275}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100276EXPORT_SYMBOL_HDA(snd_hda_codec_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278/**
279 * snd_hda_codec_write - send a single command without waiting for response
280 * @codec: the HDA codec
281 * @nid: NID to send the command
282 * @direct: direct flag
283 * @verb: the verb to send
284 * @parm: the parameter for the verb
285 *
286 * Send a single command without waiting for response.
287 *
288 * Returns 0 if successful, or a negative error code.
289 */
290int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
291 unsigned int verb, unsigned int parm)
292{
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200293 unsigned int cmd = make_codec_cmd(codec, nid, direct, verb, parm);
Takashi Iwai33fa35e2008-11-06 16:50:40 +0100294 unsigned int res;
Takashi Iwaib20f3b82009-06-02 01:20:22 +0200295 return codec_exec_verb(codec, cmd,
296 codec->bus->sync_write ? &res : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100298EXPORT_SYMBOL_HDA(snd_hda_codec_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300/**
301 * snd_hda_sequence_write - sequence writes
302 * @codec: the HDA codec
303 * @seq: VERB array to send
304 *
305 * Send the commands sequentially from the given array.
306 * The array must be terminated with NID=0.
307 */
308void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
309{
310 for (; seq->nid; seq++)
311 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
312}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100313EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315/**
316 * snd_hda_get_sub_nodes - get the range of sub nodes
317 * @codec: the HDA codec
318 * @nid: NID to parse
319 * @start_id: the pointer to store the start NID
320 *
321 * Parse the NID and store the start NID of its sub-nodes.
322 * Returns the number of sub-nodes.
323 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200324int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
325 hda_nid_t *start_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
327 unsigned int parm;
328
329 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
Danny Tholene8a7f132007-09-11 21:41:56 +0200330 if (parm == -1)
331 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 *start_id = (parm >> 16) & 0x7fff;
333 return (int)(parm & 0x7fff);
334}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100335EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100337/* connection list element */
338struct hda_conn_list {
339 struct list_head list;
340 int len;
341 hda_nid_t nid;
342 hda_nid_t conns[0];
343};
344
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200345/* look up the cached results */
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100346static struct hda_conn_list *
347lookup_conn_list(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200348{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100349 struct hda_conn_list *p;
350 list_for_each_entry(p, &codec->conn_list, list) {
351 if (p->nid == nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200352 return p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200353 }
354 return NULL;
355}
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200356
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100357static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
358 const hda_nid_t *list)
359{
360 struct hda_conn_list *p;
361
362 p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL);
363 if (!p)
364 return -ENOMEM;
365 p->len = len;
366 p->nid = nid;
367 memcpy(p->conns, list, len * sizeof(hda_nid_t));
368 list_add(&p->list, &codec->conn_list);
369 return 0;
370}
371
372static void remove_conn_list(struct hda_codec *codec)
373{
374 while (!list_empty(&codec->conn_list)) {
375 struct hda_conn_list *p;
376 p = list_first_entry(&codec->conn_list, typeof(*p), list);
377 list_del(&p->list);
378 kfree(p);
379 }
380}
381
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200382/* read the connection and add to the cache */
383static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200384{
Takashi Iwai4eea3092013-02-07 18:18:19 +0100385 hda_nid_t list[32];
386 hda_nid_t *result = list;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200387 int len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200388
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200389 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
Takashi Iwai4eea3092013-02-07 18:18:19 +0100390 if (len == -ENOSPC) {
391 len = snd_hda_get_num_raw_conns(codec, nid);
392 result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL);
393 if (!result)
394 return -ENOMEM;
395 len = snd_hda_get_raw_connections(codec, nid, result, len);
396 }
397 if (len >= 0)
398 len = snd_hda_override_conn_list(codec, nid, len, result);
399 if (result != list)
400 kfree(result);
401 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200402}
Takashi Iwaidce20792011-06-24 14:10:28 +0200403
404/**
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100405 * snd_hda_get_conn_list - get connection list
406 * @codec: the HDA codec
407 * @nid: NID to parse
408 * @len: number of connection list entries
409 * @listp: the pointer to store NID list
410 *
411 * Parses the connection list of the given widget and stores the pointer
412 * to the list of NIDs.
413 *
414 * Returns the number of connections, or a negative error code.
415 *
416 * Note that the returned pointer isn't protected against the list
417 * modification. If snd_hda_override_conn_list() might be called
418 * concurrently, protect with a mutex appropriately.
419 */
420int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
421 const hda_nid_t **listp)
422{
423 bool added = false;
424
425 for (;;) {
426 int err;
427 const struct hda_conn_list *p;
428
429 /* if the connection-list is already cached, read it */
430 p = lookup_conn_list(codec, nid);
431 if (p) {
432 if (listp)
433 *listp = p->conns;
434 return p->len;
435 }
436 if (snd_BUG_ON(added))
437 return -EINVAL;
438
439 err = read_and_add_raw_conns(codec, nid);
440 if (err < 0)
441 return err;
442 added = true;
443 }
444}
445EXPORT_SYMBOL_HDA(snd_hda_get_conn_list);
446
447/**
Takashi Iwaidce20792011-06-24 14:10:28 +0200448 * snd_hda_get_connections - copy connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 * @codec: the HDA codec
450 * @nid: NID to parse
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200451 * @conn_list: connection list array; when NULL, checks only the size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 * @max_conns: max. number of connections to store
453 *
454 * Parses the connection list of the given widget and stores the list
455 * of NIDs.
456 *
457 * Returns the number of connections, or a negative error code.
458 */
459int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200460 hda_nid_t *conn_list, int max_conns)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200461{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100462 const hda_nid_t *list;
463 int len = snd_hda_get_conn_list(codec, nid, &list);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200464
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100465 if (len > 0 && conn_list) {
466 if (len > max_conns) {
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200467 snd_printk(KERN_ERR "hda_codec: "
468 "Too many connections %d for NID 0x%x\n",
469 len, nid);
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200470 return -EINVAL;
471 }
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100472 memcpy(conn_list, list, len * sizeof(hda_nid_t));
Takashi Iwaidce20792011-06-24 14:10:28 +0200473 }
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200474
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100475 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200476}
477EXPORT_SYMBOL_HDA(snd_hda_get_connections);
478
Takashi Iwai4eea3092013-02-07 18:18:19 +0100479/* return CONNLIST_LEN parameter of the given widget */
480static unsigned int get_num_conns(struct hda_codec *codec, hda_nid_t nid)
481{
482 unsigned int wcaps = get_wcaps(codec, nid);
483 unsigned int parm;
484
485 if (!(wcaps & AC_WCAP_CONN_LIST) &&
486 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
487 return 0;
488
489 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
490 if (parm == -1)
491 parm = 0;
492 return parm;
493}
494
495int snd_hda_get_num_raw_conns(struct hda_codec *codec, hda_nid_t nid)
496{
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100497 return snd_hda_get_raw_connections(codec, nid, NULL, 0);
Takashi Iwai4eea3092013-02-07 18:18:19 +0100498}
499
Takashi Iwai9e7717c2011-07-11 15:42:52 +0200500/**
501 * snd_hda_get_raw_connections - copy connection list without cache
502 * @codec: the HDA codec
503 * @nid: NID to parse
504 * @conn_list: connection list array
505 * @max_conns: max. number of connections to store
506 *
507 * Like snd_hda_get_connections(), copy the connection list but without
508 * checking through the connection-list cache.
509 * Currently called only from hda_proc.c, so not exported.
510 */
511int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
512 hda_nid_t *conn_list, int max_conns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
514 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100515 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 unsigned int shift, num_elems, mask;
Takashi Iwai54d17402005-11-21 16:33:22 +0100517 hda_nid_t prev_nid;
Takashi Iwai5fdaecd2012-12-20 10:45:55 +0100518 int null_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Takashi Iwai4eea3092013-02-07 18:18:19 +0100520 parm = get_num_conns(codec, nid);
521 if (!parm)
Takashi Iwai8d087c72011-06-28 12:45:47 +0200522 return 0;
Jaroslav Kysela16a433d2009-07-22 16:20:40 +0200523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 if (parm & AC_CLIST_LONG) {
525 /* long form */
526 shift = 16;
527 num_elems = 2;
528 } else {
529 /* short form */
530 shift = 8;
531 num_elems = 4;
532 }
533 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 mask = (1 << (shift-1)) - 1;
535
Takashi Iwai0ba21762007-04-16 11:29:14 +0200536 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 return 0; /* no connection */
538
539 if (conn_len == 1) {
540 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200541 parm = snd_hda_codec_read(codec, nid, 0,
542 AC_VERB_GET_CONNECT_LIST, 0);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200543 if (parm == -1 && codec->bus->rirb_error)
544 return -EIO;
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100545 if (conn_list)
546 conn_list[0] = parm & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 return 1;
548 }
549
550 /* multi connection */
551 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100552 prev_nid = 0;
553 for (i = 0; i < conn_len; i++) {
554 int range_val;
555 hda_nid_t val, n;
556
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200557 if (i % num_elems == 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100558 parm = snd_hda_codec_read(codec, nid, 0,
559 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200560 if (parm == -1 && codec->bus->rirb_error)
561 return -EIO;
562 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200563 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100564 val = parm & mask;
Takashi Iwai5fdaecd2012-12-20 10:45:55 +0100565 if (val == 0 && null_count++) { /* no second chance */
Jaroslav Kysela2e9bf242009-07-18 11:48:19 +0200566 snd_printk(KERN_WARNING "hda_codec: "
567 "invalid CONNECT_LIST verb %x[%i]:%x\n",
568 nid, i, parm);
569 return 0;
570 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100571 parm >>= shift;
572 if (range_val) {
573 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200574 if (!prev_nid || prev_nid >= val) {
575 snd_printk(KERN_WARNING "hda_codec: "
576 "invalid dep_range_val %x:%x\n",
577 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100578 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100580 for (n = prev_nid + 1; n <= val; n++) {
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100581 if (conn_list) {
582 if (conns >= max_conns)
583 return -ENOSPC;
584 conn_list[conns] = n;
585 }
586 conns++;
Takashi Iwai54d17402005-11-21 16:33:22 +0100587 }
588 } else {
Takashi Iwaib5f82b12013-03-12 16:47:30 +0100589 if (conn_list) {
590 if (conns >= max_conns)
591 return -ENOSPC;
592 conn_list[conns] = val;
593 }
594 conns++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100596 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 }
598 return conns;
599}
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200602 * snd_hda_override_conn_list - add/modify the connection-list to cache
603 * @codec: the HDA codec
604 * @nid: NID to parse
605 * @len: number of connection list entries
606 * @list: the list of connection entries
607 *
608 * Add or modify the given connection-list to the cache. If the corresponding
609 * cache already exists, invalidate it and append a new one.
610 *
611 * Returns zero or a negative error code.
612 */
613int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
614 const hda_nid_t *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100616 struct hda_conn_list *p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200617
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100618 p = lookup_conn_list(codec, nid);
619 if (p) {
620 list_del(&p->list);
621 kfree(p);
622 }
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200623
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100624 return add_conn_list(codec, nid, len, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625}
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200626EXPORT_SYMBOL_HDA(snd_hda_override_conn_list);
627
628/**
Takashi Iwai8d087c72011-06-28 12:45:47 +0200629 * snd_hda_get_conn_index - get the connection index of the given NID
630 * @codec: the HDA codec
631 * @mux: NID containing the list
632 * @nid: NID to select
633 * @recursive: 1 when searching NID recursively, otherwise 0
634 *
635 * Parses the connection list of the widget @mux and checks whether the
636 * widget @nid is present. If it is, return the connection index.
637 * Otherwise it returns -1.
638 */
639int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
640 hda_nid_t nid, int recursive)
641{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100642 const hda_nid_t *conn;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200643 int i, nums;
644
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100645 nums = snd_hda_get_conn_list(codec, mux, &conn);
Takashi Iwai8d087c72011-06-28 12:45:47 +0200646 for (i = 0; i < nums; i++)
647 if (conn[i] == nid)
648 return i;
649 if (!recursive)
650 return -1;
Takashi Iwaid94ddd82012-12-20 14:42:42 +0100651 if (recursive > 10) {
Takashi Iwai8d087c72011-06-28 12:45:47 +0200652 snd_printd("hda_codec: too deep connection for 0x%x\n", nid);
653 return -1;
654 }
655 recursive++;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200656 for (i = 0; i < nums; i++) {
657 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
658 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
659 continue;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200660 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
661 return i;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200662 }
Takashi Iwai8d087c72011-06-28 12:45:47 +0200663 return -1;
664}
665EXPORT_SYMBOL_HDA(snd_hda_get_conn_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667/**
668 * snd_hda_queue_unsol_event - add an unsolicited event to queue
669 * @bus: the BUS
670 * @res: unsolicited event (lower 32bit of RIRB entry)
671 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
672 *
673 * Adds the given event to the queue. The events are processed in
674 * the workqueue asynchronously. Call this function in the interrupt
675 * hanlder when RIRB receives an unsolicited event.
676 *
677 * Returns 0 if successful, or a negative error code.
678 */
679int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
680{
681 struct hda_bus_unsolicited *unsol;
682 unsigned int wp;
683
Takashi Iwaiecf726f2011-08-09 14:22:44 +0200684 trace_hda_unsol_event(bus, res, res_ex);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200685 unsol = bus->unsol;
686 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 return 0;
688
689 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
690 unsol->wp = wp;
691
692 wp <<= 1;
693 unsol->queue[wp] = res;
694 unsol->queue[wp + 1] = res_ex;
695
Takashi Iwai6acaed32009-01-12 10:09:24 +0100696 queue_work(bus->workq, &unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 return 0;
699}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100700EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800703 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 */
David Howellsc4028952006-11-22 14:57:56 +0000705static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
David Howellsc4028952006-11-22 14:57:56 +0000707 struct hda_bus_unsolicited *unsol =
708 container_of(work, struct hda_bus_unsolicited, work);
709 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 struct hda_codec *codec;
711 unsigned int rp, caddr, res;
712
713 while (unsol->rp != unsol->wp) {
714 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
715 unsol->rp = rp;
716 rp <<= 1;
717 res = unsol->queue[rp];
718 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200719 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 continue;
721 codec = bus->caddr_tbl[caddr & 0x0f];
722 if (codec && codec->patch_ops.unsol_event)
723 codec->patch_ops.unsol_event(codec, res);
724 }
725}
726
727/*
728 * initialize unsolicited queue
729 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200730static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
732 struct hda_bus_unsolicited *unsol;
733
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100734 if (bus->unsol) /* already initialized */
735 return 0;
736
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200737 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200738 if (!unsol) {
739 snd_printk(KERN_ERR "hda_codec: "
740 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 return -ENOMEM;
742 }
David Howellsc4028952006-11-22 14:57:56 +0000743 INIT_WORK(&unsol->work, process_unsol_events);
744 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 bus->unsol = unsol;
746 return 0;
747}
748
749/*
750 * destructor
751 */
752static void snd_hda_codec_free(struct hda_codec *codec);
753
754static int snd_hda_bus_free(struct hda_bus *bus)
755{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200756 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Takashi Iwai0ba21762007-04-16 11:29:14 +0200758 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 return 0;
Takashi Iwai6acaed32009-01-12 10:09:24 +0100760 if (bus->workq)
761 flush_workqueue(bus->workq);
762 if (bus->unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 kfree(bus->unsol);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200764 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 snd_hda_codec_free(codec);
766 }
767 if (bus->ops.private_free)
768 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100769 if (bus->workq)
770 destroy_workqueue(bus->workq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 kfree(bus);
772 return 0;
773}
774
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100775static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
777 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100778 bus->shutdown = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 return snd_hda_bus_free(bus);
780}
781
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200782#ifdef CONFIG_SND_HDA_HWDEP
783static int snd_hda_bus_dev_register(struct snd_device *device)
784{
785 struct hda_bus *bus = device->device_data;
786 struct hda_codec *codec;
787 list_for_each_entry(codec, &bus->codec_list, list) {
788 snd_hda_hwdep_add_sysfs(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +0100789 snd_hda_hwdep_add_power_sysfs(codec);
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200790 }
791 return 0;
792}
793#else
794#define snd_hda_bus_dev_register NULL
795#endif
796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797/**
798 * snd_hda_bus_new - create a HDA bus
799 * @card: the card entry
800 * @temp: the template for hda_bus information
801 * @busp: the pointer to store the created bus instance
802 *
803 * Returns 0 if successful, or a negative error code.
804 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +0100805int snd_hda_bus_new(struct snd_card *card,
Takashi Iwai756e2b02007-04-16 11:27:07 +0200806 const struct hda_bus_template *temp,
807 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
809 struct hda_bus *bus;
810 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100811 static struct snd_device_ops dev_ops = {
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200812 .dev_register = snd_hda_bus_dev_register,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 .dev_free = snd_hda_bus_dev_free,
814 };
815
Takashi Iwaida3cec32008-08-08 17:12:14 +0200816 if (snd_BUG_ON(!temp))
817 return -EINVAL;
818 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
819 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 if (busp)
822 *busp = NULL;
823
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200824 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 if (bus == NULL) {
826 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
827 return -ENOMEM;
828 }
829
830 bus->card = card;
831 bus->private_data = temp->private_data;
832 bus->pci = temp->pci;
833 bus->modelname = temp->modelname;
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100834 bus->power_save = temp->power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 bus->ops = temp->ops;
836
Ingo Molnar62932df2006-01-16 16:34:20 +0100837 mutex_init(&bus->cmd_mutex);
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200838 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 INIT_LIST_HEAD(&bus->codec_list);
840
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100841 snprintf(bus->workq_name, sizeof(bus->workq_name),
842 "hd-audio%d", card->number);
843 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100844 if (!bus->workq) {
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100845 snd_printk(KERN_ERR "cannot create workqueue %s\n",
846 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100847 kfree(bus);
848 return -ENOMEM;
849 }
850
Takashi Iwai0ba21762007-04-16 11:29:14 +0200851 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
852 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 snd_hda_bus_free(bus);
854 return err;
855 }
856 if (busp)
857 *busp = bus;
858 return 0;
859}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100860EXPORT_SYMBOL_HDA(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Takashi Iwai82467612007-07-27 19:15:54 +0200862#ifdef CONFIG_SND_HDA_GENERIC
863#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200864 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200865#else
866#define is_generic_config(codec) 0
867#endif
868
Takashi Iwai645f10c2008-11-28 15:07:37 +0100869#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100870#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
871#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100872#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100873#endif
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875/*
876 * find a matching codec preset
877 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200878static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200879find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100881 struct hda_codec_preset_list *tbl;
882 const struct hda_codec_preset *preset;
Takashi Iwai5d908ab2012-08-24 18:40:10 +0200883 unsigned int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Takashi Iwai82467612007-07-27 19:15:54 +0200885 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100886 return NULL; /* use the generic parser */
887
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100888 again:
889 mutex_lock(&preset_mutex);
890 list_for_each_entry(tbl, &hda_preset_tables, list) {
891 if (!try_module_get(tbl->owner)) {
892 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
893 continue;
894 }
895 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100897 if (preset->afg && preset->afg != codec->afg)
898 continue;
899 if (preset->mfg && preset->mfg != codec->mfg)
900 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200901 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200903 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200904 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100905 preset->rev == codec->revision_id)) {
906 mutex_unlock(&preset_mutex);
907 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100911 module_put(tbl->owner);
912 }
913 mutex_unlock(&preset_mutex);
914
915 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
916 char name[32];
917 if (!mod_requested)
918 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
919 codec->vendor_id);
920 else
921 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
922 (codec->vendor_id >> 16) & 0xffff);
923 request_module(name);
924 mod_requested++;
925 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 }
927 return NULL;
928}
929
930/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200931 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200933static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
935 const struct hda_vendor_id *c;
936 const char *vendor = NULL;
937 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200938 char tmp[16];
939
940 if (codec->vendor_name)
941 goto get_chip_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
943 for (c = hda_vendor_ids; c->id; c++) {
944 if (c->id == vendor_id) {
945 vendor = c->name;
946 break;
947 }
948 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200949 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 sprintf(tmp, "Generic %04x", vendor_id);
951 vendor = tmp;
952 }
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200953 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
954 if (!codec->vendor_name)
955 return -ENOMEM;
956
957 get_chip_name:
958 if (codec->chip_name)
959 return 0;
960
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 if (codec->preset && codec->preset->name)
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200962 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
963 else {
964 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
965 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
966 }
967 if (!codec->chip_name)
Takashi Iwaif44ac832008-07-30 15:01:45 +0200968 return -ENOMEM;
969 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970}
971
972/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200973 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +0100975static void setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200977 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 hda_nid_t nid;
979
980 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
981 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200982 function_id = snd_hda_param_read(codec, nid,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200983 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +0200984 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200985 case AC_GRP_AUDIO_FUNCTION:
986 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200987 codec->afg_function_id = function_id & 0xff;
988 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200989 break;
990 case AC_GRP_MODEM_FUNCTION:
991 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200992 codec->mfg_function_id = function_id & 0xff;
993 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200994 break;
995 default:
996 break;
997 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999}
1000
1001/*
Takashi Iwai54d17402005-11-21 16:33:22 +01001002 * read widget caps for each widget and store in cache
1003 */
1004static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
1005{
1006 int i;
1007 hda_nid_t nid;
1008
1009 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
1010 &codec->start_nid);
1011 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001012 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +01001013 return -ENOMEM;
1014 nid = codec->start_nid;
1015 for (i = 0; i < codec->num_nodes; i++, nid++)
1016 codec->wcaps[i] = snd_hda_param_read(codec, nid,
1017 AC_PAR_AUDIO_WIDGET_CAP);
1018 return 0;
1019}
1020
Takashi Iwai3be14142009-02-20 14:11:16 +01001021/* read all pin default configurations and save codec->init_pins */
1022static int read_pin_defaults(struct hda_codec *codec)
1023{
1024 int i;
1025 hda_nid_t nid = codec->start_nid;
1026
1027 for (i = 0; i < codec->num_nodes; i++, nid++) {
1028 struct hda_pincfg *pin;
1029 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +02001030 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +01001031 if (wid_type != AC_WID_PIN)
1032 continue;
1033 pin = snd_array_new(&codec->init_pins);
1034 if (!pin)
1035 return -ENOMEM;
1036 pin->nid = nid;
1037 pin->cfg = snd_hda_codec_read(codec, nid, 0,
1038 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001039 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
1040 AC_VERB_GET_PIN_WIDGET_CONTROL,
1041 0);
Takashi Iwai3be14142009-02-20 14:11:16 +01001042 }
1043 return 0;
1044}
1045
1046/* look up the given pin config list and return the item matching with NID */
1047static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
1048 struct snd_array *array,
1049 hda_nid_t nid)
1050{
1051 int i;
1052 for (i = 0; i < array->used; i++) {
1053 struct hda_pincfg *pin = snd_array_elem(array, i);
1054 if (pin->nid == nid)
1055 return pin;
1056 }
1057 return NULL;
1058}
1059
Takashi Iwai3be14142009-02-20 14:11:16 +01001060/* set the current pin config value for the given NID.
1061 * the value is cached, and read via snd_hda_codec_get_pincfg()
1062 */
1063int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
1064 hda_nid_t nid, unsigned int cfg)
1065{
1066 struct hda_pincfg *pin;
1067
Takashi Iwaib82855a2009-12-27 11:24:56 +01001068 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1069 return -EINVAL;
1070
Takashi Iwai3be14142009-02-20 14:11:16 +01001071 pin = look_up_pincfg(codec, list, nid);
1072 if (!pin) {
1073 pin = snd_array_new(list);
1074 if (!pin)
1075 return -ENOMEM;
1076 pin->nid = nid;
1077 }
1078 pin->cfg = cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001079 return 0;
1080}
1081
Takashi Iwaid5191e52009-11-16 14:58:17 +01001082/**
1083 * snd_hda_codec_set_pincfg - Override a pin default configuration
1084 * @codec: the HDA codec
1085 * @nid: NID to set the pin config
1086 * @cfg: the pin default config value
1087 *
1088 * Override a pin default configuration value in the cache.
1089 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1090 * priority than the real hardware value.
1091 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001092int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1093 hda_nid_t nid, unsigned int cfg)
1094{
Takashi Iwai346ff702009-02-23 09:42:57 +01001095 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001096}
1097EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
1098
Takashi Iwaid5191e52009-11-16 14:58:17 +01001099/**
1100 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1101 * @codec: the HDA codec
1102 * @nid: NID to get the pin config
1103 *
1104 * Get the current pin config value of the given pin NID.
1105 * If the pincfg value is cached or overridden via sysfs or driver,
1106 * returns the cached value.
1107 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001108unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1109{
1110 struct hda_pincfg *pin;
1111
Takashi Iwai3be14142009-02-20 14:11:16 +01001112#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai09b70e82013-01-10 18:21:56 +01001113 {
1114 unsigned int cfg = 0;
1115 mutex_lock(&codec->user_mutex);
1116 pin = look_up_pincfg(codec, &codec->user_pins, nid);
1117 if (pin)
1118 cfg = pin->cfg;
1119 mutex_unlock(&codec->user_mutex);
1120 if (cfg)
1121 return cfg;
1122 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001123#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001124 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1125 if (pin)
1126 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001127 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1128 if (pin)
1129 return pin->cfg;
1130 return 0;
1131}
1132EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
1133
Takashi Iwaid7fdc002013-01-10 08:38:04 +01001134/* remember the current pinctl target value */
1135int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
1136 unsigned int val)
1137{
1138 struct hda_pincfg *pin;
1139
1140 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1141 if (!pin)
1142 return -EINVAL;
1143 pin->target = val;
1144 return 0;
1145}
1146EXPORT_SYMBOL_HDA(snd_hda_codec_set_pin_target);
1147
1148/* return the current pinctl target value */
1149int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
1150{
1151 struct hda_pincfg *pin;
1152
1153 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1154 if (!pin)
1155 return 0;
1156 return pin->target;
1157}
1158EXPORT_SYMBOL_HDA(snd_hda_codec_get_pin_target);
1159
Takashi Iwai92ee6162009-12-27 11:18:59 +01001160/**
1161 * snd_hda_shutup_pins - Shut up all pins
1162 * @codec: the HDA codec
1163 *
1164 * Clear all pin controls to shup up before suspend for avoiding click noise.
1165 * The controls aren't cached so that they can be resumed properly.
1166 */
1167void snd_hda_shutup_pins(struct hda_codec *codec)
1168{
1169 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001170 /* don't shut up pins when unloading the driver; otherwise it breaks
1171 * the default pin setup at the next load of the driver
1172 */
1173 if (codec->bus->shutdown)
1174 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001175 for (i = 0; i < codec->init_pins.used; i++) {
1176 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1177 /* use read here for syncing after issuing each verb */
1178 snd_hda_codec_read(codec, pin->nid, 0,
1179 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1180 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001181 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001182}
1183EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
1184
Takashi Iwai2a439522011-07-26 09:52:50 +02001185#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001186/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1187static void restore_shutup_pins(struct hda_codec *codec)
1188{
1189 int i;
1190 if (!codec->pins_shutup)
1191 return;
1192 if (codec->bus->shutdown)
1193 return;
1194 for (i = 0; i < codec->init_pins.used; i++) {
1195 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1196 snd_hda_codec_write(codec, pin->nid, 0,
1197 AC_VERB_SET_PIN_WIDGET_CONTROL,
1198 pin->ctrl);
1199 }
1200 codec->pins_shutup = 0;
1201}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001202#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001203
David Henningsson26a6cb62012-10-09 15:04:21 +02001204static void hda_jackpoll_work(struct work_struct *work)
1205{
1206 struct hda_codec *codec =
1207 container_of(work, struct hda_codec, jackpoll_work.work);
1208 if (!codec->jackpoll_interval)
1209 return;
1210
1211 snd_hda_jack_set_dirty_all(codec);
1212 snd_hda_jack_poll_all(codec);
1213 queue_delayed_work(codec->bus->workq, &codec->jackpoll_work,
1214 codec->jackpoll_interval);
1215}
1216
Takashi Iwai01751f52007-08-10 16:59:39 +02001217static void init_hda_cache(struct hda_cache_rec *cache,
1218 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001219static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001220
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001221/* release all pincfg lists */
1222static void free_init_pincfgs(struct hda_codec *codec)
Takashi Iwai3be14142009-02-20 14:11:16 +01001223{
Takashi Iwai346ff702009-02-23 09:42:57 +01001224 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001225#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001226 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001227#endif
Takashi Iwai3be14142009-02-20 14:11:16 +01001228 snd_array_free(&codec->init_pins);
1229}
1230
Takashi Iwai54d17402005-11-21 16:33:22 +01001231/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001232 * audio-converter setup caches
1233 */
1234struct hda_cvt_setup {
1235 hda_nid_t nid;
1236 u8 stream_tag;
1237 u8 channel_id;
1238 u16 format_id;
1239 unsigned char active; /* cvt is currently used */
1240 unsigned char dirty; /* setups should be cleared */
1241};
1242
1243/* get or create a cache entry for the given audio converter NID */
1244static struct hda_cvt_setup *
1245get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1246{
1247 struct hda_cvt_setup *p;
1248 int i;
1249
1250 for (i = 0; i < codec->cvt_setups.used; i++) {
1251 p = snd_array_elem(&codec->cvt_setups, i);
1252 if (p->nid == nid)
1253 return p;
1254 }
1255 p = snd_array_new(&codec->cvt_setups);
1256 if (p)
1257 p->nid = nid;
1258 return p;
1259}
1260
1261/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 * codec destructor
1263 */
1264static void snd_hda_codec_free(struct hda_codec *codec)
1265{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001266 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 return;
David Henningsson26a6cb62012-10-09 15:04:21 +02001268 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai59cad162012-06-26 15:00:20 +02001269 snd_hda_jack_tbl_clear(codec);
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001270 free_init_pincfgs(codec);
Takashi Iwai83012a72012-08-24 18:38:08 +02001271#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02001272 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001273 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001274#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001276 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001277 snd_array_free(&codec->nids);
Takashi Iwai59cad162012-06-26 15:00:20 +02001278 snd_array_free(&codec->cvt_setups);
Stephen Warren7c9359762011-06-01 11:14:17 -06001279 snd_array_free(&codec->spdif_out);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001280 remove_conn_list(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 codec->bus->caddr_tbl[codec->addr] = NULL;
1282 if (codec->patch_ops.free)
1283 codec->patch_ops.free(codec);
Takashi Iwai83012a72012-08-24 18:38:08 +02001284#ifdef CONFIG_PM
Takashi Iwai08fa20a2012-08-31 07:46:56 -07001285 if (!codec->pm_down_notified) /* cancel leftover refcounts */
Takashi Iwai68467f52012-08-28 09:14:29 -07001286 hda_call_pm_notify(codec->bus, false);
1287#endif
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001288 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +02001289 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001290 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001291 kfree(codec->vendor_name);
1292 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001293 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001294 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 kfree(codec);
1296}
1297
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001298static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec,
1299 hda_nid_t fg, unsigned int power_state);
1300
Takashi Iwaid8193872012-08-31 07:54:38 -07001301static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001302 unsigned int power_state);
Takashi Iwai9419ab62013-01-24 17:23:35 +01001303static unsigned int default_power_filter(struct hda_codec *codec, hda_nid_t nid,
1304 unsigned int power_state);
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001305
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306/**
1307 * snd_hda_codec_new - create a HDA codec
1308 * @bus: the bus to assign
1309 * @codec_addr: the codec address
1310 * @codecp: the pointer to store the generated codec
1311 *
1312 * Returns 0 if successful, or a negative error code.
1313 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001314int snd_hda_codec_new(struct hda_bus *bus,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001315 unsigned int codec_addr,
1316 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317{
1318 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001319 char component[31];
Takashi Iwaid8193872012-08-31 07:54:38 -07001320 hda_nid_t fg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 int err;
1322
Takashi Iwaida3cec32008-08-08 17:12:14 +02001323 if (snd_BUG_ON(!bus))
1324 return -EINVAL;
1325 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1326 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
1328 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001329 snd_printk(KERN_ERR "hda_codec: "
1330 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 return -EBUSY;
1332 }
1333
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001334 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 if (codec == NULL) {
1336 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1337 return -ENOMEM;
1338 }
1339
1340 codec->bus = bus;
1341 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001342 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001343 mutex_init(&codec->control_mutex);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001344 mutex_init(&codec->hash_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001345 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001346 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001347 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1348 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001349 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001350 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001351 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Stephen Warren7c9359762011-06-01 11:14:17 -06001352 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai361dab32012-05-09 14:35:27 +02001353 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01001354 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001355 INIT_LIST_HEAD(&codec->conn_list);
1356
David Henningsson26a6cb62012-10-09 15:04:21 +02001357 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Takashi Iwai83012a72012-08-24 18:38:08 +02001359#ifdef CONFIG_PM
Takashi Iwai5536c6d2012-05-08 17:08:10 +02001360 spin_lock_init(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001361 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1362 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1363 * the caller has to power down appropriatley after initialization
1364 * phase.
1365 */
1366 hda_keep_power_on(codec);
Takashi Iwai68467f52012-08-28 09:14:29 -07001367 hda_call_pm_notify(bus, true);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001368#endif
1369
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001370 if (codec->bus->modelname) {
1371 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1372 if (!codec->modelname) {
1373 snd_hda_codec_free(codec);
1374 return -ENODEV;
1375 }
1376 }
1377
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 list_add_tail(&codec->list, &bus->codec_list);
1379 bus->caddr_tbl[codec_addr] = codec;
1380
Takashi Iwai0ba21762007-04-16 11:29:14 +02001381 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1382 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001383 if (codec->vendor_id == -1)
1384 /* read again, hopefully the access method was corrected
1385 * in the last read...
1386 */
1387 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1388 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001389 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1390 AC_PAR_SUBSYSTEM_ID);
1391 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1392 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001394 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001395 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001396 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001397 err = -ENODEV;
1398 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 }
1400
Takashi Iwaid8193872012-08-31 07:54:38 -07001401 fg = codec->afg ? codec->afg : codec->mfg;
1402 err = read_widget_caps(codec, fg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001403 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001404 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001405 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001406 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001407 err = read_pin_defaults(codec);
1408 if (err < 0)
1409 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001410
Takashi Iwai0ba21762007-04-16 11:29:14 +02001411 if (!codec->subsystem_id) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001412 codec->subsystem_id =
Takashi Iwaid8193872012-08-31 07:54:38 -07001413 snd_hda_codec_read(codec, fg, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001414 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001415 }
1416
Takashi Iwai83012a72012-08-24 18:38:08 +02001417#ifdef CONFIG_PM
Takashi Iwaid8193872012-08-31 07:54:38 -07001418 codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg,
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001419 AC_PWRST_CLKSTOP);
1420 if (!codec->d3_stop_clk)
1421 bus->power_keep_link_on = 1;
Mengdong Lin5d6147f2012-08-24 12:06:30 +08001422#endif
Takashi Iwaid8193872012-08-31 07:54:38 -07001423 codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
Takashi Iwai983f6b92012-08-28 09:18:01 -07001424 AC_PWRST_EPSS);
Takashi Iwai9419ab62013-01-24 17:23:35 +01001425 codec->power_filter = default_power_filter;
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001426
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001427 /* power-up all before initialization */
Takashi Iwaid8193872012-08-31 07:54:38 -07001428 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001429
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001430 snd_hda_codec_proc_new(codec);
1431
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001432 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001433
1434 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1435 codec->subsystem_id, codec->revision_id);
1436 snd_component_add(codec->bus->card, component);
1437
1438 if (codecp)
1439 *codecp = codec;
1440 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001441
1442 error:
1443 snd_hda_codec_free(codec);
1444 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001445}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001446EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001447
Mengdong Lina15d05d2013-02-08 17:09:31 -05001448int snd_hda_codec_update_widgets(struct hda_codec *codec)
1449{
1450 hda_nid_t fg;
1451 int err;
1452
1453 /* Assume the function group node does not change,
1454 * only the widget nodes may change.
1455 */
1456 kfree(codec->wcaps);
1457 fg = codec->afg ? codec->afg : codec->mfg;
1458 err = read_widget_caps(codec, fg);
1459 if (err < 0) {
1460 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
1461 return err;
1462 }
1463
1464 snd_array_free(&codec->init_pins);
1465 err = read_pin_defaults(codec);
1466
1467 return err;
1468}
1469EXPORT_SYMBOL_HDA(snd_hda_codec_update_widgets);
1470
1471
Takashi Iwaid5191e52009-11-16 14:58:17 +01001472/**
1473 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1474 * @codec: the HDA codec
1475 *
1476 * Start parsing of the given codec tree and (re-)initialize the whole
1477 * patch instance.
1478 *
1479 * Returns 0 if successful or a negative error code.
1480 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001481int snd_hda_codec_configure(struct hda_codec *codec)
1482{
1483 int err;
1484
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001485 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001486 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001487 err = get_codec_name(codec);
1488 if (err < 0)
1489 return err;
1490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
Takashi Iwai82467612007-07-27 19:15:54 +02001492 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001494 goto patched;
1495 }
Takashi Iwai82467612007-07-27 19:15:54 +02001496 if (codec->preset && codec->preset->patch) {
1497 err = codec->preset->patch(codec);
1498 goto patched;
1499 }
1500
1501 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001502 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001503 if (err < 0)
1504 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001505
1506 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001507 if (!err && codec->patch_ops.unsol_event)
1508 err = init_unsol_queue(codec->bus);
Takashi Iwaif62faed2009-12-23 09:27:51 +01001509 /* audio codec should override the mixer name */
1510 if (!err && (codec->afg || !*codec->bus->card->mixername))
1511 snprintf(codec->bus->card->mixername,
1512 sizeof(codec->bus->card->mixername),
1513 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001514 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001516EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
Takashi Iwaied360812012-08-08 17:12:52 +02001518/* update the stream-id if changed */
1519static void update_pcm_stream_id(struct hda_codec *codec,
1520 struct hda_cvt_setup *p, hda_nid_t nid,
1521 u32 stream_tag, int channel_id)
1522{
1523 unsigned int oldval, newval;
1524
1525 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1526 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1527 newval = (stream_tag << 4) | channel_id;
1528 if (oldval != newval)
1529 snd_hda_codec_write(codec, nid, 0,
1530 AC_VERB_SET_CHANNEL_STREAMID,
1531 newval);
1532 p->stream_tag = stream_tag;
1533 p->channel_id = channel_id;
1534 }
1535}
1536
1537/* update the format-id if changed */
1538static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1539 hda_nid_t nid, int format)
1540{
1541 unsigned int oldval;
1542
1543 if (p->format_id != format) {
1544 oldval = snd_hda_codec_read(codec, nid, 0,
1545 AC_VERB_GET_STREAM_FORMAT, 0);
1546 if (oldval != format) {
1547 msleep(1);
1548 snd_hda_codec_write(codec, nid, 0,
1549 AC_VERB_SET_STREAM_FORMAT,
1550 format);
1551 }
1552 p->format_id = format;
1553 }
1554}
1555
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556/**
1557 * snd_hda_codec_setup_stream - set up the codec for streaming
1558 * @codec: the CODEC to set up
1559 * @nid: the NID to set up
1560 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1561 * @channel_id: channel id to pass, zero based.
1562 * @format: stream format.
1563 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001564void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1565 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 int channel_id, int format)
1567{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001568 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001569 struct hda_cvt_setup *p;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001570 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001571 int i;
1572
Takashi Iwai0ba21762007-04-16 11:29:14 +02001573 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001574 return;
1575
Takashi Iwai0ba21762007-04-16 11:29:14 +02001576 snd_printdd("hda_codec_setup_stream: "
1577 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001579 p = get_hda_cvt_setup(codec, nid);
Takashi Iwaiaffdb622013-01-07 15:36:45 +01001580 if (!p || p->active)
Takashi Iwaieb541332010-08-06 13:48:11 +02001581 return;
Takashi Iwaied360812012-08-08 17:12:52 +02001582
1583 if (codec->pcm_format_first)
1584 update_pcm_format(codec, p, nid, format);
1585 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1586 if (!codec->pcm_format_first)
1587 update_pcm_format(codec, p, nid, format);
1588
Takashi Iwaieb541332010-08-06 13:48:11 +02001589 p->active = 1;
1590 p->dirty = 0;
1591
1592 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001593 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001594 list_for_each_entry(c, &codec->bus->codec_list, list) {
1595 for (i = 0; i < c->cvt_setups.used; i++) {
1596 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001597 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001598 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001599 p->dirty = 1;
1600 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001603EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
Takashi Iwaif0cea792010-08-13 11:56:53 +02001605static void really_cleanup_stream(struct hda_codec *codec,
1606 struct hda_cvt_setup *q);
1607
Takashi Iwaid5191e52009-11-16 14:58:17 +01001608/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001609 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001610 * @codec: the CODEC to clean up
1611 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001612 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001613 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001614void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1615 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001616{
Takashi Iwaieb541332010-08-06 13:48:11 +02001617 struct hda_cvt_setup *p;
1618
Takashi Iwai888afa12008-03-18 09:57:50 +01001619 if (!nid)
1620 return;
1621
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001622 if (codec->no_sticky_stream)
1623 do_now = 1;
1624
Takashi Iwai888afa12008-03-18 09:57:50 +01001625 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001626 p = get_hda_cvt_setup(codec, nid);
Takashi Iwaiaffdb622013-01-07 15:36:45 +01001627 if (p && p->active) {
Takashi Iwaif0cea792010-08-13 11:56:53 +02001628 /* here we just clear the active flag when do_now isn't set;
1629 * actual clean-ups will be done later in
1630 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1631 */
1632 if (do_now)
1633 really_cleanup_stream(codec, p);
1634 else
1635 p->active = 0;
1636 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001637}
Takashi Iwaif0cea792010-08-13 11:56:53 +02001638EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001639
Takashi Iwaieb541332010-08-06 13:48:11 +02001640static void really_cleanup_stream(struct hda_codec *codec,
1641 struct hda_cvt_setup *q)
1642{
1643 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001644 if (q->stream_tag || q->channel_id)
1645 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1646 if (q->format_id)
1647 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1648);
Takashi Iwaieb541332010-08-06 13:48:11 +02001649 memset(q, 0, sizeof(*q));
1650 q->nid = nid;
1651}
1652
1653/* clean up the all conflicting obsolete streams */
1654static void purify_inactive_streams(struct hda_codec *codec)
1655{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001656 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001657 int i;
1658
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001659 list_for_each_entry(c, &codec->bus->codec_list, list) {
1660 for (i = 0; i < c->cvt_setups.used; i++) {
1661 struct hda_cvt_setup *p;
1662 p = snd_array_elem(&c->cvt_setups, i);
1663 if (p->dirty)
1664 really_cleanup_stream(c, p);
1665 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001666 }
1667}
1668
Takashi Iwai2a439522011-07-26 09:52:50 +02001669#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001670/* clean up all streams; called from suspend */
1671static void hda_cleanup_all_streams(struct hda_codec *codec)
1672{
1673 int i;
1674
1675 for (i = 0; i < codec->cvt_setups.used; i++) {
1676 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1677 if (p->stream_tag)
1678 really_cleanup_stream(codec, p);
1679 }
1680}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001681#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001682
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683/*
1684 * amp access functions
1685 */
1686
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001687/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001688#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001689#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001690#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1691#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001693#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
1695/* initialize the hash table */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001696static void init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001697 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698{
Takashi Iwai01751f52007-08-10 16:59:39 +02001699 memset(cache, 0, sizeof(*cache));
1700 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001701 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001702}
1703
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001704static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001705{
Takashi Iwai603c4012008-07-30 15:01:44 +02001706 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707}
1708
1709/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001710static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711{
Takashi Iwai01751f52007-08-10 16:59:39 +02001712 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1713 u16 cur = cache->hash[idx];
1714 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
1716 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001717 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 if (info->key == key)
1719 return info;
1720 cur = info->next;
1721 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001722 return NULL;
1723}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001725/* query the hash. allocate an entry if not found. */
1726static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1727 u32 key)
1728{
1729 struct hda_cache_head *info = get_hash(cache, key);
1730 if (!info) {
1731 u16 idx, cur;
1732 /* add a new hash entry */
1733 info = snd_array_new(&cache->buf);
1734 if (!info)
1735 return NULL;
1736 cur = snd_array_index(&cache->buf, info);
1737 info->key = key;
1738 info->val = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001739 info->dirty = 0;
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001740 idx = key % (u16)ARRAY_SIZE(cache->hash);
1741 info->next = cache->hash[idx];
1742 cache->hash[idx] = cur;
1743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 return info;
1745}
1746
Takashi Iwai01751f52007-08-10 16:59:39 +02001747/* query and allocate an amp hash entry */
1748static inline struct hda_amp_info *
1749get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1750{
1751 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1752}
1753
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001754/* overwrite the value with the key in the caps hash */
1755static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1756{
1757 struct hda_amp_info *info;
1758
1759 mutex_lock(&codec->hash_mutex);
1760 info = get_alloc_amp_hash(codec, key);
1761 if (!info) {
1762 mutex_unlock(&codec->hash_mutex);
1763 return -EINVAL;
1764 }
1765 info->amp_caps = val;
1766 info->head.val |= INFO_AMP_CAPS;
1767 mutex_unlock(&codec->hash_mutex);
1768 return 0;
1769}
1770
1771/* query the value from the caps hash; if not found, fetch the current
1772 * value from the given function and store in the hash
1773 */
1774static unsigned int
1775query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1776 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1777{
1778 struct hda_amp_info *info;
1779 unsigned int val;
1780
1781 mutex_lock(&codec->hash_mutex);
1782 info = get_alloc_amp_hash(codec, key);
1783 if (!info) {
1784 mutex_unlock(&codec->hash_mutex);
1785 return 0;
1786 }
1787 if (!(info->head.val & INFO_AMP_CAPS)) {
1788 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1789 val = func(codec, nid, dir);
1790 write_caps_hash(codec, key, val);
1791 } else {
1792 val = info->amp_caps;
1793 mutex_unlock(&codec->hash_mutex);
1794 }
1795 return val;
1796}
1797
1798static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1799 int direction)
1800{
1801 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1802 nid = codec->afg;
1803 return snd_hda_param_read(codec, nid,
1804 direction == HDA_OUTPUT ?
1805 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1806}
1807
Takashi Iwaid5191e52009-11-16 14:58:17 +01001808/**
1809 * query_amp_caps - query AMP capabilities
1810 * @codec: the HD-auio codec
1811 * @nid: the NID to query
1812 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1813 *
1814 * Query AMP capabilities for the given widget and direction.
1815 * Returns the obtained capability bits.
1816 *
1817 * When cap bits have been already read, this doesn't read again but
1818 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001820u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001822 return query_caps_hash(codec, nid, direction,
1823 HDA_HASH_KEY(nid, direction, 0),
1824 read_amp_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001826EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
Takashi Iwaid5191e52009-11-16 14:58:17 +01001828/**
1829 * snd_hda_override_amp_caps - Override the AMP capabilities
1830 * @codec: the CODEC to clean up
1831 * @nid: the NID to clean up
1832 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1833 * @caps: the capability bits to set
1834 *
1835 * Override the cached AMP caps bits value by the given one.
1836 * This function is useful if the driver needs to adjust the AMP ranges,
1837 * e.g. limit to 0dB, etc.
1838 *
1839 * Returns zero if successful or a negative error code.
1840 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001841int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1842 unsigned int caps)
1843{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001844 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001845}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001846EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001847
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001848static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
1849 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001850{
1851 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1852}
1853
Takashi Iwaid5191e52009-11-16 14:58:17 +01001854/**
1855 * snd_hda_query_pin_caps - Query PIN capabilities
1856 * @codec: the HD-auio codec
1857 * @nid: the NID to query
1858 *
1859 * Query PIN capabilities for the given widget.
1860 * Returns the obtained capability bits.
1861 *
1862 * When cap bits have been already read, this doesn't read again but
1863 * returns the cached value.
1864 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001865u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1866{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001867 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001868 read_pin_cap);
1869}
Takashi Iwai1327a322009-03-23 13:07:47 +01001870EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1871
Wu Fengguang864f92b2009-11-18 12:38:02 +08001872/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02001873 * snd_hda_override_pin_caps - Override the pin capabilities
1874 * @codec: the CODEC
1875 * @nid: the NID to override
1876 * @caps: the capability bits to set
1877 *
1878 * Override the cached PIN capabilitiy bits value by the given one.
1879 *
1880 * Returns zero if successful or a negative error code.
1881 */
1882int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1883 unsigned int caps)
1884{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001885 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02001886}
1887EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps);
1888
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001889/* read or sync the hash value with the current value;
1890 * call within hash_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 */
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001892static struct hda_amp_info *
1893update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
Takashi Iwai280e57d2012-12-14 10:32:21 +01001894 int direction, int index, bool init_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001896 struct hda_amp_info *info;
1897 unsigned int parm, val = 0;
1898 bool val_read = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001900 retry:
1901 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1902 if (!info)
1903 return NULL;
1904 if (!(info->head.val & INFO_AMP_VOL(ch))) {
1905 if (!val_read) {
1906 mutex_unlock(&codec->hash_mutex);
1907 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1908 parm |= direction == HDA_OUTPUT ?
1909 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1910 parm |= index;
1911 val = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001912 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001913 val &= 0xff;
1914 val_read = true;
1915 mutex_lock(&codec->hash_mutex);
1916 goto retry;
1917 }
1918 info->vol[ch] = val;
1919 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001920 } else if (init_only)
1921 return NULL;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001922 return info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923}
1924
1925/*
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001926 * write the current volume in info to the h/w
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 */
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001928static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001929 hda_nid_t nid, int ch, int direction, int index,
1930 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931{
1932 u32 parm;
1933
1934 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1935 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1936 parm |= index << AC_AMP_SET_INDEX_SHIFT;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001937 if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) &&
1938 (amp_caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwai38681372012-02-27 15:00:58 +01001939 ; /* set the zero value as a fake mute */
1940 else
1941 parm |= val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1943}
1944
Takashi Iwaid5191e52009-11-16 14:58:17 +01001945/**
1946 * snd_hda_codec_amp_read - Read AMP value
1947 * @codec: HD-audio codec
1948 * @nid: NID to read the AMP value
1949 * @ch: channel (left=0 or right=1)
1950 * @direction: #HDA_INPUT or #HDA_OUTPUT
1951 * @index: the index value (only for input direction)
1952 *
1953 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 */
Takashi Iwai834be882006-03-01 14:16:17 +01001955int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1956 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001958 struct hda_amp_info *info;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001959 unsigned int val = 0;
1960
1961 mutex_lock(&codec->hash_mutex);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001962 info = update_amp_hash(codec, nid, ch, direction, index, false);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001963 if (info)
1964 val = info->vol[ch];
1965 mutex_unlock(&codec->hash_mutex);
1966 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001968EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
Takashi Iwai280e57d2012-12-14 10:32:21 +01001970static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1971 int direction, int idx, int mask, int val,
1972 bool init_only)
1973{
1974 struct hda_amp_info *info;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001975 unsigned int caps;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01001976 unsigned int cache_only;
Takashi Iwai280e57d2012-12-14 10:32:21 +01001977
1978 if (snd_BUG_ON(mask & ~0xff))
1979 mask &= 0xff;
1980 val &= mask;
1981
1982 mutex_lock(&codec->hash_mutex);
1983 info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
1984 if (!info) {
1985 mutex_unlock(&codec->hash_mutex);
1986 return 0;
1987 }
1988 val |= info->vol[ch] & ~mask;
1989 if (info->vol[ch] == val) {
1990 mutex_unlock(&codec->hash_mutex);
1991 return 0;
1992 }
1993 info->vol[ch] = val;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01001994 cache_only = info->head.dirty = codec->cached_write;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001995 caps = info->amp_caps;
Takashi Iwai280e57d2012-12-14 10:32:21 +01001996 mutex_unlock(&codec->hash_mutex);
Takashi Iwaide1e37b2012-12-20 11:00:21 +01001997 if (!cache_only)
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001998 put_vol_mute(codec, caps, nid, ch, direction, idx, val);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001999 return 1;
2000}
2001
Takashi Iwaid5191e52009-11-16 14:58:17 +01002002/**
2003 * snd_hda_codec_amp_update - update the AMP value
2004 * @codec: HD-audio codec
2005 * @nid: NID to read the AMP value
2006 * @ch: channel (left=0 or right=1)
2007 * @direction: #HDA_INPUT or #HDA_OUTPUT
2008 * @idx: the index value (only for input direction)
2009 * @mask: bit mask to set
2010 * @val: the bits value to set
2011 *
2012 * Update the AMP value with a bit mask.
2013 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002014 */
Takashi Iwai834be882006-03-01 14:16:17 +01002015int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
2016 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017{
Takashi Iwai280e57d2012-12-14 10:32:21 +01002018 return codec_amp_update(codec, nid, ch, direction, idx, mask, val, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002020EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
Takashi Iwaid5191e52009-11-16 14:58:17 +01002022/**
2023 * snd_hda_codec_amp_stereo - update the AMP stereo values
2024 * @codec: HD-audio codec
2025 * @nid: NID to read the AMP value
2026 * @direction: #HDA_INPUT or #HDA_OUTPUT
2027 * @idx: the index value (only for input direction)
2028 * @mask: bit mask to set
2029 * @val: the bits value to set
2030 *
2031 * Update the AMP values like snd_hda_codec_amp_update(), but for a
2032 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02002033 */
2034int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
2035 int direction, int idx, int mask, int val)
2036{
2037 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02002038
2039 if (snd_BUG_ON(mask & ~0xff))
2040 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02002041 for (ch = 0; ch < 2; ch++)
2042 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
2043 idx, mask, val);
2044 return ret;
2045}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002046EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02002047
Takashi Iwai280e57d2012-12-14 10:32:21 +01002048/* Works like snd_hda_codec_amp_update() but it writes the value only at
2049 * the first access. If the amp was already initialized / updated beforehand,
2050 * this does nothing.
2051 */
2052int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
2053 int dir, int idx, int mask, int val)
2054{
2055 return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true);
2056}
2057EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init);
2058
2059int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
2060 int dir, int idx, int mask, int val)
2061{
2062 int ch, ret = 0;
2063
2064 if (snd_BUG_ON(mask & ~0xff))
2065 mask &= 0xff;
2066 for (ch = 0; ch < 2; ch++)
2067 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
2068 idx, mask, val);
2069 return ret;
2070}
2071EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init_stereo);
2072
Takashi Iwaid5191e52009-11-16 14:58:17 +01002073/**
2074 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
2075 * @codec: HD-audio codec
2076 *
2077 * Resume the all amp commands from the cache.
2078 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002079void snd_hda_codec_resume_amp(struct hda_codec *codec)
2080{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002081 int i;
2082
Takashi Iwaic370dd62012-12-13 18:30:04 +01002083 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01002084 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002085 for (i = 0; i < codec->amp_cache.buf.used; i++) {
2086 struct hda_amp_info *buffer;
2087 u32 key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002088 hda_nid_t nid;
2089 unsigned int idx, dir, ch;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002090 struct hda_amp_info info;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002091
2092 buffer = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwai8565f052012-12-20 12:54:18 +01002093 if (!buffer->head.dirty)
2094 continue;
2095 buffer->head.dirty = 0;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002096 info = *buffer;
2097 key = info.head.key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002098 if (!key)
2099 continue;
2100 nid = key & 0xff;
2101 idx = (key >> 16) & 0xff;
2102 dir = (key >> 24) & 0xff;
2103 for (ch = 0; ch < 2; ch++) {
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002104 if (!(info.head.val & INFO_AMP_VOL(ch)))
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002105 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002106 mutex_unlock(&codec->hash_mutex);
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002107 put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx,
2108 info.vol[ch]);
Takashi Iwaic370dd62012-12-13 18:30:04 +01002109 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002110 }
2111 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01002112 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002113}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002114EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002116static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
2117 unsigned int ofs)
2118{
2119 u32 caps = query_amp_caps(codec, nid, dir);
2120 /* get num steps */
2121 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2122 if (ofs < caps)
2123 caps -= ofs;
2124 return caps;
2125}
2126
Takashi Iwaid5191e52009-11-16 14:58:17 +01002127/**
2128 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
2129 *
2130 * The control element is supposed to have the private_value field
2131 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2132 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002133int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
2134 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135{
2136 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2137 u16 nid = get_amp_nid(kcontrol);
2138 u8 chs = get_amp_channels(kcontrol);
2139 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002140 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002142 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2143 uinfo->count = chs == 3 ? 2 : 1;
2144 uinfo->value.integer.min = 0;
2145 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
2146 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002147 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01002148 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
2149 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 return -EINVAL;
2151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 return 0;
2153}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002154EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002156
2157static inline unsigned int
2158read_amp_value(struct hda_codec *codec, hda_nid_t nid,
2159 int ch, int dir, int idx, unsigned int ofs)
2160{
2161 unsigned int val;
2162 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
2163 val &= HDA_AMP_VOLMASK;
2164 if (val >= ofs)
2165 val -= ofs;
2166 else
2167 val = 0;
2168 return val;
2169}
2170
2171static inline int
2172update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2173 int ch, int dir, int idx, unsigned int ofs,
2174 unsigned int val)
2175{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002176 unsigned int maxval;
2177
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002178 if (val > 0)
2179 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02002180 /* ofs = 0: raw max value */
2181 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002182 if (val > maxval)
2183 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002184 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
2185 HDA_AMP_VOLMASK, val);
2186}
2187
Takashi Iwaid5191e52009-11-16 14:58:17 +01002188/**
2189 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
2190 *
2191 * The control element is supposed to have the private_value field
2192 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2193 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002194int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2195 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196{
2197 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2198 hda_nid_t nid = get_amp_nid(kcontrol);
2199 int chs = get_amp_channels(kcontrol);
2200 int dir = get_amp_direction(kcontrol);
2201 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002202 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 long *valp = ucontrol->value.integer.value;
2204
2205 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002206 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002208 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 return 0;
2210}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002211EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
Takashi Iwaid5191e52009-11-16 14:58:17 +01002213/**
2214 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2215 *
2216 * The control element is supposed to have the private_value field
2217 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2218 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002219int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2220 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221{
2222 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2223 hda_nid_t nid = get_amp_nid(kcontrol);
2224 int chs = get_amp_channels(kcontrol);
2225 int dir = get_amp_direction(kcontrol);
2226 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002227 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 long *valp = ucontrol->value.integer.value;
2229 int change = 0;
2230
Takashi Iwaicb53c622007-08-10 17:21:45 +02002231 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002232 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002233 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002234 valp++;
2235 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002236 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002237 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002238 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 return change;
2240}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002241EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242
Takashi Iwaid5191e52009-11-16 14:58:17 +01002243/**
2244 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2245 *
2246 * The control element is supposed to have the private_value field
2247 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2248 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002249int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2250 unsigned int size, unsigned int __user *_tlv)
2251{
2252 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2253 hda_nid_t nid = get_amp_nid(kcontrol);
2254 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002255 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002256 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002257 u32 caps, val1, val2;
2258
2259 if (size < 4 * sizeof(unsigned int))
2260 return -ENOMEM;
2261 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002262 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2263 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002264 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002265 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002266 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01002267 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02002268 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002269 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2270 return -EFAULT;
2271 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2272 return -EFAULT;
2273 if (put_user(val1, _tlv + 2))
2274 return -EFAULT;
2275 if (put_user(val2, _tlv + 3))
2276 return -EFAULT;
2277 return 0;
2278}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002279EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002280
Takashi Iwaid5191e52009-11-16 14:58:17 +01002281/**
2282 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2283 * @codec: HD-audio codec
2284 * @nid: NID of a reference widget
2285 * @dir: #HDA_INPUT or #HDA_OUTPUT
2286 * @tlv: TLV data to be stored, at least 4 elements
2287 *
2288 * Set (static) TLV data for a virtual master volume using the AMP caps
2289 * obtained from the reference NID.
2290 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002291 */
2292void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2293 unsigned int *tlv)
2294{
2295 u32 caps;
2296 int nums, step;
2297
2298 caps = query_amp_caps(codec, nid, dir);
2299 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2300 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2301 step = (step + 1) * 25;
2302 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2303 tlv[1] = 2 * sizeof(unsigned int);
2304 tlv[2] = -nums * step;
2305 tlv[3] = step;
2306}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002307EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002308
2309/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002310static struct snd_kcontrol *
Takashi Iwaidcda5802012-10-12 17:24:51 +02002311find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002312{
2313 struct snd_ctl_elem_id id;
2314 memset(&id, 0, sizeof(id));
2315 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002316 id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01002317 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002318 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2319 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002320 strcpy(id.name, name);
2321 return snd_ctl_find_id(codec->bus->card, &id);
2322}
2323
Takashi Iwaid5191e52009-11-16 14:58:17 +01002324/**
2325 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2326 * @codec: HD-audio codec
2327 * @name: ctl id name string
2328 *
2329 * Get the control element with the given id string and IFACE_MIXER.
2330 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002331struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2332 const char *name)
2333{
Takashi Iwaidcda5802012-10-12 17:24:51 +02002334 return find_mixer_ctl(codec, name, 0, 0);
Takashi Iwai09f99702008-02-04 12:31:13 +01002335}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002336EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002337
Takashi Iwaidcda5802012-10-12 17:24:51 +02002338static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002339 int start_idx)
Takashi Iwai1afe2062010-12-23 10:17:52 +01002340{
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002341 int i, idx;
2342 /* 16 ctlrs should be large enough */
2343 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
2344 if (!find_mixer_ctl(codec, name, 0, idx))
Takashi Iwai1afe2062010-12-23 10:17:52 +01002345 return idx;
2346 }
2347 return -EBUSY;
2348}
2349
Takashi Iwaid5191e52009-11-16 14:58:17 +01002350/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002351 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002352 * @codec: HD-audio codec
2353 * @nid: corresponding NID (optional)
2354 * @kctl: the control element to assign
2355 *
2356 * Add the given control element to an array inside the codec instance.
2357 * All control elements belonging to a codec are supposed to be added
2358 * by this function so that a proper clean-up works at the free or
2359 * reconfiguration time.
2360 *
2361 * If non-zero @nid is passed, the NID is assigned to the control element.
2362 * The assignment is shown in the codec proc file.
2363 *
2364 * snd_hda_ctl_add() checks the control subdev id field whether
2365 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002366 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2367 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002368 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002369int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2370 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002371{
2372 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002373 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002374 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002375
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002376 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002377 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002378 if (nid == 0)
2379 nid = get_amp_nid_(kctl->private_value);
2380 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002381 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2382 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002383 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002384 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002385 err = snd_ctl_add(codec->bus->card, kctl);
2386 if (err < 0)
2387 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002388 item = snd_array_new(&codec->mixers);
2389 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002390 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002391 item->kctl = kctl;
2392 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002393 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002394 return 0;
2395}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002396EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002397
Takashi Iwaid5191e52009-11-16 14:58:17 +01002398/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002399 * snd_hda_add_nid - Assign a NID to a control element
2400 * @codec: HD-audio codec
2401 * @nid: corresponding NID (optional)
2402 * @kctl: the control element to assign
2403 * @index: index to kctl
2404 *
2405 * Add the given control element to an array inside the codec instance.
2406 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2407 * NID:KCTL mapping - for example "Capture Source" selector.
2408 */
2409int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2410 unsigned int index, hda_nid_t nid)
2411{
2412 struct hda_nid_item *item;
2413
2414 if (nid > 0) {
2415 item = snd_array_new(&codec->nids);
2416 if (!item)
2417 return -ENOMEM;
2418 item->kctl = kctl;
2419 item->index = index;
2420 item->nid = nid;
2421 return 0;
2422 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01002423 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2424 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002425 return -EINVAL;
2426}
2427EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2428
2429/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002430 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2431 * @codec: HD-audio codec
2432 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002433void snd_hda_ctls_clear(struct hda_codec *codec)
2434{
2435 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002436 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002437 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002438 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002439 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002440 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002441}
2442
Takashi Iwaia65d6292009-02-23 16:57:04 +01002443/* pseudo device locking
2444 * toggle card->shutdown to allow/disallow the device access (as a hack)
2445 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002446int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002447{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002448 struct snd_card *card = bus->card;
2449 struct hda_codec *codec;
2450
Takashi Iwaia65d6292009-02-23 16:57:04 +01002451 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002452 if (card->shutdown)
2453 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002454 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002455 if (!list_empty(&card->ctl_files))
2456 goto err_clear;
2457
2458 list_for_each_entry(codec, &bus->codec_list, list) {
2459 int pcm;
2460 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2461 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2462 if (!cpcm->pcm)
2463 continue;
2464 if (cpcm->pcm->streams[0].substream_opened ||
2465 cpcm->pcm->streams[1].substream_opened)
2466 goto err_clear;
2467 }
2468 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01002469 spin_unlock(&card->files_lock);
2470 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002471
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002472 err_clear:
2473 card->shutdown = 0;
2474 err_unlock:
2475 spin_unlock(&card->files_lock);
2476 return -EINVAL;
2477}
2478EXPORT_SYMBOL_HDA(snd_hda_lock_devices);
2479
2480void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002481{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002482 struct snd_card *card = bus->card;
2483
2484 card = bus->card;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002485 spin_lock(&card->files_lock);
2486 card->shutdown = 0;
2487 spin_unlock(&card->files_lock);
2488}
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002489EXPORT_SYMBOL_HDA(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002490
Takashi Iwaid5191e52009-11-16 14:58:17 +01002491/**
2492 * snd_hda_codec_reset - Clear all objects assigned to the codec
2493 * @codec: HD-audio codec
2494 *
2495 * This frees the all PCM and control elements assigned to the codec, and
2496 * clears the caches and restores the pin default configurations.
2497 *
2498 * When a device is being used, it returns -EBSY. If successfully freed,
2499 * returns zero.
2500 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002501int snd_hda_codec_reset(struct hda_codec *codec)
2502{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002503 struct hda_bus *bus = codec->bus;
2504 struct snd_card *card = bus->card;
2505 int i;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002506
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002507 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002508 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002509
2510 /* OK, let it free */
David Henningsson26a6cb62012-10-09 15:04:21 +02002511 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai83012a72012-08-24 18:38:08 +02002512#ifdef CONFIG_PM
Takashi Iwaia2d96e72012-05-09 12:36:22 +02002513 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwai339876d2012-05-08 16:57:12 +02002514 codec->power_on = 0;
2515 codec->power_transition = 0;
2516 codec->power_jiffies = jiffies;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002517 flush_workqueue(bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002518#endif
2519 snd_hda_ctls_clear(codec);
2520 /* relase PCMs */
2521 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002522 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002523 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002524 clear_bit(codec->pcm_info[i].device,
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002525 bus->pcm_dev_bits);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002526 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002527 }
2528 if (codec->patch_ops.free)
2529 codec->patch_ops.free(codec);
Takashi Iwai07dc59f2012-09-10 09:39:31 +02002530 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002531 snd_hda_jack_tbl_clear(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002532 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002533 codec->spec = NULL;
2534 free_hda_cache(&codec->amp_cache);
2535 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002536 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2537 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002538 /* free only driver_pins so that init_pins + user_pins are restored */
2539 snd_array_free(&codec->driver_pins);
Takashi Iwai09a60712012-06-26 15:01:33 +02002540 snd_array_free(&codec->cvt_setups);
2541 snd_array_free(&codec->spdif_out);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01002542 snd_array_free(&codec->verbs);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002543 codec->num_pcms = 0;
2544 codec->pcm_info = NULL;
2545 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002546 codec->slave_dig_outs = NULL;
2547 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002548 module_put(codec->owner);
2549 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002550
2551 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002552 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002553 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002554}
2555
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002556typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2557
2558/* apply the function to all matching slave ctls in the mixer list */
2559static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002560 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002561{
2562 struct hda_nid_item *items;
2563 const char * const *s;
2564 int i, err;
2565
2566 items = codec->mixers.list;
2567 for (i = 0; i < codec->mixers.used; i++) {
2568 struct snd_kcontrol *sctl = items[i].kctl;
2569 if (!sctl || !sctl->id.name ||
2570 sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2571 continue;
2572 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002573 char tmpname[sizeof(sctl->id.name)];
2574 const char *name = *s;
2575 if (suffix) {
2576 snprintf(tmpname, sizeof(tmpname), "%s %s",
2577 name, suffix);
2578 name = tmpname;
2579 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002580 if (!strcmp(sctl->id.name, name)) {
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002581 err = func(data, sctl);
2582 if (err)
2583 return err;
2584 break;
2585 }
2586 }
2587 }
2588 return 0;
2589}
2590
2591static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2592{
2593 return 1;
2594}
2595
Takashi Iwai18478e82012-03-09 17:51:10 +01002596/* guess the value corresponding to 0dB */
2597static int get_kctl_0dB_offset(struct snd_kcontrol *kctl)
2598{
2599 int _tlv[4];
2600 const int *tlv = NULL;
2601 int val = -1;
2602
2603 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2604 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2605 mm_segment_t fs = get_fs();
2606 set_fs(get_ds());
2607 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2608 tlv = _tlv;
2609 set_fs(fs);
2610 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2611 tlv = kctl->tlv.p;
2612 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE)
2613 val = -tlv[2] / tlv[3];
2614 return val;
2615}
2616
2617/* call kctl->put with the given value(s) */
2618static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2619{
2620 struct snd_ctl_elem_value *ucontrol;
2621 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2622 if (!ucontrol)
2623 return -ENOMEM;
2624 ucontrol->value.integer.value[0] = val;
2625 ucontrol->value.integer.value[1] = val;
2626 kctl->put(kctl, ucontrol);
2627 kfree(ucontrol);
2628 return 0;
2629}
2630
2631/* initialize the slave volume with 0dB */
2632static int init_slave_0dB(void *data, struct snd_kcontrol *slave)
2633{
2634 int offset = get_kctl_0dB_offset(slave);
2635 if (offset > 0)
2636 put_kctl_with_value(slave, offset);
2637 return 0;
2638}
2639
2640/* unmute the slave */
2641static int init_slave_unmute(void *data, struct snd_kcontrol *slave)
2642{
2643 return put_kctl_with_value(slave, 1);
2644}
2645
Takashi Iwaid5191e52009-11-16 14:58:17 +01002646/**
2647 * snd_hda_add_vmaster - create a virtual master control and add slaves
2648 * @codec: HD-audio codec
2649 * @name: vmaster control name
2650 * @tlv: TLV data (optional)
2651 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002652 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002653 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002654 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002655 *
2656 * Create a virtual master control with the given name. The TLV data
2657 * must be either NULL or a valid data.
2658 *
2659 * @slaves is a NULL-terminated array of strings, each of which is a
2660 * slave control name. All controls with these names are assigned to
2661 * the new virtual master control.
2662 *
2663 * This function returns zero if successful or a negative error code.
2664 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002665int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002666 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002667 const char *suffix, bool init_slave_vol,
2668 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002669{
2670 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002671 int err;
2672
Takashi Iwai29e58532012-03-12 12:25:03 +01002673 if (ctl_ret)
2674 *ctl_ret = NULL;
2675
Takashi Iwai9322ca52012-02-03 14:28:01 +01002676 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002677 if (err != 1) {
Takashi Iwai2f085542008-02-22 18:43:50 +01002678 snd_printdd("No slave found for %s\n", name);
2679 return 0;
2680 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002681 kctl = snd_ctl_make_virtual_master(name, tlv);
2682 if (!kctl)
2683 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002684 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002685 if (err < 0)
2686 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002687
Takashi Iwai9322ca52012-02-03 14:28:01 +01002688 err = map_slaves(codec, slaves, suffix,
2689 (map_slave_func_t)snd_ctl_add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002690 if (err < 0)
2691 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002692
2693 /* init with master mute & zero volume */
2694 put_kctl_with_value(kctl, 0);
2695 if (init_slave_vol)
2696 map_slaves(codec, slaves, suffix,
2697 tlv ? init_slave_0dB : init_slave_unmute, kctl);
2698
Takashi Iwai29e58532012-03-12 12:25:03 +01002699 if (ctl_ret)
2700 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002701 return 0;
2702}
Takashi Iwai18478e82012-03-09 17:51:10 +01002703EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002704
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002705/*
2706 * mute-LED control using vmaster
2707 */
2708static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2709 struct snd_ctl_elem_info *uinfo)
2710{
2711 static const char * const texts[] = {
David Henningssonc86c2d42013-01-03 14:12:29 +01002712 "On", "Off", "Follow Master"
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002713 };
2714 unsigned int index;
2715
2716 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2717 uinfo->count = 1;
2718 uinfo->value.enumerated.items = 3;
2719 index = uinfo->value.enumerated.item;
2720 if (index >= 3)
2721 index = 2;
2722 strcpy(uinfo->value.enumerated.name, texts[index]);
2723 return 0;
2724}
2725
2726static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2727 struct snd_ctl_elem_value *ucontrol)
2728{
2729 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2730 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2731 return 0;
2732}
2733
2734static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2735 struct snd_ctl_elem_value *ucontrol)
2736{
2737 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2738 unsigned int old_mode = hook->mute_mode;
2739
2740 hook->mute_mode = ucontrol->value.enumerated.item[0];
2741 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2742 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2743 if (old_mode == hook->mute_mode)
2744 return 0;
2745 snd_hda_sync_vmaster_hook(hook);
2746 return 1;
2747}
2748
2749static struct snd_kcontrol_new vmaster_mute_mode = {
2750 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2751 .name = "Mute-LED Mode",
2752 .info = vmaster_mute_mode_info,
2753 .get = vmaster_mute_mode_get,
2754 .put = vmaster_mute_mode_put,
2755};
2756
2757/*
2758 * Add a mute-LED hook with the given vmaster switch kctl
2759 * "Mute-LED Mode" control is automatically created and associated with
2760 * the given hook.
2761 */
2762int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735c2012-03-13 07:55:10 +01002763 struct hda_vmaster_mute_hook *hook,
2764 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002765{
2766 struct snd_kcontrol *kctl;
2767
2768 if (!hook->hook || !hook->sw_kctl)
2769 return 0;
2770 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2771 hook->codec = codec;
2772 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735c2012-03-13 07:55:10 +01002773 if (!expose_enum_ctl)
2774 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002775 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2776 if (!kctl)
2777 return -ENOMEM;
2778 return snd_hda_ctl_add(codec, 0, kctl);
2779}
2780EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook);
2781
2782/*
2783 * Call the hook with the current value for synchronization
2784 * Should be called in init callback
2785 */
2786void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2787{
2788 if (!hook->hook || !hook->codec)
2789 return;
2790 switch (hook->mute_mode) {
2791 case HDA_VMUTE_FOLLOW_MASTER:
2792 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2793 break;
2794 default:
2795 hook->hook(hook->codec, hook->mute_mode);
2796 break;
2797 }
2798}
2799EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook);
2800
2801
Takashi Iwaid5191e52009-11-16 14:58:17 +01002802/**
2803 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2804 *
2805 * The control element is supposed to have the private_value field
2806 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2807 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002808int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2809 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810{
2811 int chs = get_amp_channels(kcontrol);
2812
2813 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2814 uinfo->count = chs == 3 ? 2 : 1;
2815 uinfo->value.integer.min = 0;
2816 uinfo->value.integer.max = 1;
2817 return 0;
2818}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002819EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820
Takashi Iwaid5191e52009-11-16 14:58:17 +01002821/**
2822 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2823 *
2824 * The control element is supposed to have the private_value field
2825 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2826 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002827int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2828 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829{
2830 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2831 hda_nid_t nid = get_amp_nid(kcontrol);
2832 int chs = get_amp_channels(kcontrol);
2833 int dir = get_amp_direction(kcontrol);
2834 int idx = get_amp_index(kcontrol);
2835 long *valp = ucontrol->value.integer.value;
2836
2837 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002838 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002839 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002841 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002842 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 return 0;
2844}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002845EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846
Takashi Iwaid5191e52009-11-16 14:58:17 +01002847/**
2848 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2849 *
2850 * The control element is supposed to have the private_value field
2851 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2852 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002853int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2854 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855{
2856 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2857 hda_nid_t nid = get_amp_nid(kcontrol);
2858 int chs = get_amp_channels(kcontrol);
2859 int dir = get_amp_direction(kcontrol);
2860 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 long *valp = ucontrol->value.integer.value;
2862 int change = 0;
2863
Takashi Iwaicb53c622007-08-10 17:21:45 +02002864 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002865 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002866 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002867 HDA_AMP_MUTE,
2868 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002869 valp++;
2870 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002871 if (chs & 2)
2872 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002873 HDA_AMP_MUTE,
2874 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002875 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002876 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 return change;
2878}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002879EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880
2881/*
Takashi Iwai985be542005-11-02 18:26:49 +01002882 * bound volume controls
2883 *
2884 * bind multiple volumes (# indices, from 0)
2885 */
2886
2887#define AMP_VAL_IDX_SHIFT 19
2888#define AMP_VAL_IDX_MASK (0x0f<<19)
2889
Takashi Iwaid5191e52009-11-16 14:58:17 +01002890/**
2891 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2892 *
2893 * The control element is supposed to have the private_value field
2894 * set up via HDA_BIND_MUTE*() macros.
2895 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002896int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2897 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002898{
2899 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2900 unsigned long pval;
2901 int err;
2902
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002903 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002904 pval = kcontrol->private_value;
2905 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2906 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2907 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002908 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002909 return err;
2910}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002911EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002912
Takashi Iwaid5191e52009-11-16 14:58:17 +01002913/**
2914 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2915 *
2916 * The control element is supposed to have the private_value field
2917 * set up via HDA_BIND_MUTE*() macros.
2918 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002919int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2920 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002921{
2922 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2923 unsigned long pval;
2924 int i, indices, err = 0, change = 0;
2925
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002926 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002927 pval = kcontrol->private_value;
2928 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2929 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002930 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2931 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002932 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2933 if (err < 0)
2934 break;
2935 change |= err;
2936 }
2937 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002938 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002939 return err < 0 ? err : change;
2940}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002941EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002942
Takashi Iwaid5191e52009-11-16 14:58:17 +01002943/**
2944 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2945 *
2946 * The control element is supposed to have the private_value field
2947 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002948 */
2949int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2950 struct snd_ctl_elem_info *uinfo)
2951{
2952 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2953 struct hda_bind_ctls *c;
2954 int err;
2955
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002956 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002957 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002958 kcontrol->private_value = *c->values;
2959 err = c->ops->info(kcontrol, uinfo);
2960 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002961 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002962 return err;
2963}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002964EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002965
Takashi Iwaid5191e52009-11-16 14:58:17 +01002966/**
2967 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2968 *
2969 * The control element is supposed to have the private_value field
2970 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2971 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002972int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2973 struct snd_ctl_elem_value *ucontrol)
2974{
2975 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2976 struct hda_bind_ctls *c;
2977 int err;
2978
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002979 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002980 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002981 kcontrol->private_value = *c->values;
2982 err = c->ops->get(kcontrol, ucontrol);
2983 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002984 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002985 return err;
2986}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002987EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002988
Takashi Iwaid5191e52009-11-16 14:58:17 +01002989/**
2990 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2991 *
2992 * The control element is supposed to have the private_value field
2993 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2994 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002995int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2996 struct snd_ctl_elem_value *ucontrol)
2997{
2998 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2999 struct hda_bind_ctls *c;
3000 unsigned long *vals;
3001 int err = 0, change = 0;
3002
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003003 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003004 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003005 for (vals = c->values; *vals; vals++) {
3006 kcontrol->private_value = *vals;
3007 err = c->ops->put(kcontrol, ucontrol);
3008 if (err < 0)
3009 break;
3010 change |= err;
3011 }
3012 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003013 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003014 return err < 0 ? err : change;
3015}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003016EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02003017
Takashi Iwaid5191e52009-11-16 14:58:17 +01003018/**
3019 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
3020 *
3021 * The control element is supposed to have the private_value field
3022 * set up via HDA_BIND_VOL() macro.
3023 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003024int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
3025 unsigned int size, unsigned int __user *tlv)
3026{
3027 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3028 struct hda_bind_ctls *c;
3029 int err;
3030
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003031 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003032 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003033 kcontrol->private_value = *c->values;
3034 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
3035 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003036 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003037 return err;
3038}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003039EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02003040
3041struct hda_ctl_ops snd_hda_bind_vol = {
3042 .info = snd_hda_mixer_amp_volume_info,
3043 .get = snd_hda_mixer_amp_volume_get,
3044 .put = snd_hda_mixer_amp_volume_put,
3045 .tlv = snd_hda_mixer_amp_tlv
3046};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003047EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02003048
3049struct hda_ctl_ops snd_hda_bind_sw = {
3050 .info = snd_hda_mixer_amp_switch_info,
3051 .get = snd_hda_mixer_amp_switch_get,
3052 .put = snd_hda_mixer_amp_switch_put,
3053 .tlv = snd_hda_mixer_amp_tlv
3054};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003055EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02003056
3057/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 * SPDIF out controls
3059 */
3060
Takashi Iwai0ba21762007-04-16 11:29:14 +02003061static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
3062 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063{
3064 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
3065 uinfo->count = 1;
3066 return 0;
3067}
3068
Takashi Iwai0ba21762007-04-16 11:29:14 +02003069static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
3070 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071{
3072 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3073 IEC958_AES0_NONAUDIO |
3074 IEC958_AES0_CON_EMPHASIS_5015 |
3075 IEC958_AES0_CON_NOT_COPYRIGHT;
3076 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
3077 IEC958_AES1_CON_ORIGINAL;
3078 return 0;
3079}
3080
Takashi Iwai0ba21762007-04-16 11:29:14 +02003081static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
3082 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083{
3084 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3085 IEC958_AES0_NONAUDIO |
3086 IEC958_AES0_PRO_EMPHASIS_5015;
3087 return 0;
3088}
3089
Takashi Iwai0ba21762007-04-16 11:29:14 +02003090static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
3091 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092{
3093 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003094 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003095 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003097 mutex_lock(&codec->spdif_mutex);
3098 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c9359762011-06-01 11:14:17 -06003099 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
3100 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
3101 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
3102 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003103 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104
3105 return 0;
3106}
3107
3108/* convert from SPDIF status bits to HDA SPDIF bits
3109 * bit 0 (DigEn) is always set zero (to be filled later)
3110 */
3111static unsigned short convert_from_spdif_status(unsigned int sbits)
3112{
3113 unsigned short val = 0;
3114
3115 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003116 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003118 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003120 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
3121 IEC958_AES0_PRO_EMPHASIS_5015)
3122 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003124 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
3125 IEC958_AES0_CON_EMPHASIS_5015)
3126 val |= AC_DIG1_EMPHASIS;
3127 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
3128 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02003130 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
3132 }
3133 return val;
3134}
3135
3136/* convert to SPDIF status bits from HDA SPDIF bits
3137 */
3138static unsigned int convert_to_spdif_status(unsigned short val)
3139{
3140 unsigned int sbits = 0;
3141
Takashi Iwai0ba21762007-04-16 11:29:14 +02003142 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003144 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 sbits |= IEC958_AES0_PROFESSIONAL;
3146 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwaia686fd12013-03-20 15:42:00 +01003147 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
3149 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003150 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003152 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003154 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3156 sbits |= val & (0x7f << 8);
3157 }
3158 return sbits;
3159}
3160
Takashi Iwai2f728532008-09-25 16:32:41 +02003161/* set digital convert verbs both for the given NID and its slaves */
3162static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3163 int verb, int val)
3164{
Takashi Iwaidda14412011-05-02 11:29:30 +02003165 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02003166
Takashi Iwai9e976972008-11-25 08:17:20 +01003167 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003168 d = codec->slave_dig_outs;
3169 if (!d)
3170 return;
3171 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01003172 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003173}
3174
3175static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3176 int dig1, int dig2)
3177{
3178 if (dig1 != -1)
3179 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3180 if (dig2 != -1)
3181 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3182}
3183
Takashi Iwai0ba21762007-04-16 11:29:14 +02003184static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3185 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186{
3187 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003188 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003189 struct hda_spdif_out *spdif;
3190 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 unsigned short val;
3192 int change;
3193
Ingo Molnar62932df2006-01-16 16:34:20 +01003194 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003195 spdif = snd_array_elem(&codec->spdif_out, idx);
3196 nid = spdif->nid;
Stephen Warren7c9359762011-06-01 11:14:17 -06003197 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3199 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3200 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c9359762011-06-01 11:14:17 -06003201 val = convert_from_spdif_status(spdif->status);
3202 val |= spdif->ctls & 1;
3203 change = spdif->ctls != val;
3204 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003205 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02003206 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01003207 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 return change;
3209}
3210
Takashi Iwaia5ce8892007-07-23 15:42:26 +02003211#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212
Takashi Iwai0ba21762007-04-16 11:29:14 +02003213static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3214 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215{
3216 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003217 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003218 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003220 mutex_lock(&codec->spdif_mutex);
3221 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c9359762011-06-01 11:14:17 -06003222 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003223 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 return 0;
3225}
3226
Stephen Warren74b654c2011-06-01 11:14:18 -06003227static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3228 int dig1, int dig2)
3229{
3230 set_dig_out_convert(codec, nid, dig1, dig2);
3231 /* unmute amp switch (if any) */
3232 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3233 (dig1 & AC_DIG1_ENABLE))
3234 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3235 HDA_AMP_MUTE, 0);
3236}
3237
Takashi Iwai0ba21762007-04-16 11:29:14 +02003238static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3239 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240{
3241 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06003242 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003243 struct hda_spdif_out *spdif;
3244 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 unsigned short val;
3246 int change;
3247
Ingo Molnar62932df2006-01-16 16:34:20 +01003248 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003249 spdif = snd_array_elem(&codec->spdif_out, idx);
3250 nid = spdif->nid;
Stephen Warren7c9359762011-06-01 11:14:17 -06003251 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003253 val |= AC_DIG1_ENABLE;
Stephen Warren7c9359762011-06-01 11:14:17 -06003254 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003255 spdif->ctls = val;
3256 if (change && nid != (u16)-1)
3257 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003258 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 return change;
3260}
3261
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003262static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 {
3264 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3265 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003266 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 .info = snd_hda_spdif_mask_info,
3268 .get = snd_hda_spdif_cmask_get,
3269 },
3270 {
3271 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3272 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003273 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 .info = snd_hda_spdif_mask_info,
3275 .get = snd_hda_spdif_pmask_get,
3276 },
3277 {
3278 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003279 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 .info = snd_hda_spdif_mask_info,
3281 .get = snd_hda_spdif_default_get,
3282 .put = snd_hda_spdif_default_put,
3283 },
3284 {
3285 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003286 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 .info = snd_hda_spdif_out_switch_info,
3288 .get = snd_hda_spdif_out_switch_get,
3289 .put = snd_hda_spdif_out_switch_put,
3290 },
3291 { } /* end */
3292};
3293
3294/**
Takashi Iwaidcda5802012-10-12 17:24:51 +02003295 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 * @codec: the HDA codec
Takashi Iwaidcda5802012-10-12 17:24:51 +02003297 * @associated_nid: NID that new ctls associated with
3298 * @cvt_nid: converter NID
3299 * @type: HDA_PCM_TYPE_*
3300 * Creates controls related with the digital output.
3301 * Called from each patch supporting the digital out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 *
3303 * Returns 0 if successful, or a negative error code.
3304 */
Takashi Iwaidcda5802012-10-12 17:24:51 +02003305int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3306 hda_nid_t associated_nid,
3307 hda_nid_t cvt_nid,
3308 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309{
3310 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003311 struct snd_kcontrol *kctl;
3312 struct snd_kcontrol_new *dig_mix;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003313 int idx = 0;
3314 const int spdif_index = 16;
Stephen Warren7c9359762011-06-01 11:14:17 -06003315 struct hda_spdif_out *spdif;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003316 struct hda_bus *bus = codec->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003318 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003319 type == HDA_PCM_TYPE_SPDIF) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003320 idx = spdif_index;
3321 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003322 type == HDA_PCM_TYPE_HDMI) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003323 /* suppose a single SPDIF device */
3324 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3325 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
3326 if (!kctl)
3327 break;
3328 kctl->id.index = spdif_index;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003329 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003330 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003331 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003332 if (!bus->primary_dig_out_type)
3333 bus->primary_dig_out_type = type;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003334
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003335 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003336 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003337 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
3338 return -EBUSY;
3339 }
Stephen Warren7c9359762011-06-01 11:14:17 -06003340 spdif = snd_array_new(&codec->spdif_out);
Mengdong Lin25336e82013-03-07 14:10:25 -05003341 if (!spdif)
3342 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3344 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003345 if (!kctl)
3346 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01003347 kctl->id.index = idx;
Stephen Warren7c9359762011-06-01 11:14:17 -06003348 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003349 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003350 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 return err;
3352 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003353 spdif->nid = cvt_nid;
3354 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c9359762011-06-01 11:14:17 -06003355 AC_VERB_GET_DIGI_CONVERT_1, 0);
3356 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 return 0;
3358}
Takashi Iwaidcda5802012-10-12 17:24:51 +02003359EXPORT_SYMBOL_HDA(snd_hda_create_dig_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003361/* get the hda_spdif_out entry from the given NID
3362 * call within spdif_mutex lock
3363 */
Stephen Warren7c9359762011-06-01 11:14:17 -06003364struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3365 hda_nid_t nid)
3366{
3367 int i;
3368 for (i = 0; i < codec->spdif_out.used; i++) {
3369 struct hda_spdif_out *spdif =
3370 snd_array_elem(&codec->spdif_out, i);
3371 if (spdif->nid == nid)
3372 return spdif;
3373 }
3374 return NULL;
3375}
3376EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
3377
Stephen Warren74b654c2011-06-01 11:14:18 -06003378void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3379{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003380 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003381
3382 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003383 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003384 spdif->nid = (u16)-1;
3385 mutex_unlock(&codec->spdif_mutex);
3386}
3387EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
3388
3389void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3390{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003391 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003392 unsigned short val;
3393
3394 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003395 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003396 if (spdif->nid != nid) {
3397 spdif->nid = nid;
3398 val = spdif->ctls;
3399 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3400 }
3401 mutex_unlock(&codec->spdif_mutex);
3402}
3403EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
3404
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003406 * SPDIF sharing with analog output
3407 */
3408static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3409 struct snd_ctl_elem_value *ucontrol)
3410{
3411 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3412 ucontrol->value.integer.value[0] = mout->share_spdif;
3413 return 0;
3414}
3415
3416static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3417 struct snd_ctl_elem_value *ucontrol)
3418{
3419 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3420 mout->share_spdif = !!ucontrol->value.integer.value[0];
3421 return 0;
3422}
3423
3424static struct snd_kcontrol_new spdif_share_sw = {
3425 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3426 .name = "IEC958 Default PCM Playback Switch",
3427 .info = snd_ctl_boolean_mono_info,
3428 .get = spdif_share_sw_get,
3429 .put = spdif_share_sw_put,
3430};
3431
Takashi Iwaid5191e52009-11-16 14:58:17 +01003432/**
3433 * snd_hda_create_spdif_share_sw - create Default PCM switch
3434 * @codec: the HDA codec
3435 * @mout: multi-out instance
3436 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003437int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3438 struct hda_multi_out *mout)
3439{
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003440 struct snd_kcontrol *kctl;
3441
Takashi Iwai9a081602008-02-12 18:37:26 +01003442 if (!mout->dig_out_nid)
3443 return 0;
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003444
3445 kctl = snd_ctl_new1(&spdif_share_sw, mout);
3446 if (!kctl)
3447 return -ENOMEM;
Takashi Iwai9a081602008-02-12 18:37:26 +01003448 /* ATTENTION: here mout is passed as private_data, instead of codec */
Mengdong Lin4c7a5482013-03-07 14:11:05 -05003449 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
Takashi Iwai9a081602008-02-12 18:37:26 +01003450}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003451EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003452
3453/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 * SPDIF input
3455 */
3456
3457#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3458
Takashi Iwai0ba21762007-04-16 11:29:14 +02003459static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3460 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461{
3462 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3463
3464 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3465 return 0;
3466}
3467
Takashi Iwai0ba21762007-04-16 11:29:14 +02003468static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3469 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470{
3471 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3472 hda_nid_t nid = kcontrol->private_value;
3473 unsigned int val = !!ucontrol->value.integer.value[0];
3474 int change;
3475
Ingo Molnar62932df2006-01-16 16:34:20 +01003476 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003478 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003480 snd_hda_codec_write_cache(codec, nid, 0,
3481 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003483 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 return change;
3485}
3486
Takashi Iwai0ba21762007-04-16 11:29:14 +02003487static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3488 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489{
3490 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3491 hda_nid_t nid = kcontrol->private_value;
3492 unsigned short val;
3493 unsigned int sbits;
3494
Andrew Paprocki3982d172007-12-19 12:13:44 +01003495 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 sbits = convert_to_spdif_status(val);
3497 ucontrol->value.iec958.status[0] = sbits;
3498 ucontrol->value.iec958.status[1] = sbits >> 8;
3499 ucontrol->value.iec958.status[2] = sbits >> 16;
3500 ucontrol->value.iec958.status[3] = sbits >> 24;
3501 return 0;
3502}
3503
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003504static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 {
3506 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003507 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 .info = snd_hda_spdif_in_switch_info,
3509 .get = snd_hda_spdif_in_switch_get,
3510 .put = snd_hda_spdif_in_switch_put,
3511 },
3512 {
3513 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3514 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003515 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 .info = snd_hda_spdif_mask_info,
3517 .get = snd_hda_spdif_in_status_get,
3518 },
3519 { } /* end */
3520};
3521
3522/**
3523 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3524 * @codec: the HDA codec
3525 * @nid: audio in widget NID
3526 *
3527 * Creates controls related with the SPDIF input.
3528 * Called from each patch supporting the SPDIF in.
3529 *
3530 * Returns 0 if successful, or a negative error code.
3531 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003532int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533{
3534 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003535 struct snd_kcontrol *kctl;
3536 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003537 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538
Takashi Iwaidcda5802012-10-12 17:24:51 +02003539 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003540 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003541 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3542 return -EBUSY;
3543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3545 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003546 if (!kctl)
3547 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003549 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003550 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 return err;
3552 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003553 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003554 snd_hda_codec_read(codec, nid, 0,
3555 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003556 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 return 0;
3558}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003559EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003561/*
3562 * command cache
3563 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564
Takashi Iwaic370dd62012-12-13 18:30:04 +01003565/* build a 31bit cache key with the widget id and the command parameter */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003566#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3567#define get_cmd_cache_nid(key) ((key) & 0xff)
3568#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3569
3570/**
3571 * snd_hda_codec_write_cache - send a single command with caching
3572 * @codec: the HDA codec
3573 * @nid: NID to send the command
3574 * @direct: direct flag
3575 * @verb: the verb to send
3576 * @parm: the parameter for the verb
3577 *
3578 * Send a single command without waiting for response.
3579 *
3580 * Returns 0 if successful, or a negative error code.
3581 */
3582int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3583 int direct, unsigned int verb, unsigned int parm)
3584{
Takashi Iwaic370dd62012-12-13 18:30:04 +01003585 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003586 struct hda_cache_head *c;
3587 u32 key;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003588 unsigned int cache_only;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003589
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003590 cache_only = codec->cached_write;
3591 if (!cache_only) {
Takashi Iwaic370dd62012-12-13 18:30:04 +01003592 err = snd_hda_codec_write(codec, nid, direct, verb, parm);
3593 if (err < 0)
3594 return err;
3595 }
3596
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003597 /* parm may contain the verb stuff for get/set amp */
3598 verb = verb | (parm >> 8);
3599 parm &= 0xff;
3600 key = build_cmd_cache_key(nid, verb);
3601 mutex_lock(&codec->bus->cmd_mutex);
3602 c = get_alloc_hash(&codec->cmd_cache, key);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003603 if (c) {
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003604 c->val = parm;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003605 c->dirty = cache_only;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003606 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003607 mutex_unlock(&codec->bus->cmd_mutex);
3608 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003609}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003610EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003611
Takashi Iwaid5191e52009-11-16 14:58:17 +01003612/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003613 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3614 * @codec: the HDA codec
3615 * @nid: NID to send the command
3616 * @direct: direct flag
3617 * @verb: the verb to send
3618 * @parm: the parameter for the verb
3619 *
3620 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3621 * command if the parameter is already identical with the cached value.
3622 * If not, it sends the command and refreshes the cache.
3623 *
3624 * Returns 0 if successful, or a negative error code.
3625 */
3626int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3627 int direct, unsigned int verb, unsigned int parm)
3628{
3629 struct hda_cache_head *c;
3630 u32 key;
3631
3632 /* parm may contain the verb stuff for get/set amp */
3633 verb = verb | (parm >> 8);
3634 parm &= 0xff;
3635 key = build_cmd_cache_key(nid, verb);
3636 mutex_lock(&codec->bus->cmd_mutex);
3637 c = get_hash(&codec->cmd_cache, key);
3638 if (c && c->val == parm) {
3639 mutex_unlock(&codec->bus->cmd_mutex);
3640 return 0;
3641 }
3642 mutex_unlock(&codec->bus->cmd_mutex);
3643 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3644}
3645EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3646
3647/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003648 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3649 * @codec: HD-audio codec
3650 *
3651 * Execute all verbs recorded in the command caches to resume.
3652 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003653void snd_hda_codec_resume_cache(struct hda_codec *codec)
3654{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003655 int i;
3656
Takashi Iwaic370dd62012-12-13 18:30:04 +01003657 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01003658 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003659 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3660 struct hda_cache_head *buffer;
3661 u32 key;
3662
3663 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3664 key = buffer->key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003665 if (!key)
3666 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003667 if (!buffer->dirty)
3668 continue;
3669 buffer->dirty = 0;
3670 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003671 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3672 get_cmd_cache_cmd(key), buffer->val);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003673 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003674 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01003675 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003676}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003677EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003678
3679/**
3680 * snd_hda_sequence_write_cache - sequence writes with caching
3681 * @codec: the HDA codec
3682 * @seq: VERB array to send
3683 *
3684 * Send the commands sequentially from the given array.
3685 * Thte commands are recorded on cache for power-save and resume.
3686 * The array must be terminated with NID=0.
3687 */
3688void snd_hda_sequence_write_cache(struct hda_codec *codec,
3689 const struct hda_verb *seq)
3690{
3691 for (; seq->nid; seq++)
3692 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3693 seq->param);
3694}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003695EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003696
Takashi Iwaidc870f32013-01-22 15:24:30 +01003697/**
3698 * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
3699 * @codec: HD-audio codec
3700 */
3701void snd_hda_codec_flush_cache(struct hda_codec *codec)
3702{
3703 snd_hda_codec_resume_amp(codec);
3704 snd_hda_codec_resume_cache(codec);
3705}
3706EXPORT_SYMBOL_HDA(snd_hda_codec_flush_cache);
3707
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003708void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003709 unsigned int power_state)
Takashi Iwai54d17402005-11-21 16:33:22 +01003710{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003711 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003712 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003713
Takashi Iwaicb53c622007-08-10 17:21:45 +02003714 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003715 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003716 unsigned int state = power_state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003717 if (!(wcaps & AC_WCAP_POWER))
3718 continue;
Takashi Iwai9419ab62013-01-24 17:23:35 +01003719 if (codec->power_filter) {
3720 state = codec->power_filter(codec, nid, power_state);
3721 if (state != power_state && power_state == AC_PWRST_D3)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003722 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003723 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003724 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003725 state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003726 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003727}
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003728EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3729
3730/*
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003731 * supported power states check
3732 */
3733static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
3734 unsigned int power_state)
3735{
3736 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
3737
Mengdong Line037cb42012-08-10 14:11:58 +02003738 if (sup == -1)
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003739 return false;
3740 if (sup & power_state)
3741 return true;
3742 else
3743 return false;
3744}
3745
3746/*
Takashi Iwai432c6412012-08-28 09:59:20 -07003747 * wait until the state is reached, returns the current state
3748 */
3749static unsigned int hda_sync_power_state(struct hda_codec *codec,
3750 hda_nid_t fg,
3751 unsigned int power_state)
3752{
3753 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3754 unsigned int state, actual_state;
3755
3756 for (;;) {
3757 state = snd_hda_codec_read(codec, fg, 0,
3758 AC_VERB_GET_POWER_STATE, 0);
3759 if (state & AC_PWRST_ERROR)
3760 break;
3761 actual_state = (state >> 4) & 0x0f;
3762 if (actual_state == power_state)
3763 break;
3764 if (time_after_eq(jiffies, end_time))
3765 break;
3766 /* wait until the codec reachs to the target state */
3767 msleep(1);
3768 }
3769 return state;
3770}
3771
Takashi Iwai9419ab62013-01-24 17:23:35 +01003772/* don't power down the widget if it controls eapd and EAPD_BTLENABLE is set */
3773static unsigned int default_power_filter(struct hda_codec *codec, hda_nid_t nid,
3774 unsigned int power_state)
3775{
3776 if (power_state == AC_PWRST_D3 &&
3777 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
3778 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3779 int eapd = snd_hda_codec_read(codec, nid, 0,
3780 AC_VERB_GET_EAPD_BTLENABLE, 0);
3781 if (eapd & 0x02)
3782 return AC_PWRST_D0;
3783 }
3784 return power_state;
3785}
3786
Takashi Iwai432c6412012-08-28 09:59:20 -07003787/*
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003788 * set power state of the codec, and return the power state
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003789 */
Takashi Iwaid8193872012-08-31 07:54:38 -07003790static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003791 unsigned int power_state)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003792{
Takashi Iwaid8193872012-08-31 07:54:38 -07003793 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003794 int count;
3795 unsigned int state;
3796
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003797 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003798 if (power_state == AC_PWRST_D3) {
3799 /* transition time less than 10ms for power down */
Takashi Iwai983f6b92012-08-28 09:18:01 -07003800 msleep(codec->epss ? 10 : 100);
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003801 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08003802
3803 /* repeat power states setting at most 10 times*/
3804 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07003805 if (codec->patch_ops.set_power_state)
3806 codec->patch_ops.set_power_state(codec, fg,
3807 power_state);
3808 else {
3809 snd_hda_codec_read(codec, fg, 0,
3810 AC_VERB_SET_POWER_STATE,
3811 power_state);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003812 snd_hda_codec_set_power_to_all(codec, fg, power_state);
Takashi Iwai432c6412012-08-28 09:59:20 -07003813 }
3814 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08003815 if (!(state & AC_PWRST_ERROR))
3816 break;
3817 }
Mengdong Linb8dfc4622012-08-23 17:32:30 +08003818
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003819 return state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003820}
Takashi Iwai54d17402005-11-21 16:33:22 +01003821
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003822/* sync power states of all widgets;
3823 * this is called at the end of codec parsing
3824 */
3825static void sync_power_up_states(struct hda_codec *codec)
3826{
3827 hda_nid_t nid = codec->start_nid;
3828 int i;
3829
3830 /* don't care if no or standard filter is used */
3831 if (!codec->power_filter || codec->power_filter == default_power_filter)
3832 return;
3833
3834 for (i = 0; i < codec->num_nodes; i++, nid++) {
3835 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9040d102013-01-24 17:47:17 +01003836 unsigned int target;
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003837 if (!(wcaps & AC_WCAP_POWER))
3838 continue;
3839 target = codec->power_filter(codec, nid, AC_PWRST_D0);
3840 if (target == AC_PWRST_D0)
3841 continue;
Takashi Iwai9040d102013-01-24 17:47:17 +01003842 if (!snd_hda_check_power_state(codec, nid, target))
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003843 snd_hda_codec_write(codec, nid, 0,
3844 AC_VERB_SET_POWER_STATE, target);
3845 }
3846}
3847
Takashi Iwai11aeff02008-07-30 15:01:46 +02003848#ifdef CONFIG_SND_HDA_HWDEP
3849/* execute additional init verbs */
3850static void hda_exec_init_verbs(struct hda_codec *codec)
3851{
3852 if (codec->init_verbs.list)
3853 snd_hda_sequence_write(codec, codec->init_verbs.list);
3854}
3855#else
3856static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3857#endif
3858
Takashi Iwai2a439522011-07-26 09:52:50 +02003859#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003860/*
3861 * call suspend and power-down; used both from PM and power-save
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003862 * this function returns the power state in the end
Takashi Iwaicb53c622007-08-10 17:21:45 +02003863 */
Dylan Reidd17344b2012-09-28 15:57:01 -07003864static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003865{
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003866 unsigned int state;
3867
Takashi Iwai989c3182012-11-19 14:14:58 +01003868 codec->in_pm = 1;
3869
Takashi Iwaicb53c622007-08-10 17:21:45 +02003870 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02003871 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02003872 hda_cleanup_all_streams(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003873 state = hda_set_power_state(codec, AC_PWRST_D3);
Dylan Reidd17344b2012-09-28 15:57:01 -07003874 /* Cancel delayed work if we aren't currently running from it. */
3875 if (!in_wq)
3876 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003877 spin_lock(&codec->power_lock);
3878 snd_hda_update_power_acct(codec);
3879 trace_hda_power_down(codec);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003880 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003881 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003882 codec->power_jiffies = jiffies;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003883 spin_unlock(&codec->power_lock);
Takashi Iwai989c3182012-11-19 14:14:58 +01003884 codec->in_pm = 0;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003885 return state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003886}
3887
Takashi Iwaic370dd62012-12-13 18:30:04 +01003888/* mark all entries of cmd and amp caches dirty */
3889static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
3890{
3891 int i;
3892 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3893 struct hda_cache_head *cmd;
3894 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
3895 cmd->dirty = 1;
3896 }
3897 for (i = 0; i < codec->amp_cache.buf.used; i++) {
3898 struct hda_amp_info *amp;
David Henningssonf038fca2013-01-15 15:27:19 +01003899 amp = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003900 amp->head.dirty = 1;
3901 }
3902}
3903
Takashi Iwaicb53c622007-08-10 17:21:45 +02003904/*
3905 * kick up codec; used both from PM and power-save
3906 */
3907static void hda_call_codec_resume(struct hda_codec *codec)
3908{
Takashi Iwai989c3182012-11-19 14:14:58 +01003909 codec->in_pm = 1;
3910
Takashi Iwaic370dd62012-12-13 18:30:04 +01003911 hda_mark_cmd_cache_dirty(codec);
3912
Takashi Iwai7f308302012-05-08 16:52:23 +02003913 /* set as if powered on for avoiding re-entering the resume
3914 * in the resume / power-save sequence
3915 */
3916 hda_keep_power_on(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003917 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003918 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003919 hda_exec_init_verbs(codec);
Takashi Iwai31614bb2013-01-23 15:58:40 +01003920 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003921 if (codec->patch_ops.resume)
3922 codec->patch_ops.resume(codec);
3923 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003924 if (codec->patch_ops.init)
3925 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003926 snd_hda_codec_resume_amp(codec);
3927 snd_hda_codec_resume_cache(codec);
3928 }
David Henningsson26a6cb62012-10-09 15:04:21 +02003929
3930 if (codec->jackpoll_interval)
3931 hda_jackpoll_work(&codec->jackpoll_work.work);
Takashi Iwai31614bb2013-01-23 15:58:40 +01003932 else
David Henningsson26a6cb62012-10-09 15:04:21 +02003933 snd_hda_jack_report_sync(codec);
Takashi Iwai989c3182012-11-19 14:14:58 +01003934
3935 codec->in_pm = 0;
Takashi Iwai7f308302012-05-08 16:52:23 +02003936 snd_hda_power_down(codec); /* flag down before returning */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003937}
Takashi Iwai2a439522011-07-26 09:52:50 +02003938#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003939
Takashi Iwai54d17402005-11-21 16:33:22 +01003940
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941/**
3942 * snd_hda_build_controls - build mixer controls
3943 * @bus: the BUS
3944 *
3945 * Creates mixer controls for each codec included in the bus.
3946 *
3947 * Returns 0 if successful, otherwise a negative error code.
3948 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01003949int snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003951 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952
Takashi Iwai0ba21762007-04-16 11:29:14 +02003953 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003954 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003955 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01003956 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003957 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003958 err = snd_hda_codec_reset(codec);
3959 if (err < 0) {
3960 printk(KERN_ERR
3961 "hda_codec: cannot revert codec\n");
3962 return err;
3963 }
3964 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003966 return 0;
3967}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003968EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003970/*
3971 * add standard channel maps if not specified
3972 */
3973static int add_std_chmaps(struct hda_codec *codec)
3974{
3975 int i, str, err;
3976
3977 for (i = 0; i < codec->num_pcms; i++) {
3978 for (str = 0; str < 2; str++) {
3979 struct snd_pcm *pcm = codec->pcm_info[i].pcm;
3980 struct hda_pcm_stream *hinfo =
3981 &codec->pcm_info[i].stream[str];
3982 struct snd_pcm_chmap *chmap;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003983 const struct snd_pcm_chmap_elem *elem;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003984
3985 if (codec->pcm_info[i].own_chmap)
3986 continue;
3987 if (!pcm || !hinfo->substreams)
3988 continue;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003989 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
3990 err = snd_pcm_add_chmap_ctls(pcm, str, elem,
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003991 hinfo->channels_max,
3992 0, &chmap);
3993 if (err < 0)
3994 return err;
3995 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
3996 }
3997 }
3998 return 0;
3999}
4000
Takashi Iwaiee81abb2012-11-08 17:12:10 +01004001/* default channel maps for 2.1 speakers;
4002 * since HD-audio supports only stereo, odd number channels are omitted
4003 */
4004const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
4005 { .channels = 2,
4006 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
4007 { .channels = 4,
4008 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
4009 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
4010 { }
4011};
4012EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
4013
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004014int snd_hda_codec_build_controls(struct hda_codec *codec)
4015{
4016 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02004017 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004018 /* continue to initialize... */
4019 if (codec->patch_ops.init)
4020 err = codec->patch_ops.init(codec);
4021 if (!err && codec->patch_ops.build_controls)
4022 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004023 if (err < 0)
4024 return err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004025
4026 /* we create chmaps here instead of build_pcms */
4027 err = add_std_chmaps(codec);
4028 if (err < 0)
4029 return err;
4030
David Henningsson26a6cb62012-10-09 15:04:21 +02004031 if (codec->jackpoll_interval)
4032 hda_jackpoll_work(&codec->jackpoll_work.work);
4033 else
4034 snd_hda_jack_report_sync(codec); /* call at the last init point */
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004035 sync_power_up_states(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 return 0;
4037}
4038
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039/*
4040 * stream formats
4041 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02004042struct hda_rate_tbl {
4043 unsigned int hz;
4044 unsigned int alsa_bits;
4045 unsigned int hda_fmt;
4046};
4047
Takashi Iwai92f10b32010-08-03 14:21:00 +02004048/* rate = base * mult / div */
4049#define HDA_RATE(base, mult, div) \
4050 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
4051 (((div) - 1) << AC_FMT_DIV_SHIFT))
4052
Takashi Iwaibefdf312005-08-22 13:57:55 +02004053static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004055
4056 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004057 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
4058 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
4059 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
4060 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
4061 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
4062 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
4063 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
4064 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
4065 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
4066 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
4067 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004068#define AC_PAR_PCM_RATE_BITS 11
4069 /* up to bits 10, 384kHZ isn't supported properly */
4070
4071 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004072 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004073
Takashi Iwaibefdf312005-08-22 13:57:55 +02004074 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075};
4076
4077/**
4078 * snd_hda_calc_stream_format - calculate format bitset
4079 * @rate: the sample rate
4080 * @channels: the number of channels
4081 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
4082 * @maxbps: the max. bps
4083 *
4084 * Calculate the format bitset from the given rate, channels and th PCM format.
4085 *
4086 * Return zero if invalid.
4087 */
4088unsigned int snd_hda_calc_stream_format(unsigned int rate,
4089 unsigned int channels,
4090 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03004091 unsigned int maxbps,
4092 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093{
4094 int i;
4095 unsigned int val = 0;
4096
Takashi Iwaibefdf312005-08-22 13:57:55 +02004097 for (i = 0; rate_bits[i].hz; i++)
4098 if (rate_bits[i].hz == rate) {
4099 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 break;
4101 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02004102 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103 snd_printdd("invalid rate %d\n", rate);
4104 return 0;
4105 }
4106
4107 if (channels == 0 || channels > 8) {
4108 snd_printdd("invalid channels %d\n", channels);
4109 return 0;
4110 }
4111 val |= channels - 1;
4112
4113 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004114 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004115 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004116 break;
4117 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004118 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004119 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 case 20:
4121 case 24:
4122 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004123 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004124 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004126 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02004128 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 break;
4130 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004131 snd_printdd("invalid format width %d\n",
4132 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 return 0;
4134 }
4135
Anssi Hannula32c168c2010-08-03 13:28:57 +03004136 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004137 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03004138
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 return val;
4140}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004141EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004143static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
4144 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004145{
4146 unsigned int val = 0;
4147 if (nid != codec->afg &&
4148 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
4149 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
4150 if (!val || val == -1)
4151 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
4152 if (!val || val == -1)
4153 return 0;
4154 return val;
4155}
4156
4157static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
4158{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004159 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004160 get_pcm_param);
4161}
4162
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004163static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
4164 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004165{
4166 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
4167 if (!streams || streams == -1)
4168 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
4169 if (!streams || streams == -1)
4170 return 0;
4171 return streams;
4172}
4173
4174static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
4175{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004176 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004177 get_stream_param);
4178}
4179
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180/**
4181 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4182 * @codec: the HDA codec
4183 * @nid: NID to query
4184 * @ratesp: the pointer to store the detected rate bitflags
4185 * @formatsp: the pointer to store the detected formats
4186 * @bpsp: the pointer to store the detected format widths
4187 *
4188 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4189 * or @bsps argument is ignored.
4190 *
4191 * Returns 0 if successful, otherwise a negative error code.
4192 */
Stephen Warren384a48d2011-06-01 11:14:21 -06004193int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
4195{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004196 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004198 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004199 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200
4201 if (ratesp) {
4202 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004203 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02004205 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004207 if (rates == 0) {
4208 snd_printk(KERN_ERR "hda_codec: rates == 0 "
4209 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
4210 nid, val,
4211 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
4212 return -EIO;
4213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 *ratesp = rates;
4215 }
4216
4217 if (formatsp || bpsp) {
4218 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004219 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004221 streams = query_stream_param(codec, nid);
4222 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224
4225 bps = 0;
4226 if (streams & AC_SUPFMT_PCM) {
4227 if (val & AC_SUPPCM_BITS_8) {
4228 formats |= SNDRV_PCM_FMTBIT_U8;
4229 bps = 8;
4230 }
4231 if (val & AC_SUPPCM_BITS_16) {
4232 formats |= SNDRV_PCM_FMTBIT_S16_LE;
4233 bps = 16;
4234 }
4235 if (wcaps & AC_WCAP_DIGITAL) {
4236 if (val & AC_SUPPCM_BITS_32)
4237 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4238 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4239 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4240 if (val & AC_SUPPCM_BITS_24)
4241 bps = 24;
4242 else if (val & AC_SUPPCM_BITS_20)
4243 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004244 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4245 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4247 if (val & AC_SUPPCM_BITS_32)
4248 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249 else if (val & AC_SUPPCM_BITS_24)
4250 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02004251 else if (val & AC_SUPPCM_BITS_20)
4252 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 }
4254 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004255#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02004256 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004258 if (!bps)
4259 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02004260 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004261#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02004262 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02004263 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264 /* temporary hack: we have still no proper support
4265 * for the direct AC3 stream...
4266 */
4267 formats |= SNDRV_PCM_FMTBIT_U8;
4268 bps = 8;
4269 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004270 if (formats == 0) {
4271 snd_printk(KERN_ERR "hda_codec: formats == 0 "
4272 "(nid=0x%x, val=0x%x, ovrd=%i, "
4273 "streams=0x%x)\n",
4274 nid, val,
4275 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4276 streams);
4277 return -EIO;
4278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 if (formatsp)
4280 *formatsp = formats;
4281 if (bpsp)
4282 *bpsp = bps;
4283 }
4284
4285 return 0;
4286}
Stephen Warren384a48d2011-06-01 11:14:21 -06004287EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288
4289/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004290 * snd_hda_is_supported_format - Check the validity of the format
4291 * @codec: HD-audio codec
4292 * @nid: NID to check
4293 * @format: the HD-audio format value to check
4294 *
4295 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 *
4297 * Returns 1 if supported, 0 if not.
4298 */
4299int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4300 unsigned int format)
4301{
4302 int i;
4303 unsigned int val = 0, rate, stream;
4304
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004305 val = query_pcm_param(codec, nid);
4306 if (!val)
4307 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308
4309 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004310 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02004311 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 if (val & (1 << i))
4313 break;
4314 return 0;
4315 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004316 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 return 0;
4318
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004319 stream = query_stream_param(codec, nid);
4320 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 return 0;
4322
4323 if (stream & AC_SUPFMT_PCM) {
4324 switch (format & 0xf0) {
4325 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004326 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 return 0;
4328 break;
4329 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004330 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 return 0;
4332 break;
4333 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004334 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 return 0;
4336 break;
4337 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004338 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 return 0;
4340 break;
4341 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004342 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 return 0;
4344 break;
4345 default:
4346 return 0;
4347 }
4348 } else {
4349 /* FIXME: check for float32 and AC3? */
4350 }
4351
4352 return 1;
4353}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004354EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355
4356/*
4357 * PCM stuff
4358 */
4359static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4360 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004361 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362{
4363 return 0;
4364}
4365
4366static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4367 struct hda_codec *codec,
4368 unsigned int stream_tag,
4369 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004370 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371{
4372 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4373 return 0;
4374}
4375
4376static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4377 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004378 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379{
Takashi Iwai888afa12008-03-18 09:57:50 +01004380 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381 return 0;
4382}
4383
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004384static int set_pcm_default_values(struct hda_codec *codec,
4385 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004387 int err;
4388
Takashi Iwai0ba21762007-04-16 11:29:14 +02004389 /* query support PCM information from the given NID */
4390 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004391 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02004392 info->rates ? NULL : &info->rates,
4393 info->formats ? NULL : &info->formats,
4394 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004395 if (err < 0)
4396 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 }
4398 if (info->ops.open == NULL)
4399 info->ops.open = hda_pcm_default_open_close;
4400 if (info->ops.close == NULL)
4401 info->ops.close = hda_pcm_default_open_close;
4402 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004403 if (snd_BUG_ON(!info->nid))
4404 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 info->ops.prepare = hda_pcm_default_prepare;
4406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004408 if (snd_BUG_ON(!info->nid))
4409 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 info->ops.cleanup = hda_pcm_default_cleanup;
4411 }
4412 return 0;
4413}
4414
Takashi Iwaieb541332010-08-06 13:48:11 +02004415/*
4416 * codec prepare/cleanup entries
4417 */
4418int snd_hda_codec_prepare(struct hda_codec *codec,
4419 struct hda_pcm_stream *hinfo,
4420 unsigned int stream,
4421 unsigned int format,
4422 struct snd_pcm_substream *substream)
4423{
4424 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004425 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004426 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4427 if (ret >= 0)
4428 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004429 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004430 return ret;
4431}
4432EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
4433
4434void snd_hda_codec_cleanup(struct hda_codec *codec,
4435 struct hda_pcm_stream *hinfo,
4436 struct snd_pcm_substream *substream)
4437{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004438 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004439 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004440 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004441}
4442EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
4443
Takashi Iwaid5191e52009-11-16 14:58:17 +01004444/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01004445const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4446 "Audio", "SPDIF", "HDMI", "Modem"
4447};
4448
Takashi Iwai176d5332008-07-30 15:01:44 +02004449/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004450 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01004451 *
4452 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004453 */
4454static int get_empty_pcm_device(struct hda_bus *bus, int type)
4455{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004456 /* audio device indices; not linear to keep compatibility */
4457 static int audio_idx[HDA_PCM_NTYPES][5] = {
4458 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4459 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01004460 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004461 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004462 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004463 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004464
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004465 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004466 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
4467 return -EINVAL;
4468 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004469
4470 for (i = 0; audio_idx[type][i] >= 0 ; i++)
4471 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4472 return audio_idx[type][i];
4473
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004474 /* non-fixed slots starting from 10 */
4475 for (i = 10; i < 32; i++) {
4476 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4477 return i;
4478 }
4479
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004480 snd_printk(KERN_WARNING "Too many %s devices\n",
4481 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004482 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004483}
4484
4485/*
Takashi Iwai176d5332008-07-30 15:01:44 +02004486 * attach a new PCM stream
4487 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004488static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02004489{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004490 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02004491 struct hda_pcm_stream *info;
4492 int stream, err;
4493
Takashi Iwaib91f0802008-11-04 08:43:08 +01004494 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02004495 return -EINVAL;
4496 for (stream = 0; stream < 2; stream++) {
4497 info = &pcm->stream[stream];
4498 if (info->substreams) {
4499 err = set_pcm_default_values(codec, info);
4500 if (err < 0)
4501 return err;
4502 }
4503 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004504 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02004505}
4506
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004507/* assign all PCMs of the given codec */
4508int snd_hda_codec_build_pcms(struct hda_codec *codec)
4509{
4510 unsigned int pcm;
4511 int err;
4512
4513 if (!codec->num_pcms) {
4514 if (!codec->patch_ops.build_pcms)
4515 return 0;
4516 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004517 if (err < 0) {
4518 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004519 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004520 err = snd_hda_codec_reset(codec);
4521 if (err < 0) {
4522 printk(KERN_ERR
4523 "hda_codec: cannot revert codec\n");
4524 return err;
4525 }
4526 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004527 }
4528 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4529 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4530 int dev;
4531
4532 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004533 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004534
4535 if (!cpcm->pcm) {
4536 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4537 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004538 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004539 cpcm->device = dev;
4540 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004541 if (err < 0) {
4542 printk(KERN_ERR "hda_codec: cannot attach "
4543 "PCM stream %d for codec #%d\n",
4544 dev, codec->addr);
4545 continue; /* no fatal error */
4546 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004547 }
4548 }
4549 return 0;
4550}
4551
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552/**
4553 * snd_hda_build_pcms - build PCM information
4554 * @bus: the BUS
4555 *
4556 * Create PCM information for each codec included in the bus.
4557 *
4558 * The build_pcms codec patch is requested to set up codec->num_pcms and
4559 * codec->pcm_info properly. The array is referred by the top-level driver
4560 * to create its PCM instances.
4561 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4562 * callback.
4563 *
4564 * At least, substreams, channels_min and channels_max must be filled for
4565 * each stream. substreams = 0 indicates that the stream doesn't exist.
4566 * When rates and/or formats are zero, the supported values are queried
4567 * from the given nid. The nid is used also by the default ops.prepare
4568 * and ops.cleanup callbacks.
4569 *
4570 * The driver needs to call ops.open in its open callback. Similarly,
4571 * ops.close is supposed to be called in the close callback.
4572 * ops.prepare should be called in the prepare or hw_params callback
4573 * with the proper parameters for set up.
4574 * ops.cleanup should be called in hw_free for clean up of streams.
4575 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004576 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 */
Takashi Iwai5cb543d2012-08-09 13:49:23 +02004578int snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004580 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581
Takashi Iwai0ba21762007-04-16 11:29:14 +02004582 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004583 int err = snd_hda_codec_build_pcms(codec);
4584 if (err < 0)
4585 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 }
4587 return 0;
4588}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004589EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591/**
4592 * snd_hda_check_board_config - compare the current codec with the config table
4593 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004594 * @num_configs: number of config enums
4595 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 * @tbl: configuration table, terminated by null entries
4597 *
4598 * Compares the modelname or PCI subsystem id of the current codec with the
4599 * given configuration table. If a matching entry is found, returns its
4600 * config value (supposed to be 0 or positive).
4601 *
4602 * If no entries are matching, the function returns a negative value.
4603 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004604int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004605 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02004606 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607{
Takashi Iwaif44ac832008-07-30 15:01:45 +02004608 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004609 int i;
4610 for (i = 0; i < num_configs; i++) {
4611 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02004612 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004613 snd_printd(KERN_INFO "hda_codec: model '%s' is "
4614 "selected\n", models[i]);
4615 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616 }
4617 }
4618 }
4619
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004620 if (!codec->bus->pci || !tbl)
4621 return -1;
4622
4623 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
4624 if (!tbl)
4625 return -1;
4626 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02004627#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004628 char tmp[10];
4629 const char *model = NULL;
4630 if (models)
4631 model = models[tbl->value];
4632 if (!model) {
4633 sprintf(tmp, "#%d", tbl->value);
4634 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004636 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4637 "for config %x:%x (%s)\n",
4638 model, tbl->subvendor, tbl->subdevice,
4639 (tbl->name ? tbl->name : "Unknown device"));
4640#endif
4641 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 }
4643 return -1;
4644}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004645EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646
4647/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004648 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004649 subsystem ID with the
4650 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004651
4652 This is important for Gateway notebooks with SB450 HDA Audio
4653 where the vendor ID of the PCI device is:
4654 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4655 and the vendor/subvendor are found only at the codec.
4656
4657 * @codec: the HDA codec
4658 * @num_configs: number of config enums
4659 * @models: array of model name strings
4660 * @tbl: configuration table, terminated by null entries
4661 *
4662 * Compares the modelname or PCI subsystem id of the current codec with the
4663 * given configuration table. If a matching entry is found, returns its
4664 * config value (supposed to be 0 or positive).
4665 *
4666 * If no entries are matching, the function returns a negative value.
4667 */
4668int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004669 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004670 const struct snd_pci_quirk *tbl)
4671{
4672 const struct snd_pci_quirk *q;
4673
4674 /* Search for codec ID */
4675 for (q = tbl; q->subvendor; q++) {
Takashi Iwaie2301a42011-11-22 19:58:56 +01004676 unsigned int mask = 0xffff0000 | q->subdevice_mask;
4677 unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
4678 if ((codec->subsystem_id & mask) == id)
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004679 break;
4680 }
4681
4682 if (!q->subvendor)
4683 return -1;
4684
4685 tbl = q;
4686
4687 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02004688#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004689 char tmp[10];
4690 const char *model = NULL;
4691 if (models)
4692 model = models[tbl->value];
4693 if (!model) {
4694 sprintf(tmp, "#%d", tbl->value);
4695 model = tmp;
4696 }
4697 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4698 "for config %x:%x (%s)\n",
4699 model, tbl->subvendor, tbl->subdevice,
4700 (tbl->name ? tbl->name : "Unknown device"));
4701#endif
4702 return tbl->value;
4703 }
4704 return -1;
4705}
4706EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4707
4708/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709 * snd_hda_add_new_ctls - create controls from the array
4710 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004711 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 *
4713 * This helper function creates and add new controls in the given array.
4714 * The array must be terminated with an empty entry as terminator.
4715 *
4716 * Returns 0 if successful, or a negative error code.
4717 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004718int snd_hda_add_new_ctls(struct hda_codec *codec,
4719 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004721 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722
4723 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004724 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004725 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004726 if (knew->iface == -1) /* skip this codec private value */
4727 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004728 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004729 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004730 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004731 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004732 if (addr > 0)
4733 kctl->id.device = addr;
4734 if (idx > 0)
4735 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004736 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004737 if (!err)
4738 break;
4739 /* try first with another device index corresponding to
4740 * the codec addr; if it still fails (or it's the
4741 * primary codec), then try another control index
4742 */
4743 if (!addr && codec->addr)
4744 addr = codec->addr;
4745 else if (!idx && !knew->index) {
4746 idx = find_empty_mixer_ctl_idx(codec,
Takashi Iwaidcda5802012-10-12 17:24:51 +02004747 knew->name, 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004748 if (idx <= 0)
4749 return err;
4750 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004751 return err;
4752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753 }
4754 return 0;
4755}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004756EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757
Takashi Iwai83012a72012-08-24 18:38:08 +02004758#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02004759static void hda_power_work(struct work_struct *work)
4760{
4761 struct hda_codec *codec =
4762 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004763 struct hda_bus *bus = codec->bus;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004764 unsigned int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004765
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004766 spin_lock(&codec->power_lock);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004767 if (codec->power_transition > 0) { /* during power-up sequence? */
4768 spin_unlock(&codec->power_lock);
4769 return;
4770 }
Maxim Levitsky2e492462007-09-03 15:26:57 +02004771 if (!codec->power_on || codec->power_count) {
4772 codec->power_transition = 0;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004773 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004774 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02004775 }
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004776 spin_unlock(&codec->power_lock);
4777
Dylan Reidd17344b2012-09-28 15:57:01 -07004778 state = hda_call_codec_suspend(codec, true);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004779 codec->pm_down_notified = 0;
4780 if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK)) {
4781 codec->pm_down_notified = 1;
Takashi Iwai68467f52012-08-28 09:14:29 -07004782 hda_call_pm_notify(bus, false);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004783 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004784}
4785
4786static void hda_keep_power_on(struct hda_codec *codec)
4787{
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004788 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004789 codec->power_count++;
4790 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004791 codec->power_jiffies = jiffies;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004792 spin_unlock(&codec->power_lock);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004793}
4794
Takashi Iwaid5191e52009-11-16 14:58:17 +01004795/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01004796void snd_hda_update_power_acct(struct hda_codec *codec)
4797{
4798 unsigned long delta = jiffies - codec->power_jiffies;
4799 if (codec->power_on)
4800 codec->power_on_acct += delta;
4801 else
4802 codec->power_off_acct += delta;
4803 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004804}
4805
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004806/* Transition to powered up, if wait_power_down then wait for a pending
4807 * transition to D3 to complete. A pending D3 transition is indicated
4808 * with power_transition == -1. */
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004809/* call this with codec->power_lock held! */
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004810static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004811{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004812 struct hda_bus *bus = codec->bus;
4813
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004814 /* Return if power_on or transitioning to power_on, unless currently
4815 * powering down. */
4816 if ((codec->power_on || codec->power_transition > 0) &&
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004817 !(wait_power_down && codec->power_transition < 0))
Takashi Iwaicb53c622007-08-10 17:21:45 +02004818 return;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004819 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004820
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004821 cancel_delayed_work_sync(&codec->power_work);
4822
4823 spin_lock(&codec->power_lock);
Dylan Reidb43d2242012-06-21 21:51:22 -07004824 /* If the power down delayed work was cancelled above before starting,
4825 * then there is no need to go through power up here.
4826 */
4827 if (codec->power_on) {
Takashi Iwai535b6c52012-08-20 21:25:22 +02004828 if (codec->power_transition < 0)
4829 codec->power_transition = 0;
Dylan Reidb43d2242012-06-21 21:51:22 -07004830 return;
4831 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004832
Takashi Iwaid66fee52011-08-02 15:39:31 +02004833 trace_hda_power_up(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004834 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004835 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004836 codec->power_jiffies = jiffies;
Takashi Iwai7f308302012-05-08 16:52:23 +02004837 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004838 spin_unlock(&codec->power_lock);
4839
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004840 if (codec->pm_down_notified) {
4841 codec->pm_down_notified = 0;
Takashi Iwai68467f52012-08-28 09:14:29 -07004842 hda_call_pm_notify(bus, true);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004843 }
4844
Takashi Iwaicb53c622007-08-10 17:21:45 +02004845 hda_call_codec_resume(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004846
4847 spin_lock(&codec->power_lock);
Takashi Iwaia221e282007-08-16 16:35:33 +02004848 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004849}
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004850
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004851#define power_save(codec) \
4852 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004853
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004854/* Transition to powered down */
4855static void __snd_hda_power_down(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004856{
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004857 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004858 return;
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004859
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004860 if (power_save(codec)) {
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004861 codec->power_transition = -1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01004862 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004863 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02004864 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004865}
4866
4867/**
4868 * snd_hda_power_save - Power-up/down/sync the codec
4869 * @codec: HD-audio codec
4870 * @delta: the counter delta to change
4871 *
4872 * Change the power-up counter via @delta, and power up or down the hardware
4873 * appropriately. For the power-down, queue to the delayed action.
4874 * Passing zero to @delta means to synchronize the power state.
4875 */
4876void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
4877{
4878 spin_lock(&codec->power_lock);
4879 codec->power_count += delta;
4880 trace_hda_power_count(codec);
4881 if (delta > 0)
4882 __snd_hda_power_up(codec, d3wait);
4883 else
4884 __snd_hda_power_down(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004885 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004886}
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004887EXPORT_SYMBOL_HDA(snd_hda_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004888
Takashi Iwaid5191e52009-11-16 14:58:17 +01004889/**
4890 * snd_hda_check_amp_list_power - Check the amp list and update the power
4891 * @codec: HD-audio codec
4892 * @check: the object containing an AMP list and the status
4893 * @nid: NID to check / update
4894 *
4895 * Check whether the given NID is in the amp list. If it's in the list,
4896 * check the current AMP status, and update the the power-status according
4897 * to the mute status.
4898 *
4899 * This function is supposed to be set or called from the check_power_status
4900 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004901 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004902int snd_hda_check_amp_list_power(struct hda_codec *codec,
4903 struct hda_loopback_check *check,
4904 hda_nid_t nid)
4905{
Takashi Iwai031024e2011-05-02 11:29:30 +02004906 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004907 int ch, v;
4908
4909 if (!check->amplist)
4910 return 0;
4911 for (p = check->amplist; p->nid; p++) {
4912 if (p->nid == nid)
4913 break;
4914 }
4915 if (!p->nid)
4916 return 0; /* nothing changed */
4917
4918 for (p = check->amplist; p->nid; p++) {
4919 for (ch = 0; ch < 2; ch++) {
4920 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4921 p->idx);
4922 if (!(v & HDA_AMP_MUTE) && v > 0) {
4923 if (!check->power_on) {
4924 check->power_on = 1;
4925 snd_hda_power_up(codec);
4926 }
4927 return 1;
4928 }
4929 }
4930 }
4931 if (check->power_on) {
4932 check->power_on = 0;
4933 snd_hda_power_down(codec);
4934 }
4935 return 0;
4936}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004937EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004938#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004940/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004941 * Channel mode helper
4942 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004943
4944/**
4945 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4946 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004947int snd_hda_ch_mode_info(struct hda_codec *codec,
4948 struct snd_ctl_elem_info *uinfo,
4949 const struct hda_channel_mode *chmode,
4950 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004951{
4952 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4953 uinfo->count = 1;
4954 uinfo->value.enumerated.items = num_chmodes;
4955 if (uinfo->value.enumerated.item >= num_chmodes)
4956 uinfo->value.enumerated.item = num_chmodes - 1;
4957 sprintf(uinfo->value.enumerated.name, "%dch",
4958 chmode[uinfo->value.enumerated.item].channels);
4959 return 0;
4960}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004961EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004962
Takashi Iwaid5191e52009-11-16 14:58:17 +01004963/**
4964 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4965 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004966int snd_hda_ch_mode_get(struct hda_codec *codec,
4967 struct snd_ctl_elem_value *ucontrol,
4968 const struct hda_channel_mode *chmode,
4969 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004970 int max_channels)
4971{
4972 int i;
4973
4974 for (i = 0; i < num_chmodes; i++) {
4975 if (max_channels == chmode[i].channels) {
4976 ucontrol->value.enumerated.item[0] = i;
4977 break;
4978 }
4979 }
4980 return 0;
4981}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004982EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004983
Takashi Iwaid5191e52009-11-16 14:58:17 +01004984/**
4985 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4986 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004987int snd_hda_ch_mode_put(struct hda_codec *codec,
4988 struct snd_ctl_elem_value *ucontrol,
4989 const struct hda_channel_mode *chmode,
4990 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004991 int *max_channelsp)
4992{
4993 unsigned int mode;
4994
4995 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01004996 if (mode >= num_chmodes)
4997 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004998 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004999 return 0;
5000 /* change the current channel setting */
5001 *max_channelsp = chmode[mode].channels;
5002 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005003 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005004 return 1;
5005}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005006EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01005007
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008/*
5009 * input MUX helper
5010 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01005011
5012/**
5013 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
5014 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005015int snd_hda_input_mux_info(const struct hda_input_mux *imux,
5016 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017{
5018 unsigned int index;
5019
5020 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5021 uinfo->count = 1;
5022 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02005023 if (!imux->num_items)
5024 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025 index = uinfo->value.enumerated.item;
5026 if (index >= imux->num_items)
5027 index = imux->num_items - 1;
5028 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
5029 return 0;
5030}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005031EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032
Takashi Iwaid5191e52009-11-16 14:58:17 +01005033/**
5034 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
5035 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005036int snd_hda_input_mux_put(struct hda_codec *codec,
5037 const struct hda_input_mux *imux,
5038 struct snd_ctl_elem_value *ucontrol,
5039 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040 unsigned int *cur_val)
5041{
5042 unsigned int idx;
5043
Takashi Iwai5513b0c2007-10-09 11:58:41 +02005044 if (!imux->num_items)
5045 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046 idx = ucontrol->value.enumerated.item[0];
5047 if (idx >= imux->num_items)
5048 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005049 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005051 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
5052 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053 *cur_val = idx;
5054 return 1;
5055}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005056EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057
5058
5059/*
Takashi Iwaidda415d2012-11-30 18:34:38 +01005060 * process kcontrol info callback of a simple string enum array
5061 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
5062 */
5063int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
5064 struct snd_ctl_elem_info *uinfo,
5065 int num_items, const char * const *texts)
5066{
5067 static const char * const texts_default[] = {
5068 "Disabled", "Enabled"
5069 };
5070
5071 if (!texts || !num_items) {
5072 num_items = 2;
5073 texts = texts_default;
5074 }
5075
5076 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5077 uinfo->count = 1;
5078 uinfo->value.enumerated.items = num_items;
5079 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
5080 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
5081 strcpy(uinfo->value.enumerated.name,
5082 texts[uinfo->value.enumerated.item]);
5083 return 0;
5084}
5085EXPORT_SYMBOL_HDA(snd_hda_enum_helper_info);
5086
5087/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088 * Multi-channel / digital-out PCM helper functions
5089 */
5090
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005091/* setup SPDIF output stream */
5092static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
5093 unsigned int stream_tag, unsigned int format)
5094{
Laurence Darby3bef1c32012-11-03 17:00:06 +00005095 struct hda_spdif_out *spdif;
5096 unsigned int curr_fmt;
5097 bool reset;
Stephen Warren7c9359762011-06-01 11:14:17 -06005098
Laurence Darby3bef1c32012-11-03 17:00:06 +00005099 spdif = snd_hda_spdif_out_of_nid(codec, nid);
5100 curr_fmt = snd_hda_codec_read(codec, nid, 0,
5101 AC_VERB_GET_STREAM_FORMAT, 0);
5102 reset = codec->spdif_status_reset &&
5103 (spdif->ctls & AC_DIG1_ENABLE) &&
5104 curr_fmt != format;
5105
5106 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
5107 updated */
5108 if (reset)
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005109 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06005110 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02005111 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005112 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02005113 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02005114 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02005115 for (d = codec->slave_dig_outs; *d; d++)
5116 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
5117 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005118 }
Takashi Iwai2f728532008-09-25 16:32:41 +02005119 /* turn on again (if needed) */
Laurence Darby3bef1c32012-11-03 17:00:06 +00005120 if (reset)
Takashi Iwai2f728532008-09-25 16:32:41 +02005121 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06005122 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02005123}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005124
Takashi Iwai2f728532008-09-25 16:32:41 +02005125static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
5126{
5127 snd_hda_codec_cleanup_stream(codec, nid);
5128 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02005129 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02005130 for (d = codec->slave_dig_outs; *d; d++)
5131 snd_hda_codec_cleanup_stream(codec, *d);
5132 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005133}
5134
Takashi Iwaid5191e52009-11-16 14:58:17 +01005135/**
5136 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
5137 * @bus: HD-audio bus
5138 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005139void snd_hda_bus_reboot_notify(struct hda_bus *bus)
5140{
5141 struct hda_codec *codec;
5142
5143 if (!bus)
5144 return;
5145 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005146 if (hda_codec_is_power_on(codec) &&
5147 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005148 codec->patch_ops.reboot_notify(codec);
5149 }
5150}
Takashi Iwai8f217a22009-11-10 18:26:12 +01005151EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005152
Takashi Iwaid5191e52009-11-16 14:58:17 +01005153/**
5154 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005156int snd_hda_multi_out_dig_open(struct hda_codec *codec,
5157 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158{
Ingo Molnar62932df2006-01-16 16:34:20 +01005159 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02005160 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
5161 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02005162 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01005164 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165 return 0;
5166}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005167EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168
Takashi Iwaid5191e52009-11-16 14:58:17 +01005169/**
5170 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
5171 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005172int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
5173 struct hda_multi_out *mout,
5174 unsigned int stream_tag,
5175 unsigned int format,
5176 struct snd_pcm_substream *substream)
5177{
5178 mutex_lock(&codec->spdif_mutex);
5179 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
5180 mutex_unlock(&codec->spdif_mutex);
5181 return 0;
5182}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005183EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005184
Takashi Iwaid5191e52009-11-16 14:58:17 +01005185/**
5186 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
5187 */
Takashi Iwai9411e212009-02-13 11:32:28 +01005188int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
5189 struct hda_multi_out *mout)
5190{
5191 mutex_lock(&codec->spdif_mutex);
5192 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5193 mutex_unlock(&codec->spdif_mutex);
5194 return 0;
5195}
5196EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
5197
Takashi Iwaid5191e52009-11-16 14:58:17 +01005198/**
5199 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005201int snd_hda_multi_out_dig_close(struct hda_codec *codec,
5202 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203{
Ingo Molnar62932df2006-01-16 16:34:20 +01005204 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01005206 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207 return 0;
5208}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005209EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210
Takashi Iwaid5191e52009-11-16 14:58:17 +01005211/**
5212 * snd_hda_multi_out_analog_open - open analog outputs
5213 *
5214 * Open analog outputs and set up the hw-constraints.
5215 * If the digital outputs can be opened as slave, open the digital
5216 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005218int snd_hda_multi_out_analog_open(struct hda_codec *codec,
5219 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01005220 struct snd_pcm_substream *substream,
5221 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222{
Takashi Iwai9a081602008-02-12 18:37:26 +01005223 struct snd_pcm_runtime *runtime = substream->runtime;
5224 runtime->hw.channels_max = mout->max_channels;
5225 if (mout->dig_out_nid) {
5226 if (!mout->analog_rates) {
5227 mout->analog_rates = hinfo->rates;
5228 mout->analog_formats = hinfo->formats;
5229 mout->analog_maxbps = hinfo->maxbps;
5230 } else {
5231 runtime->hw.rates = mout->analog_rates;
5232 runtime->hw.formats = mout->analog_formats;
5233 hinfo->maxbps = mout->analog_maxbps;
5234 }
5235 if (!mout->spdif_rates) {
5236 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
5237 &mout->spdif_rates,
5238 &mout->spdif_formats,
5239 &mout->spdif_maxbps);
5240 }
5241 mutex_lock(&codec->spdif_mutex);
5242 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02005243 if ((runtime->hw.rates & mout->spdif_rates) &&
5244 (runtime->hw.formats & mout->spdif_formats)) {
5245 runtime->hw.rates &= mout->spdif_rates;
5246 runtime->hw.formats &= mout->spdif_formats;
5247 if (mout->spdif_maxbps < hinfo->maxbps)
5248 hinfo->maxbps = mout->spdif_maxbps;
5249 } else {
5250 mout->share_spdif = 0;
5251 /* FIXME: need notify? */
5252 }
Takashi Iwai9a081602008-02-12 18:37:26 +01005253 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02005254 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01005255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256 return snd_pcm_hw_constraint_step(substream->runtime, 0,
5257 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
5258}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005259EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260
Takashi Iwaid5191e52009-11-16 14:58:17 +01005261/**
5262 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
5263 *
5264 * Set up the i/o for analog out.
5265 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005267int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5268 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269 unsigned int stream_tag,
5270 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01005271 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005272{
Takashi Iwaidda14412011-05-02 11:29:30 +02005273 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005275 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276 int i;
5277
Ingo Molnar62932df2006-01-16 16:34:20 +01005278 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005279 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01005280 if (mout->dig_out_nid && mout->share_spdif &&
5281 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02005283 snd_hda_is_supported_format(codec, mout->dig_out_nid,
5284 format) &&
Stephen Warren7c9359762011-06-01 11:14:17 -06005285 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005287 setup_dig_out_stream(codec, mout->dig_out_nid,
5288 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289 } else {
5290 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02005291 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292 }
5293 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005294 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295
5296 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005297 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5298 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005299 if (!mout->no_share_stream &&
5300 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005302 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5303 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005304 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005305 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5306 if (!mout->no_share_stream && mout->hp_out_nid[i])
5307 snd_hda_codec_setup_stream(codec,
5308 mout->hp_out_nid[i],
5309 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005310 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02005311 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01005312 snd_hda_codec_setup_stream(codec,
5313 mout->extra_out_nid[i],
5314 stream_tag, 0, format);
5315
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 /* surrounds */
5317 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02005318 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005319 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5320 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005321 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005322 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5323 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324 }
5325 return 0;
5326}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005327EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328
Takashi Iwaid5191e52009-11-16 14:58:17 +01005329/**
5330 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005332int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5333 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334{
Takashi Iwaidda14412011-05-02 11:29:30 +02005335 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336 int i;
5337
5338 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01005339 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01005341 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005342 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5343 if (mout->hp_out_nid[i])
5344 snd_hda_codec_cleanup_stream(codec,
5345 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005346 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5347 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01005348 snd_hda_codec_cleanup_stream(codec,
5349 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01005350 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02005352 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353 mout->dig_out_used = 0;
5354 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005355 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356 return 0;
5357}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005358EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359
Takashi Iwai47408602012-04-20 13:06:53 +02005360/**
5361 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
5362 *
5363 * Guess the suitable VREF pin bits to be set as the pin-control value.
5364 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5365 */
5366unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5367{
5368 unsigned int pincap;
5369 unsigned int oldval;
5370 oldval = snd_hda_codec_read(codec, pin, 0,
5371 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5372 pincap = snd_hda_query_pin_caps(codec, pin);
5373 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5374 /* Exception: if the default pin setup is vref50, we give it priority */
5375 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5376 return AC_PINCTL_VREF_80;
5377 else if (pincap & AC_PINCAP_VREF_50)
5378 return AC_PINCTL_VREF_50;
5379 else if (pincap & AC_PINCAP_VREF_100)
5380 return AC_PINCTL_VREF_100;
5381 else if (pincap & AC_PINCAP_VREF_GRD)
5382 return AC_PINCTL_VREF_GRD;
5383 return AC_PINCTL_VREF_HIZ;
5384}
5385EXPORT_SYMBOL_HDA(snd_hda_get_default_vref);
5386
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005387/* correct the pin ctl value for matching with the pin cap */
5388unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
5389 hda_nid_t pin, unsigned int val)
5390{
5391 static unsigned int cap_lists[][2] = {
5392 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
5393 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
5394 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
5395 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
5396 };
5397 unsigned int cap;
5398
5399 if (!val)
5400 return 0;
5401 cap = snd_hda_query_pin_caps(codec, pin);
5402 if (!cap)
5403 return val; /* don't know what to do... */
5404
5405 if (val & AC_PINCTL_OUT_EN) {
5406 if (!(cap & AC_PINCAP_OUT))
5407 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5408 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
5409 val &= ~AC_PINCTL_HP_EN;
5410 }
5411
5412 if (val & AC_PINCTL_IN_EN) {
5413 if (!(cap & AC_PINCAP_IN))
5414 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5415 else {
5416 unsigned int vcap, vref;
5417 int i;
5418 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5419 vref = val & AC_PINCTL_VREFEN;
5420 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
5421 if (vref == cap_lists[i][0] &&
5422 !(vcap & cap_lists[i][1])) {
5423 if (i == ARRAY_SIZE(cap_lists) - 1)
5424 vref = AC_PINCTL_VREF_HIZ;
5425 else
5426 vref = cap_lists[i + 1][0];
5427 }
5428 }
5429 val &= ~AC_PINCTL_VREFEN;
5430 val |= vref;
5431 }
5432 }
5433
5434 return val;
5435}
5436EXPORT_SYMBOL_HDA(snd_hda_correct_pin_ctl);
5437
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005438int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5439 unsigned int val, bool cached)
5440{
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005441 val = snd_hda_correct_pin_ctl(codec, pin, val);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01005442 snd_hda_codec_set_pin_target(codec, pin, val);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005443 if (cached)
5444 return snd_hda_codec_update_cache(codec, pin, 0,
5445 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5446 else
5447 return snd_hda_codec_write(codec, pin, 0,
5448 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5449}
5450EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl);
5451
Takashi Iwai990061c2010-09-09 22:08:44 +02005452/**
5453 * snd_hda_add_imux_item - Add an item to input_mux
5454 *
5455 * When the same label is used already in the existing items, the number
5456 * suffix is appended to the label. This label index number is stored
5457 * to type_idx when non-NULL pointer is given.
5458 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005459int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5460 int index, int *type_idx)
5461{
5462 int i, label_idx = 0;
5463 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5464 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5465 return -EINVAL;
5466 }
5467 for (i = 0; i < imux->num_items; i++) {
5468 if (!strncmp(label, imux->items[i].label, strlen(label)))
5469 label_idx++;
5470 }
5471 if (type_idx)
5472 *type_idx = label_idx;
5473 if (label_idx > 0)
5474 snprintf(imux->items[imux->num_items].label,
5475 sizeof(imux->items[imux->num_items].label),
5476 "%s %d", label, label_idx);
5477 else
5478 strlcpy(imux->items[imux->num_items].label, label,
5479 sizeof(imux->items[imux->num_items].label));
5480 imux->items[imux->num_items].index = index;
5481 imux->num_items++;
5482 return 0;
5483}
5484EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005485
Takashi Iwai4a471b72005-12-07 13:56:29 +01005486
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487#ifdef CONFIG_PM
5488/*
5489 * power management
5490 */
5491
5492/**
5493 * snd_hda_suspend - suspend the codecs
5494 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495 *
5496 * Returns 0 if successful.
5497 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005498int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005500 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501
Takashi Iwai0ba21762007-04-16 11:29:14 +02005502 list_for_each_entry(codec, &bus->codec_list, list) {
David Henningsson26a6cb62012-10-09 15:04:21 +02005503 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005504 if (hda_codec_is_power_on(codec))
Dylan Reidd17344b2012-09-28 15:57:01 -07005505 hda_call_codec_suspend(codec, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506 }
5507 return 0;
5508}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005509EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510
5511/**
5512 * snd_hda_resume - resume the codecs
5513 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 *
5515 * Returns 0 if successful.
5516 */
5517int snd_hda_resume(struct hda_bus *bus)
5518{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005519 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520
Takashi Iwai0ba21762007-04-16 11:29:14 +02005521 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai7f308302012-05-08 16:52:23 +02005522 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524 return 0;
5525}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005526EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005527#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005528
5529/*
5530 * generic arrays
5531 */
5532
Takashi Iwaid5191e52009-11-16 14:58:17 +01005533/**
5534 * snd_array_new - get a new element from the given array
5535 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005536 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005537 * Get a new element from the given array. If it exceeds the
5538 * pre-allocated array size, re-allocate the array.
5539 *
5540 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005541 */
5542void *snd_array_new(struct snd_array *array)
5543{
Takashi Iwai12f17712012-10-10 08:59:14 +02005544 if (snd_BUG_ON(!array->elem_size))
5545 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005546 if (array->used >= array->alloced) {
5547 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005548 int size = (num + 1) * array->elem_size;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005549 int oldsize = array->alloced * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005550 void *nlist;
5551 if (snd_BUG_ON(num >= 4096))
5552 return NULL;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005553 nlist = krealloc(array->list, size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005554 if (!nlist)
5555 return NULL;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005556 memset(nlist + oldsize, 0, size - oldsize);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005557 array->list = nlist;
5558 array->alloced = num;
5559 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005560 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005561}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005562EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005563
Takashi Iwaid5191e52009-11-16 14:58:17 +01005564/**
5565 * snd_array_free - free the given array elements
5566 * @array: the array object
5567 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005568void snd_array_free(struct snd_array *array)
5569{
5570 kfree(array->list);
5571 array->used = 0;
5572 array->alloced = 0;
5573 array->list = NULL;
5574}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005575EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005576
Takashi Iwaid5191e52009-11-16 14:58:17 +01005577/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005578 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5579 * @pcm: PCM caps bits
5580 * @buf: the string buffer to write
5581 * @buflen: the max buffer length
5582 *
5583 * used by hda_proc.c and hda_eld.c
5584 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005585void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5586{
5587 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5588 int i, j;
5589
5590 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5591 if (pcm & (AC_SUPPCM_BITS_8 << i))
5592 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5593
5594 buf[j] = '\0'; /* necessary when j == 0 */
5595}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005596EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005597
5598MODULE_DESCRIPTION("HDA codec core");
5599MODULE_LICENSE("GPL");