blob: 733bce62670c070df696e964310ebd641225dd9d [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",
176 "SPDIF In", "Digitial In", "Reserved", "Other"
177 };
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 Iwaia12d3e12011-04-07 15:55:15 +0200385 hda_nid_t list[HDA_MAX_CONNECTIONS];
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200386 int len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200387
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200388 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200389 if (len < 0)
390 return len;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200391 return snd_hda_override_conn_list(codec, nid, len, list);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200392}
Takashi Iwaidce20792011-06-24 14:10:28 +0200393
394/**
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100395 * snd_hda_get_conn_list - get connection list
396 * @codec: the HDA codec
397 * @nid: NID to parse
398 * @len: number of connection list entries
399 * @listp: the pointer to store NID list
400 *
401 * Parses the connection list of the given widget and stores the pointer
402 * to the list of NIDs.
403 *
404 * Returns the number of connections, or a negative error code.
405 *
406 * Note that the returned pointer isn't protected against the list
407 * modification. If snd_hda_override_conn_list() might be called
408 * concurrently, protect with a mutex appropriately.
409 */
410int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
411 const hda_nid_t **listp)
412{
413 bool added = false;
414
415 for (;;) {
416 int err;
417 const struct hda_conn_list *p;
418
419 /* if the connection-list is already cached, read it */
420 p = lookup_conn_list(codec, nid);
421 if (p) {
422 if (listp)
423 *listp = p->conns;
424 return p->len;
425 }
426 if (snd_BUG_ON(added))
427 return -EINVAL;
428
429 err = read_and_add_raw_conns(codec, nid);
430 if (err < 0)
431 return err;
432 added = true;
433 }
434}
435EXPORT_SYMBOL_HDA(snd_hda_get_conn_list);
436
437/**
Takashi Iwaidce20792011-06-24 14:10:28 +0200438 * snd_hda_get_connections - copy connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 * @codec: the HDA codec
440 * @nid: NID to parse
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200441 * @conn_list: connection list array; when NULL, checks only the size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 * @max_conns: max. number of connections to store
443 *
444 * Parses the connection list of the given widget and stores the list
445 * of NIDs.
446 *
447 * Returns the number of connections, or a negative error code.
448 */
449int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200450 hda_nid_t *conn_list, int max_conns)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200451{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100452 const hda_nid_t *list;
453 int len = snd_hda_get_conn_list(codec, nid, &list);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200454
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100455 if (len > 0 && conn_list) {
456 if (len > max_conns) {
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200457 snd_printk(KERN_ERR "hda_codec: "
458 "Too many connections %d for NID 0x%x\n",
459 len, nid);
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200460 return -EINVAL;
461 }
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100462 memcpy(conn_list, list, len * sizeof(hda_nid_t));
Takashi Iwaidce20792011-06-24 14:10:28 +0200463 }
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200464
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100465 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200466}
467EXPORT_SYMBOL_HDA(snd_hda_get_connections);
468
Takashi Iwai9e7717c2011-07-11 15:42:52 +0200469/**
470 * snd_hda_get_raw_connections - copy connection list without cache
471 * @codec: the HDA codec
472 * @nid: NID to parse
473 * @conn_list: connection list array
474 * @max_conns: max. number of connections to store
475 *
476 * Like snd_hda_get_connections(), copy the connection list but without
477 * checking through the connection-list cache.
478 * Currently called only from hda_proc.c, so not exported.
479 */
480int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
481 hda_nid_t *conn_list, int max_conns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
483 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100484 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 unsigned int shift, num_elems, mask;
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200486 unsigned int wcaps;
Takashi Iwai54d17402005-11-21 16:33:22 +0100487 hda_nid_t prev_nid;
Takashi Iwai5fdaecd2012-12-20 10:45:55 +0100488 int null_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Takashi Iwaida3cec32008-08-08 17:12:14 +0200490 if (snd_BUG_ON(!conn_list || max_conns <= 0))
491 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Takashi Iwai1ba7a7c2009-07-27 12:56:26 +0200493 wcaps = get_wcaps(codec, nid);
494 if (!(wcaps & AC_WCAP_CONN_LIST) &&
Takashi Iwai8d087c72011-06-28 12:45:47 +0200495 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
496 return 0;
Jaroslav Kysela16a433d2009-07-22 16:20:40 +0200497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
499 if (parm & AC_CLIST_LONG) {
500 /* long form */
501 shift = 16;
502 num_elems = 2;
503 } else {
504 /* short form */
505 shift = 8;
506 num_elems = 4;
507 }
508 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 mask = (1 << (shift-1)) - 1;
510
Takashi Iwai0ba21762007-04-16 11:29:14 +0200511 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 return 0; /* no connection */
513
514 if (conn_len == 1) {
515 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200516 parm = snd_hda_codec_read(codec, nid, 0,
517 AC_VERB_GET_CONNECT_LIST, 0);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200518 if (parm == -1 && codec->bus->rirb_error)
519 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 conn_list[0] = parm & mask;
521 return 1;
522 }
523
524 /* multi connection */
525 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100526 prev_nid = 0;
527 for (i = 0; i < conn_len; i++) {
528 int range_val;
529 hda_nid_t val, n;
530
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200531 if (i % num_elems == 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100532 parm = snd_hda_codec_read(codec, nid, 0,
533 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200534 if (parm == -1 && codec->bus->rirb_error)
535 return -EIO;
536 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200537 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100538 val = parm & mask;
Takashi Iwai5fdaecd2012-12-20 10:45:55 +0100539 if (val == 0 && null_count++) { /* no second chance */
Jaroslav Kysela2e9bf242009-07-18 11:48:19 +0200540 snd_printk(KERN_WARNING "hda_codec: "
541 "invalid CONNECT_LIST verb %x[%i]:%x\n",
542 nid, i, parm);
543 return 0;
544 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100545 parm >>= shift;
546 if (range_val) {
547 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200548 if (!prev_nid || prev_nid >= val) {
549 snd_printk(KERN_WARNING "hda_codec: "
550 "invalid dep_range_val %x:%x\n",
551 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100552 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100554 for (n = prev_nid + 1; n <= val; n++) {
555 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200556 snd_printk(KERN_ERR "hda_codec: "
557 "Too many connections %d for NID 0x%x\n",
558 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100559 return -EINVAL;
560 }
561 conn_list[conns++] = n;
562 }
563 } else {
564 if (conns >= max_conns) {
Takashi Iwai5aacc212010-07-30 10:36:29 +0200565 snd_printk(KERN_ERR "hda_codec: "
566 "Too many connections %d for NID 0x%x\n",
567 conns, nid);
Takashi Iwai54d17402005-11-21 16:33:22 +0100568 return -EINVAL;
569 }
570 conn_list[conns++] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100572 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 }
574 return conns;
575}
576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200578 * snd_hda_override_conn_list - add/modify the connection-list to cache
579 * @codec: the HDA codec
580 * @nid: NID to parse
581 * @len: number of connection list entries
582 * @list: the list of connection entries
583 *
584 * Add or modify the given connection-list to the cache. If the corresponding
585 * cache already exists, invalidate it and append a new one.
586 *
587 * Returns zero or a negative error code.
588 */
589int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
590 const hda_nid_t *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100592 struct hda_conn_list *p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200593
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100594 p = lookup_conn_list(codec, nid);
595 if (p) {
596 list_del(&p->list);
597 kfree(p);
598 }
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200599
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100600 return add_conn_list(codec, nid, len, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200602EXPORT_SYMBOL_HDA(snd_hda_override_conn_list);
603
604/**
Takashi Iwai8d087c72011-06-28 12:45:47 +0200605 * snd_hda_get_conn_index - get the connection index of the given NID
606 * @codec: the HDA codec
607 * @mux: NID containing the list
608 * @nid: NID to select
609 * @recursive: 1 when searching NID recursively, otherwise 0
610 *
611 * Parses the connection list of the widget @mux and checks whether the
612 * widget @nid is present. If it is, return the connection index.
613 * Otherwise it returns -1.
614 */
615int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
616 hda_nid_t nid, int recursive)
617{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100618 const hda_nid_t *conn;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200619 int i, nums;
620
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100621 nums = snd_hda_get_conn_list(codec, mux, &conn);
Takashi Iwai8d087c72011-06-28 12:45:47 +0200622 for (i = 0; i < nums; i++)
623 if (conn[i] == nid)
624 return i;
625 if (!recursive)
626 return -1;
Takashi Iwaid94ddd82012-12-20 14:42:42 +0100627 if (recursive > 10) {
Takashi Iwai8d087c72011-06-28 12:45:47 +0200628 snd_printd("hda_codec: too deep connection for 0x%x\n", nid);
629 return -1;
630 }
631 recursive++;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200632 for (i = 0; i < nums; i++) {
633 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
634 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
635 continue;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200636 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
637 return i;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200638 }
Takashi Iwai8d087c72011-06-28 12:45:47 +0200639 return -1;
640}
641EXPORT_SYMBOL_HDA(snd_hda_get_conn_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643/**
644 * snd_hda_queue_unsol_event - add an unsolicited event to queue
645 * @bus: the BUS
646 * @res: unsolicited event (lower 32bit of RIRB entry)
647 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
648 *
649 * Adds the given event to the queue. The events are processed in
650 * the workqueue asynchronously. Call this function in the interrupt
651 * hanlder when RIRB receives an unsolicited event.
652 *
653 * Returns 0 if successful, or a negative error code.
654 */
655int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
656{
657 struct hda_bus_unsolicited *unsol;
658 unsigned int wp;
659
Takashi Iwaiecf726f2011-08-09 14:22:44 +0200660 trace_hda_unsol_event(bus, res, res_ex);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200661 unsol = bus->unsol;
662 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 return 0;
664
665 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
666 unsol->wp = wp;
667
668 wp <<= 1;
669 unsol->queue[wp] = res;
670 unsol->queue[wp + 1] = res_ex;
671
Takashi Iwai6acaed32009-01-12 10:09:24 +0100672 queue_work(bus->workq, &unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674 return 0;
675}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100676EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800679 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 */
David Howellsc4028952006-11-22 14:57:56 +0000681static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
David Howellsc4028952006-11-22 14:57:56 +0000683 struct hda_bus_unsolicited *unsol =
684 container_of(work, struct hda_bus_unsolicited, work);
685 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 struct hda_codec *codec;
687 unsigned int rp, caddr, res;
688
689 while (unsol->rp != unsol->wp) {
690 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
691 unsol->rp = rp;
692 rp <<= 1;
693 res = unsol->queue[rp];
694 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200695 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 continue;
697 codec = bus->caddr_tbl[caddr & 0x0f];
698 if (codec && codec->patch_ops.unsol_event)
699 codec->patch_ops.unsol_event(codec, res);
700 }
701}
702
703/*
704 * initialize unsolicited queue
705 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200706static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
708 struct hda_bus_unsolicited *unsol;
709
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100710 if (bus->unsol) /* already initialized */
711 return 0;
712
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200713 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200714 if (!unsol) {
715 snd_printk(KERN_ERR "hda_codec: "
716 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 return -ENOMEM;
718 }
David Howellsc4028952006-11-22 14:57:56 +0000719 INIT_WORK(&unsol->work, process_unsol_events);
720 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 bus->unsol = unsol;
722 return 0;
723}
724
725/*
726 * destructor
727 */
728static void snd_hda_codec_free(struct hda_codec *codec);
729
730static int snd_hda_bus_free(struct hda_bus *bus)
731{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200732 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Takashi Iwai0ba21762007-04-16 11:29:14 +0200734 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 return 0;
Takashi Iwai6acaed32009-01-12 10:09:24 +0100736 if (bus->workq)
737 flush_workqueue(bus->workq);
738 if (bus->unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 kfree(bus->unsol);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200740 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 snd_hda_codec_free(codec);
742 }
743 if (bus->ops.private_free)
744 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100745 if (bus->workq)
746 destroy_workqueue(bus->workq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 kfree(bus);
748 return 0;
749}
750
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100751static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
753 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100754 bus->shutdown = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 return snd_hda_bus_free(bus);
756}
757
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200758#ifdef CONFIG_SND_HDA_HWDEP
759static int snd_hda_bus_dev_register(struct snd_device *device)
760{
761 struct hda_bus *bus = device->device_data;
762 struct hda_codec *codec;
763 list_for_each_entry(codec, &bus->codec_list, list) {
764 snd_hda_hwdep_add_sysfs(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +0100765 snd_hda_hwdep_add_power_sysfs(codec);
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200766 }
767 return 0;
768}
769#else
770#define snd_hda_bus_dev_register NULL
771#endif
772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773/**
774 * snd_hda_bus_new - create a HDA bus
775 * @card: the card entry
776 * @temp: the template for hda_bus information
777 * @busp: the pointer to store the created bus instance
778 *
779 * Returns 0 if successful, or a negative error code.
780 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +0100781int snd_hda_bus_new(struct snd_card *card,
Takashi Iwai756e2b02007-04-16 11:27:07 +0200782 const struct hda_bus_template *temp,
783 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
785 struct hda_bus *bus;
786 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100787 static struct snd_device_ops dev_ops = {
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200788 .dev_register = snd_hda_bus_dev_register,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 .dev_free = snd_hda_bus_dev_free,
790 };
791
Takashi Iwaida3cec32008-08-08 17:12:14 +0200792 if (snd_BUG_ON(!temp))
793 return -EINVAL;
794 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
795 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797 if (busp)
798 *busp = NULL;
799
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200800 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 if (bus == NULL) {
802 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
803 return -ENOMEM;
804 }
805
806 bus->card = card;
807 bus->private_data = temp->private_data;
808 bus->pci = temp->pci;
809 bus->modelname = temp->modelname;
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100810 bus->power_save = temp->power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 bus->ops = temp->ops;
812
Ingo Molnar62932df2006-01-16 16:34:20 +0100813 mutex_init(&bus->cmd_mutex);
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200814 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 INIT_LIST_HEAD(&bus->codec_list);
816
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100817 snprintf(bus->workq_name, sizeof(bus->workq_name),
818 "hd-audio%d", card->number);
819 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100820 if (!bus->workq) {
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100821 snd_printk(KERN_ERR "cannot create workqueue %s\n",
822 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100823 kfree(bus);
824 return -ENOMEM;
825 }
826
Takashi Iwai0ba21762007-04-16 11:29:14 +0200827 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
828 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 snd_hda_bus_free(bus);
830 return err;
831 }
832 if (busp)
833 *busp = bus;
834 return 0;
835}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100836EXPORT_SYMBOL_HDA(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Takashi Iwai82467612007-07-27 19:15:54 +0200838#ifdef CONFIG_SND_HDA_GENERIC
839#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200840 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200841#else
842#define is_generic_config(codec) 0
843#endif
844
Takashi Iwai645f10c2008-11-28 15:07:37 +0100845#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100846#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
847#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100848#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100849#endif
850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851/*
852 * find a matching codec preset
853 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200854static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200855find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100857 struct hda_codec_preset_list *tbl;
858 const struct hda_codec_preset *preset;
Takashi Iwai5d908ab2012-08-24 18:40:10 +0200859 unsigned int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Takashi Iwai82467612007-07-27 19:15:54 +0200861 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100862 return NULL; /* use the generic parser */
863
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100864 again:
865 mutex_lock(&preset_mutex);
866 list_for_each_entry(tbl, &hda_preset_tables, list) {
867 if (!try_module_get(tbl->owner)) {
868 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
869 continue;
870 }
871 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100873 if (preset->afg && preset->afg != codec->afg)
874 continue;
875 if (preset->mfg && preset->mfg != codec->mfg)
876 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200877 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200879 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200880 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100881 preset->rev == codec->revision_id)) {
882 mutex_unlock(&preset_mutex);
883 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100887 module_put(tbl->owner);
888 }
889 mutex_unlock(&preset_mutex);
890
891 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
892 char name[32];
893 if (!mod_requested)
894 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
895 codec->vendor_id);
896 else
897 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
898 (codec->vendor_id >> 16) & 0xffff);
899 request_module(name);
900 mod_requested++;
901 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 }
903 return NULL;
904}
905
906/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200907 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200909static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
911 const struct hda_vendor_id *c;
912 const char *vendor = NULL;
913 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200914 char tmp[16];
915
916 if (codec->vendor_name)
917 goto get_chip_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
919 for (c = hda_vendor_ids; c->id; c++) {
920 if (c->id == vendor_id) {
921 vendor = c->name;
922 break;
923 }
924 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200925 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 sprintf(tmp, "Generic %04x", vendor_id);
927 vendor = tmp;
928 }
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200929 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
930 if (!codec->vendor_name)
931 return -ENOMEM;
932
933 get_chip_name:
934 if (codec->chip_name)
935 return 0;
936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 if (codec->preset && codec->preset->name)
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200938 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
939 else {
940 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
941 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
942 }
943 if (!codec->chip_name)
Takashi Iwaif44ac832008-07-30 15:01:45 +0200944 return -ENOMEM;
945 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946}
947
948/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200949 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +0100951static void setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200953 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 hda_nid_t nid;
955
956 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
957 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200958 function_id = snd_hda_param_read(codec, nid,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200959 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +0200960 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200961 case AC_GRP_AUDIO_FUNCTION:
962 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200963 codec->afg_function_id = function_id & 0xff;
964 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200965 break;
966 case AC_GRP_MODEM_FUNCTION:
967 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200968 codec->mfg_function_id = function_id & 0xff;
969 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200970 break;
971 default:
972 break;
973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975}
976
977/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100978 * read widget caps for each widget and store in cache
979 */
980static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
981{
982 int i;
983 hda_nid_t nid;
984
985 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
986 &codec->start_nid);
987 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200988 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100989 return -ENOMEM;
990 nid = codec->start_nid;
991 for (i = 0; i < codec->num_nodes; i++, nid++)
992 codec->wcaps[i] = snd_hda_param_read(codec, nid,
993 AC_PAR_AUDIO_WIDGET_CAP);
994 return 0;
995}
996
Takashi Iwai3be14142009-02-20 14:11:16 +0100997/* read all pin default configurations and save codec->init_pins */
998static int read_pin_defaults(struct hda_codec *codec)
999{
1000 int i;
1001 hda_nid_t nid = codec->start_nid;
1002
1003 for (i = 0; i < codec->num_nodes; i++, nid++) {
1004 struct hda_pincfg *pin;
1005 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +02001006 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +01001007 if (wid_type != AC_WID_PIN)
1008 continue;
1009 pin = snd_array_new(&codec->init_pins);
1010 if (!pin)
1011 return -ENOMEM;
1012 pin->nid = nid;
1013 pin->cfg = snd_hda_codec_read(codec, nid, 0,
1014 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001015 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
1016 AC_VERB_GET_PIN_WIDGET_CONTROL,
1017 0);
Takashi Iwai3be14142009-02-20 14:11:16 +01001018 }
1019 return 0;
1020}
1021
1022/* look up the given pin config list and return the item matching with NID */
1023static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
1024 struct snd_array *array,
1025 hda_nid_t nid)
1026{
1027 int i;
1028 for (i = 0; i < array->used; i++) {
1029 struct hda_pincfg *pin = snd_array_elem(array, i);
1030 if (pin->nid == nid)
1031 return pin;
1032 }
1033 return NULL;
1034}
1035
Takashi Iwai3be14142009-02-20 14:11:16 +01001036/* set the current pin config value for the given NID.
1037 * the value is cached, and read via snd_hda_codec_get_pincfg()
1038 */
1039int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
1040 hda_nid_t nid, unsigned int cfg)
1041{
1042 struct hda_pincfg *pin;
1043
Takashi Iwaib82855a2009-12-27 11:24:56 +01001044 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1045 return -EINVAL;
1046
Takashi Iwai3be14142009-02-20 14:11:16 +01001047 pin = look_up_pincfg(codec, list, nid);
1048 if (!pin) {
1049 pin = snd_array_new(list);
1050 if (!pin)
1051 return -ENOMEM;
1052 pin->nid = nid;
1053 }
1054 pin->cfg = cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001055 return 0;
1056}
1057
Takashi Iwaid5191e52009-11-16 14:58:17 +01001058/**
1059 * snd_hda_codec_set_pincfg - Override a pin default configuration
1060 * @codec: the HDA codec
1061 * @nid: NID to set the pin config
1062 * @cfg: the pin default config value
1063 *
1064 * Override a pin default configuration value in the cache.
1065 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1066 * priority than the real hardware value.
1067 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001068int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1069 hda_nid_t nid, unsigned int cfg)
1070{
Takashi Iwai346ff702009-02-23 09:42:57 +01001071 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001072}
1073EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
1074
Takashi Iwaid5191e52009-11-16 14:58:17 +01001075/**
1076 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1077 * @codec: the HDA codec
1078 * @nid: NID to get the pin config
1079 *
1080 * Get the current pin config value of the given pin NID.
1081 * If the pincfg value is cached or overridden via sysfs or driver,
1082 * returns the cached value.
1083 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001084unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1085{
1086 struct hda_pincfg *pin;
1087
Takashi Iwai3be14142009-02-20 14:11:16 +01001088#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001089 pin = look_up_pincfg(codec, &codec->user_pins, nid);
Takashi Iwai3be14142009-02-20 14:11:16 +01001090 if (pin)
1091 return pin->cfg;
1092#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001093 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1094 if (pin)
1095 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001096 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1097 if (pin)
1098 return pin->cfg;
1099 return 0;
1100}
1101EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
1102
Takashi Iwai92ee6162009-12-27 11:18:59 +01001103/**
1104 * snd_hda_shutup_pins - Shut up all pins
1105 * @codec: the HDA codec
1106 *
1107 * Clear all pin controls to shup up before suspend for avoiding click noise.
1108 * The controls aren't cached so that they can be resumed properly.
1109 */
1110void snd_hda_shutup_pins(struct hda_codec *codec)
1111{
1112 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001113 /* don't shut up pins when unloading the driver; otherwise it breaks
1114 * the default pin setup at the next load of the driver
1115 */
1116 if (codec->bus->shutdown)
1117 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001118 for (i = 0; i < codec->init_pins.used; i++) {
1119 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1120 /* use read here for syncing after issuing each verb */
1121 snd_hda_codec_read(codec, pin->nid, 0,
1122 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1123 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001124 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001125}
1126EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
1127
Takashi Iwai2a439522011-07-26 09:52:50 +02001128#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001129/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1130static void restore_shutup_pins(struct hda_codec *codec)
1131{
1132 int i;
1133 if (!codec->pins_shutup)
1134 return;
1135 if (codec->bus->shutdown)
1136 return;
1137 for (i = 0; i < codec->init_pins.used; i++) {
1138 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1139 snd_hda_codec_write(codec, pin->nid, 0,
1140 AC_VERB_SET_PIN_WIDGET_CONTROL,
1141 pin->ctrl);
1142 }
1143 codec->pins_shutup = 0;
1144}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001145#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001146
David Henningsson26a6cb62012-10-09 15:04:21 +02001147static void hda_jackpoll_work(struct work_struct *work)
1148{
1149 struct hda_codec *codec =
1150 container_of(work, struct hda_codec, jackpoll_work.work);
1151 if (!codec->jackpoll_interval)
1152 return;
1153
1154 snd_hda_jack_set_dirty_all(codec);
1155 snd_hda_jack_poll_all(codec);
1156 queue_delayed_work(codec->bus->workq, &codec->jackpoll_work,
1157 codec->jackpoll_interval);
1158}
1159
Takashi Iwai01751f52007-08-10 16:59:39 +02001160static void init_hda_cache(struct hda_cache_rec *cache,
1161 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001162static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001163
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001164/* release all pincfg lists */
1165static void free_init_pincfgs(struct hda_codec *codec)
Takashi Iwai3be14142009-02-20 14:11:16 +01001166{
Takashi Iwai346ff702009-02-23 09:42:57 +01001167 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001168#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001169 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001170#endif
Takashi Iwai3be14142009-02-20 14:11:16 +01001171 snd_array_free(&codec->init_pins);
1172}
1173
Takashi Iwai54d17402005-11-21 16:33:22 +01001174/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001175 * audio-converter setup caches
1176 */
1177struct hda_cvt_setup {
1178 hda_nid_t nid;
1179 u8 stream_tag;
1180 u8 channel_id;
1181 u16 format_id;
1182 unsigned char active; /* cvt is currently used */
1183 unsigned char dirty; /* setups should be cleared */
1184};
1185
1186/* get or create a cache entry for the given audio converter NID */
1187static struct hda_cvt_setup *
1188get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1189{
1190 struct hda_cvt_setup *p;
1191 int i;
1192
1193 for (i = 0; i < codec->cvt_setups.used; i++) {
1194 p = snd_array_elem(&codec->cvt_setups, i);
1195 if (p->nid == nid)
1196 return p;
1197 }
1198 p = snd_array_new(&codec->cvt_setups);
1199 if (p)
1200 p->nid = nid;
1201 return p;
1202}
1203
1204/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 * codec destructor
1206 */
1207static void snd_hda_codec_free(struct hda_codec *codec)
1208{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001209 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 return;
David Henningsson26a6cb62012-10-09 15:04:21 +02001211 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai59cad162012-06-26 15:00:20 +02001212 snd_hda_jack_tbl_clear(codec);
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001213 free_init_pincfgs(codec);
Takashi Iwai83012a72012-08-24 18:38:08 +02001214#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02001215 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001216 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001217#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001219 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001220 snd_array_free(&codec->nids);
Takashi Iwai59cad162012-06-26 15:00:20 +02001221 snd_array_free(&codec->cvt_setups);
Stephen Warren7c935972011-06-01 11:14:17 -06001222 snd_array_free(&codec->spdif_out);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001223 remove_conn_list(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 codec->bus->caddr_tbl[codec->addr] = NULL;
1225 if (codec->patch_ops.free)
1226 codec->patch_ops.free(codec);
Takashi Iwai83012a72012-08-24 18:38:08 +02001227#ifdef CONFIG_PM
Takashi Iwai08fa20a2012-08-31 07:46:56 -07001228 if (!codec->pm_down_notified) /* cancel leftover refcounts */
Takashi Iwai68467f52012-08-28 09:14:29 -07001229 hda_call_pm_notify(codec->bus, false);
1230#endif
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001231 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +02001232 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001233 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001234 kfree(codec->vendor_name);
1235 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001236 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001237 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 kfree(codec);
1239}
1240
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001241static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec,
1242 hda_nid_t fg, unsigned int power_state);
1243
Takashi Iwaid8193872012-08-31 07:54:38 -07001244static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001245 unsigned int power_state);
1246
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247/**
1248 * snd_hda_codec_new - create a HDA codec
1249 * @bus: the bus to assign
1250 * @codec_addr: the codec address
1251 * @codecp: the pointer to store the generated codec
1252 *
1253 * Returns 0 if successful, or a negative error code.
1254 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001255int snd_hda_codec_new(struct hda_bus *bus,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001256 unsigned int codec_addr,
1257 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258{
1259 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001260 char component[31];
Takashi Iwaid8193872012-08-31 07:54:38 -07001261 hda_nid_t fg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 int err;
1263
Takashi Iwaida3cec32008-08-08 17:12:14 +02001264 if (snd_BUG_ON(!bus))
1265 return -EINVAL;
1266 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1267 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
1269 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001270 snd_printk(KERN_ERR "hda_codec: "
1271 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 return -EBUSY;
1273 }
1274
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001275 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 if (codec == NULL) {
1277 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1278 return -ENOMEM;
1279 }
1280
1281 codec->bus = bus;
1282 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001283 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001284 mutex_init(&codec->control_mutex);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001285 mutex_init(&codec->hash_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001286 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001287 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001288 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1289 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001290 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001291 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001292 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Stephen Warren7c935972011-06-01 11:14:17 -06001293 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai361dab32012-05-09 14:35:27 +02001294 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01001295 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001296 INIT_LIST_HEAD(&codec->conn_list);
1297
David Henningsson26a6cb62012-10-09 15:04:21 +02001298 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Takashi Iwai83012a72012-08-24 18:38:08 +02001300#ifdef CONFIG_PM
Takashi Iwai5536c6d2012-05-08 17:08:10 +02001301 spin_lock_init(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001302 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1303 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1304 * the caller has to power down appropriatley after initialization
1305 * phase.
1306 */
1307 hda_keep_power_on(codec);
Takashi Iwai68467f52012-08-28 09:14:29 -07001308 hda_call_pm_notify(bus, true);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001309#endif
1310
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001311 if (codec->bus->modelname) {
1312 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1313 if (!codec->modelname) {
1314 snd_hda_codec_free(codec);
1315 return -ENODEV;
1316 }
1317 }
1318
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 list_add_tail(&codec->list, &bus->codec_list);
1320 bus->caddr_tbl[codec_addr] = codec;
1321
Takashi Iwai0ba21762007-04-16 11:29:14 +02001322 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1323 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001324 if (codec->vendor_id == -1)
1325 /* read again, hopefully the access method was corrected
1326 * in the last read...
1327 */
1328 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1329 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001330 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1331 AC_PAR_SUBSYSTEM_ID);
1332 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1333 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001335 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001336 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001337 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001338 err = -ENODEV;
1339 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 }
1341
Takashi Iwaid8193872012-08-31 07:54:38 -07001342 fg = codec->afg ? codec->afg : codec->mfg;
1343 err = read_widget_caps(codec, fg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001344 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001345 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001346 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001347 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001348 err = read_pin_defaults(codec);
1349 if (err < 0)
1350 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001351
Takashi Iwai0ba21762007-04-16 11:29:14 +02001352 if (!codec->subsystem_id) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001353 codec->subsystem_id =
Takashi Iwaid8193872012-08-31 07:54:38 -07001354 snd_hda_codec_read(codec, fg, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001355 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001356 }
1357
Takashi Iwai83012a72012-08-24 18:38:08 +02001358#ifdef CONFIG_PM
Takashi Iwaid8193872012-08-31 07:54:38 -07001359 codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg,
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001360 AC_PWRST_CLKSTOP);
1361 if (!codec->d3_stop_clk)
1362 bus->power_keep_link_on = 1;
Mengdong Lin5d6147f2012-08-24 12:06:30 +08001363#endif
Takashi Iwaid8193872012-08-31 07:54:38 -07001364 codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
Takashi Iwai983f6b92012-08-28 09:18:01 -07001365 AC_PWRST_EPSS);
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001366
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001367 /* power-up all before initialization */
Takashi Iwaid8193872012-08-31 07:54:38 -07001368 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001369
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001370 snd_hda_codec_proc_new(codec);
1371
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001372 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001373
1374 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1375 codec->subsystem_id, codec->revision_id);
1376 snd_component_add(codec->bus->card, component);
1377
1378 if (codecp)
1379 *codecp = codec;
1380 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001381
1382 error:
1383 snd_hda_codec_free(codec);
1384 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001385}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001386EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001387
Takashi Iwaid5191e52009-11-16 14:58:17 +01001388/**
1389 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1390 * @codec: the HDA codec
1391 *
1392 * Start parsing of the given codec tree and (re-)initialize the whole
1393 * patch instance.
1394 *
1395 * Returns 0 if successful or a negative error code.
1396 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001397int snd_hda_codec_configure(struct hda_codec *codec)
1398{
1399 int err;
1400
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001401 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001402 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001403 err = get_codec_name(codec);
1404 if (err < 0)
1405 return err;
1406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Takashi Iwai82467612007-07-27 19:15:54 +02001408 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001410 goto patched;
1411 }
Takashi Iwai82467612007-07-27 19:15:54 +02001412 if (codec->preset && codec->preset->patch) {
1413 err = codec->preset->patch(codec);
1414 goto patched;
1415 }
1416
1417 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001418 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001419 if (err < 0)
1420 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001421
1422 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001423 if (!err && codec->patch_ops.unsol_event)
1424 err = init_unsol_queue(codec->bus);
Takashi Iwaif62faed2009-12-23 09:27:51 +01001425 /* audio codec should override the mixer name */
1426 if (!err && (codec->afg || !*codec->bus->card->mixername))
1427 snprintf(codec->bus->card->mixername,
1428 sizeof(codec->bus->card->mixername),
1429 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001430 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001432EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
Takashi Iwaied360812012-08-08 17:12:52 +02001434/* update the stream-id if changed */
1435static void update_pcm_stream_id(struct hda_codec *codec,
1436 struct hda_cvt_setup *p, hda_nid_t nid,
1437 u32 stream_tag, int channel_id)
1438{
1439 unsigned int oldval, newval;
1440
1441 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1442 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1443 newval = (stream_tag << 4) | channel_id;
1444 if (oldval != newval)
1445 snd_hda_codec_write(codec, nid, 0,
1446 AC_VERB_SET_CHANNEL_STREAMID,
1447 newval);
1448 p->stream_tag = stream_tag;
1449 p->channel_id = channel_id;
1450 }
1451}
1452
1453/* update the format-id if changed */
1454static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1455 hda_nid_t nid, int format)
1456{
1457 unsigned int oldval;
1458
1459 if (p->format_id != format) {
1460 oldval = snd_hda_codec_read(codec, nid, 0,
1461 AC_VERB_GET_STREAM_FORMAT, 0);
1462 if (oldval != format) {
1463 msleep(1);
1464 snd_hda_codec_write(codec, nid, 0,
1465 AC_VERB_SET_STREAM_FORMAT,
1466 format);
1467 }
1468 p->format_id = format;
1469 }
1470}
1471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472/**
1473 * snd_hda_codec_setup_stream - set up the codec for streaming
1474 * @codec: the CODEC to set up
1475 * @nid: the NID to set up
1476 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1477 * @channel_id: channel id to pass, zero based.
1478 * @format: stream format.
1479 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001480void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1481 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 int channel_id, int format)
1483{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001484 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001485 struct hda_cvt_setup *p;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001486 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001487 int i;
1488
Takashi Iwai0ba21762007-04-16 11:29:14 +02001489 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001490 return;
1491
Takashi Iwai0ba21762007-04-16 11:29:14 +02001492 snd_printdd("hda_codec_setup_stream: "
1493 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001495 p = get_hda_cvt_setup(codec, nid);
Takashi Iwaiaffdb622013-01-07 15:36:45 +01001496 if (!p || p->active)
Takashi Iwaieb541332010-08-06 13:48:11 +02001497 return;
Takashi Iwaied360812012-08-08 17:12:52 +02001498
1499 if (codec->pcm_format_first)
1500 update_pcm_format(codec, p, nid, format);
1501 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1502 if (!codec->pcm_format_first)
1503 update_pcm_format(codec, p, nid, format);
1504
Takashi Iwaieb541332010-08-06 13:48:11 +02001505 p->active = 1;
1506 p->dirty = 0;
1507
1508 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001509 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001510 list_for_each_entry(c, &codec->bus->codec_list, list) {
1511 for (i = 0; i < c->cvt_setups.used; i++) {
1512 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001513 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001514 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001515 p->dirty = 1;
1516 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001519EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Takashi Iwaif0cea792010-08-13 11:56:53 +02001521static void really_cleanup_stream(struct hda_codec *codec,
1522 struct hda_cvt_setup *q);
1523
Takashi Iwaid5191e52009-11-16 14:58:17 +01001524/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001525 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001526 * @codec: the CODEC to clean up
1527 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001528 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001529 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001530void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1531 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001532{
Takashi Iwaieb541332010-08-06 13:48:11 +02001533 struct hda_cvt_setup *p;
1534
Takashi Iwai888afa12008-03-18 09:57:50 +01001535 if (!nid)
1536 return;
1537
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001538 if (codec->no_sticky_stream)
1539 do_now = 1;
1540
Takashi Iwai888afa12008-03-18 09:57:50 +01001541 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001542 p = get_hda_cvt_setup(codec, nid);
Takashi Iwaiaffdb622013-01-07 15:36:45 +01001543 if (p && p->active) {
Takashi Iwaif0cea792010-08-13 11:56:53 +02001544 /* here we just clear the active flag when do_now isn't set;
1545 * actual clean-ups will be done later in
1546 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1547 */
1548 if (do_now)
1549 really_cleanup_stream(codec, p);
1550 else
1551 p->active = 0;
1552 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001553}
Takashi Iwaif0cea792010-08-13 11:56:53 +02001554EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001555
Takashi Iwaieb541332010-08-06 13:48:11 +02001556static void really_cleanup_stream(struct hda_codec *codec,
1557 struct hda_cvt_setup *q)
1558{
1559 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001560 if (q->stream_tag || q->channel_id)
1561 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1562 if (q->format_id)
1563 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1564);
Takashi Iwaieb541332010-08-06 13:48:11 +02001565 memset(q, 0, sizeof(*q));
1566 q->nid = nid;
1567}
1568
1569/* clean up the all conflicting obsolete streams */
1570static void purify_inactive_streams(struct hda_codec *codec)
1571{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001572 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001573 int i;
1574
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001575 list_for_each_entry(c, &codec->bus->codec_list, list) {
1576 for (i = 0; i < c->cvt_setups.used; i++) {
1577 struct hda_cvt_setup *p;
1578 p = snd_array_elem(&c->cvt_setups, i);
1579 if (p->dirty)
1580 really_cleanup_stream(c, p);
1581 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001582 }
1583}
1584
Takashi Iwai2a439522011-07-26 09:52:50 +02001585#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001586/* clean up all streams; called from suspend */
1587static void hda_cleanup_all_streams(struct hda_codec *codec)
1588{
1589 int i;
1590
1591 for (i = 0; i < codec->cvt_setups.used; i++) {
1592 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1593 if (p->stream_tag)
1594 really_cleanup_stream(codec, p);
1595 }
1596}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001597#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001598
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599/*
1600 * amp access functions
1601 */
1602
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001603/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001604#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001605#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001606#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1607#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001609#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
1611/* initialize the hash table */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001612static void init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001613 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614{
Takashi Iwai01751f52007-08-10 16:59:39 +02001615 memset(cache, 0, sizeof(*cache));
1616 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001617 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001618}
1619
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001620static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001621{
Takashi Iwai603c4012008-07-30 15:01:44 +02001622 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623}
1624
1625/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a542010-03-30 18:03:44 +02001626static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627{
Takashi Iwai01751f52007-08-10 16:59:39 +02001628 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1629 u16 cur = cache->hash[idx];
1630 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
1632 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001633 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 if (info->key == key)
1635 return info;
1636 cur = info->next;
1637 }
Takashi Iwaia68d5a542010-03-30 18:03:44 +02001638 return NULL;
1639}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
Takashi Iwaia68d5a542010-03-30 18:03:44 +02001641/* query the hash. allocate an entry if not found. */
1642static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1643 u32 key)
1644{
1645 struct hda_cache_head *info = get_hash(cache, key);
1646 if (!info) {
1647 u16 idx, cur;
1648 /* add a new hash entry */
1649 info = snd_array_new(&cache->buf);
1650 if (!info)
1651 return NULL;
1652 cur = snd_array_index(&cache->buf, info);
1653 info->key = key;
1654 info->val = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001655 info->dirty = 0;
Takashi Iwaia68d5a542010-03-30 18:03:44 +02001656 idx = key % (u16)ARRAY_SIZE(cache->hash);
1657 info->next = cache->hash[idx];
1658 cache->hash[idx] = cur;
1659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 return info;
1661}
1662
Takashi Iwai01751f52007-08-10 16:59:39 +02001663/* query and allocate an amp hash entry */
1664static inline struct hda_amp_info *
1665get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1666{
1667 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1668}
1669
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001670/* overwrite the value with the key in the caps hash */
1671static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1672{
1673 struct hda_amp_info *info;
1674
1675 mutex_lock(&codec->hash_mutex);
1676 info = get_alloc_amp_hash(codec, key);
1677 if (!info) {
1678 mutex_unlock(&codec->hash_mutex);
1679 return -EINVAL;
1680 }
1681 info->amp_caps = val;
1682 info->head.val |= INFO_AMP_CAPS;
1683 mutex_unlock(&codec->hash_mutex);
1684 return 0;
1685}
1686
1687/* query the value from the caps hash; if not found, fetch the current
1688 * value from the given function and store in the hash
1689 */
1690static unsigned int
1691query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1692 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1693{
1694 struct hda_amp_info *info;
1695 unsigned int val;
1696
1697 mutex_lock(&codec->hash_mutex);
1698 info = get_alloc_amp_hash(codec, key);
1699 if (!info) {
1700 mutex_unlock(&codec->hash_mutex);
1701 return 0;
1702 }
1703 if (!(info->head.val & INFO_AMP_CAPS)) {
1704 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1705 val = func(codec, nid, dir);
1706 write_caps_hash(codec, key, val);
1707 } else {
1708 val = info->amp_caps;
1709 mutex_unlock(&codec->hash_mutex);
1710 }
1711 return val;
1712}
1713
1714static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1715 int direction)
1716{
1717 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1718 nid = codec->afg;
1719 return snd_hda_param_read(codec, nid,
1720 direction == HDA_OUTPUT ?
1721 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1722}
1723
Takashi Iwaid5191e52009-11-16 14:58:17 +01001724/**
1725 * query_amp_caps - query AMP capabilities
1726 * @codec: the HD-auio codec
1727 * @nid: the NID to query
1728 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1729 *
1730 * Query AMP capabilities for the given widget and direction.
1731 * Returns the obtained capability bits.
1732 *
1733 * When cap bits have been already read, this doesn't read again but
1734 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001736u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001738 return query_caps_hash(codec, nid, direction,
1739 HDA_HASH_KEY(nid, direction, 0),
1740 read_amp_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001742EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
Takashi Iwaid5191e52009-11-16 14:58:17 +01001744/**
1745 * snd_hda_override_amp_caps - Override the AMP capabilities
1746 * @codec: the CODEC to clean up
1747 * @nid: the NID to clean up
1748 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1749 * @caps: the capability bits to set
1750 *
1751 * Override the cached AMP caps bits value by the given one.
1752 * This function is useful if the driver needs to adjust the AMP ranges,
1753 * e.g. limit to 0dB, etc.
1754 *
1755 * Returns zero if successful or a negative error code.
1756 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001757int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1758 unsigned int caps)
1759{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001760 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001761}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001762EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001763
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001764static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
1765 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001766{
1767 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1768}
1769
Takashi Iwaid5191e52009-11-16 14:58:17 +01001770/**
1771 * snd_hda_query_pin_caps - Query PIN capabilities
1772 * @codec: the HD-auio codec
1773 * @nid: the NID to query
1774 *
1775 * Query PIN capabilities for the given widget.
1776 * Returns the obtained capability bits.
1777 *
1778 * When cap bits have been already read, this doesn't read again but
1779 * returns the cached value.
1780 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001781u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1782{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001783 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001784 read_pin_cap);
1785}
Takashi Iwai1327a322009-03-23 13:07:47 +01001786EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1787
Wu Fengguang864f92b2009-11-18 12:38:02 +08001788/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02001789 * snd_hda_override_pin_caps - Override the pin capabilities
1790 * @codec: the CODEC
1791 * @nid: the NID to override
1792 * @caps: the capability bits to set
1793 *
1794 * Override the cached PIN capabilitiy bits value by the given one.
1795 *
1796 * Returns zero if successful or a negative error code.
1797 */
1798int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1799 unsigned int caps)
1800{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001801 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02001802}
1803EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps);
1804
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001805/* read or sync the hash value with the current value;
1806 * call within hash_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 */
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001808static struct hda_amp_info *
1809update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
Takashi Iwai280e57d2012-12-14 10:32:21 +01001810 int direction, int index, bool init_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001812 struct hda_amp_info *info;
1813 unsigned int parm, val = 0;
1814 bool val_read = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001816 retry:
1817 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1818 if (!info)
1819 return NULL;
1820 if (!(info->head.val & INFO_AMP_VOL(ch))) {
1821 if (!val_read) {
1822 mutex_unlock(&codec->hash_mutex);
1823 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1824 parm |= direction == HDA_OUTPUT ?
1825 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1826 parm |= index;
1827 val = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001828 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001829 val &= 0xff;
1830 val_read = true;
1831 mutex_lock(&codec->hash_mutex);
1832 goto retry;
1833 }
1834 info->vol[ch] = val;
1835 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001836 } else if (init_only)
1837 return NULL;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001838 return info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839}
1840
1841/*
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001842 * write the current volume in info to the h/w
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 */
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001844static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001845 hda_nid_t nid, int ch, int direction, int index,
1846 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847{
1848 u32 parm;
1849
1850 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1851 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1852 parm |= index << AC_AMP_SET_INDEX_SHIFT;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001853 if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) &&
1854 (amp_caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwai38681372012-02-27 15:00:58 +01001855 ; /* set the zero value as a fake mute */
1856 else
1857 parm |= val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1859}
1860
Takashi Iwaid5191e52009-11-16 14:58:17 +01001861/**
1862 * snd_hda_codec_amp_read - Read AMP value
1863 * @codec: HD-audio codec
1864 * @nid: NID to read the AMP value
1865 * @ch: channel (left=0 or right=1)
1866 * @direction: #HDA_INPUT or #HDA_OUTPUT
1867 * @index: the index value (only for input direction)
1868 *
1869 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 */
Takashi Iwai834be882006-03-01 14:16:17 +01001871int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1872 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001874 struct hda_amp_info *info;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001875 unsigned int val = 0;
1876
1877 mutex_lock(&codec->hash_mutex);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001878 info = update_amp_hash(codec, nid, ch, direction, index, false);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001879 if (info)
1880 val = info->vol[ch];
1881 mutex_unlock(&codec->hash_mutex);
1882 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001884EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
Takashi Iwai280e57d2012-12-14 10:32:21 +01001886static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1887 int direction, int idx, int mask, int val,
1888 bool init_only)
1889{
1890 struct hda_amp_info *info;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001891 unsigned int caps;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01001892 unsigned int cache_only;
Takashi Iwai280e57d2012-12-14 10:32:21 +01001893
1894 if (snd_BUG_ON(mask & ~0xff))
1895 mask &= 0xff;
1896 val &= mask;
1897
1898 mutex_lock(&codec->hash_mutex);
1899 info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
1900 if (!info) {
1901 mutex_unlock(&codec->hash_mutex);
1902 return 0;
1903 }
1904 val |= info->vol[ch] & ~mask;
1905 if (info->vol[ch] == val) {
1906 mutex_unlock(&codec->hash_mutex);
1907 return 0;
1908 }
1909 info->vol[ch] = val;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01001910 cache_only = info->head.dirty = codec->cached_write;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001911 caps = info->amp_caps;
Takashi Iwai280e57d2012-12-14 10:32:21 +01001912 mutex_unlock(&codec->hash_mutex);
Takashi Iwaide1e37b2012-12-20 11:00:21 +01001913 if (!cache_only)
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001914 put_vol_mute(codec, caps, nid, ch, direction, idx, val);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001915 return 1;
1916}
1917
Takashi Iwaid5191e52009-11-16 14:58:17 +01001918/**
1919 * snd_hda_codec_amp_update - update the AMP value
1920 * @codec: HD-audio codec
1921 * @nid: NID to read the AMP value
1922 * @ch: channel (left=0 or right=1)
1923 * @direction: #HDA_INPUT or #HDA_OUTPUT
1924 * @idx: the index value (only for input direction)
1925 * @mask: bit mask to set
1926 * @val: the bits value to set
1927 *
1928 * Update the AMP value with a bit mask.
1929 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001930 */
Takashi Iwai834be882006-03-01 14:16:17 +01001931int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1932 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933{
Takashi Iwai280e57d2012-12-14 10:32:21 +01001934 return codec_amp_update(codec, nid, ch, direction, idx, mask, val, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001936EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
Takashi Iwaid5191e52009-11-16 14:58:17 +01001938/**
1939 * snd_hda_codec_amp_stereo - update the AMP stereo values
1940 * @codec: HD-audio codec
1941 * @nid: NID to read the AMP value
1942 * @direction: #HDA_INPUT or #HDA_OUTPUT
1943 * @idx: the index value (only for input direction)
1944 * @mask: bit mask to set
1945 * @val: the bits value to set
1946 *
1947 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1948 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001949 */
1950int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1951 int direction, int idx, int mask, int val)
1952{
1953 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001954
1955 if (snd_BUG_ON(mask & ~0xff))
1956 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001957 for (ch = 0; ch < 2; ch++)
1958 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1959 idx, mask, val);
1960 return ret;
1961}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001962EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001963
Takashi Iwai280e57d2012-12-14 10:32:21 +01001964/* Works like snd_hda_codec_amp_update() but it writes the value only at
1965 * the first access. If the amp was already initialized / updated beforehand,
1966 * this does nothing.
1967 */
1968int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
1969 int dir, int idx, int mask, int val)
1970{
1971 return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true);
1972}
1973EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init);
1974
1975int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
1976 int dir, int idx, int mask, int val)
1977{
1978 int ch, ret = 0;
1979
1980 if (snd_BUG_ON(mask & ~0xff))
1981 mask &= 0xff;
1982 for (ch = 0; ch < 2; ch++)
1983 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
1984 idx, mask, val);
1985 return ret;
1986}
1987EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init_stereo);
1988
Takashi Iwaid5191e52009-11-16 14:58:17 +01001989/**
1990 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1991 * @codec: HD-audio codec
1992 *
1993 * Resume the all amp commands from the cache.
1994 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001995void snd_hda_codec_resume_amp(struct hda_codec *codec)
1996{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001997 int i;
1998
Takashi Iwaic370dd62012-12-13 18:30:04 +01001999 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01002000 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002001 for (i = 0; i < codec->amp_cache.buf.used; i++) {
2002 struct hda_amp_info *buffer;
2003 u32 key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002004 hda_nid_t nid;
2005 unsigned int idx, dir, ch;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002006 struct hda_amp_info info;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002007
2008 buffer = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwai8565f052012-12-20 12:54:18 +01002009 if (!buffer->head.dirty)
2010 continue;
2011 buffer->head.dirty = 0;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002012 info = *buffer;
2013 key = info.head.key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002014 if (!key)
2015 continue;
2016 nid = key & 0xff;
2017 idx = (key >> 16) & 0xff;
2018 dir = (key >> 24) & 0xff;
2019 for (ch = 0; ch < 2; ch++) {
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002020 if (!(info.head.val & INFO_AMP_VOL(ch)))
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002021 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002022 mutex_unlock(&codec->hash_mutex);
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002023 put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx,
2024 info.vol[ch]);
Takashi Iwaic370dd62012-12-13 18:30:04 +01002025 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002026 }
2027 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01002028 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002029}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002030EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002032static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
2033 unsigned int ofs)
2034{
2035 u32 caps = query_amp_caps(codec, nid, dir);
2036 /* get num steps */
2037 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2038 if (ofs < caps)
2039 caps -= ofs;
2040 return caps;
2041}
2042
Takashi Iwaid5191e52009-11-16 14:58:17 +01002043/**
2044 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
2045 *
2046 * The control element is supposed to have the private_value field
2047 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2048 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002049int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
2050 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051{
2052 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2053 u16 nid = get_amp_nid(kcontrol);
2054 u8 chs = get_amp_channels(kcontrol);
2055 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002056 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002058 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2059 uinfo->count = chs == 3 ? 2 : 1;
2060 uinfo->value.integer.min = 0;
2061 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
2062 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002063 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01002064 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
2065 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 return -EINVAL;
2067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 return 0;
2069}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002070EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002072
2073static inline unsigned int
2074read_amp_value(struct hda_codec *codec, hda_nid_t nid,
2075 int ch, int dir, int idx, unsigned int ofs)
2076{
2077 unsigned int val;
2078 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
2079 val &= HDA_AMP_VOLMASK;
2080 if (val >= ofs)
2081 val -= ofs;
2082 else
2083 val = 0;
2084 return val;
2085}
2086
2087static inline int
2088update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2089 int ch, int dir, int idx, unsigned int ofs,
2090 unsigned int val)
2091{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002092 unsigned int maxval;
2093
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002094 if (val > 0)
2095 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02002096 /* ofs = 0: raw max value */
2097 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002098 if (val > maxval)
2099 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002100 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
2101 HDA_AMP_VOLMASK, val);
2102}
2103
Takashi Iwaid5191e52009-11-16 14:58:17 +01002104/**
2105 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
2106 *
2107 * The control element is supposed to have the private_value field
2108 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2109 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002110int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2111 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112{
2113 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2114 hda_nid_t nid = get_amp_nid(kcontrol);
2115 int chs = get_amp_channels(kcontrol);
2116 int dir = get_amp_direction(kcontrol);
2117 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002118 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 long *valp = ucontrol->value.integer.value;
2120
2121 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002122 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002124 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 return 0;
2126}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002127EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
Takashi Iwaid5191e52009-11-16 14:58:17 +01002129/**
2130 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2131 *
2132 * The control element is supposed to have the private_value field
2133 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2134 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002135int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2136 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137{
2138 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2139 hda_nid_t nid = get_amp_nid(kcontrol);
2140 int chs = get_amp_channels(kcontrol);
2141 int dir = get_amp_direction(kcontrol);
2142 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002143 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 long *valp = ucontrol->value.integer.value;
2145 int change = 0;
2146
Takashi Iwaicb53c622007-08-10 17:21:45 +02002147 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002148 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002149 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002150 valp++;
2151 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002152 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002153 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002154 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 return change;
2156}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002157EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
Takashi Iwaid5191e52009-11-16 14:58:17 +01002159/**
2160 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2161 *
2162 * The control element is supposed to have the private_value field
2163 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2164 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002165int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2166 unsigned int size, unsigned int __user *_tlv)
2167{
2168 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2169 hda_nid_t nid = get_amp_nid(kcontrol);
2170 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002171 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002172 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002173 u32 caps, val1, val2;
2174
2175 if (size < 4 * sizeof(unsigned int))
2176 return -ENOMEM;
2177 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002178 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2179 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002180 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002181 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002182 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01002183 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02002184 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002185 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2186 return -EFAULT;
2187 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2188 return -EFAULT;
2189 if (put_user(val1, _tlv + 2))
2190 return -EFAULT;
2191 if (put_user(val2, _tlv + 3))
2192 return -EFAULT;
2193 return 0;
2194}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002195EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002196
Takashi Iwaid5191e52009-11-16 14:58:17 +01002197/**
2198 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2199 * @codec: HD-audio codec
2200 * @nid: NID of a reference widget
2201 * @dir: #HDA_INPUT or #HDA_OUTPUT
2202 * @tlv: TLV data to be stored, at least 4 elements
2203 *
2204 * Set (static) TLV data for a virtual master volume using the AMP caps
2205 * obtained from the reference NID.
2206 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002207 */
2208void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2209 unsigned int *tlv)
2210{
2211 u32 caps;
2212 int nums, step;
2213
2214 caps = query_amp_caps(codec, nid, dir);
2215 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2216 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2217 step = (step + 1) * 25;
2218 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2219 tlv[1] = 2 * sizeof(unsigned int);
2220 tlv[2] = -nums * step;
2221 tlv[3] = step;
2222}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002223EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002224
2225/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002226static struct snd_kcontrol *
Takashi Iwaidcda5802012-10-12 17:24:51 +02002227find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002228{
2229 struct snd_ctl_elem_id id;
2230 memset(&id, 0, sizeof(id));
2231 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002232 id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01002233 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002234 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2235 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002236 strcpy(id.name, name);
2237 return snd_ctl_find_id(codec->bus->card, &id);
2238}
2239
Takashi Iwaid5191e52009-11-16 14:58:17 +01002240/**
2241 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2242 * @codec: HD-audio codec
2243 * @name: ctl id name string
2244 *
2245 * Get the control element with the given id string and IFACE_MIXER.
2246 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002247struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2248 const char *name)
2249{
Takashi Iwaidcda5802012-10-12 17:24:51 +02002250 return find_mixer_ctl(codec, name, 0, 0);
Takashi Iwai09f99702008-02-04 12:31:13 +01002251}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002252EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002253
Takashi Iwaidcda5802012-10-12 17:24:51 +02002254static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
2255 int dev)
Takashi Iwai1afe2062010-12-23 10:17:52 +01002256{
2257 int idx;
2258 for (idx = 0; idx < 16; idx++) { /* 16 ctlrs should be large enough */
Takashi Iwaidcda5802012-10-12 17:24:51 +02002259 if (!find_mixer_ctl(codec, name, dev, idx))
Takashi Iwai1afe2062010-12-23 10:17:52 +01002260 return idx;
2261 }
2262 return -EBUSY;
2263}
2264
Takashi Iwaid5191e52009-11-16 14:58:17 +01002265/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002266 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002267 * @codec: HD-audio codec
2268 * @nid: corresponding NID (optional)
2269 * @kctl: the control element to assign
2270 *
2271 * Add the given control element to an array inside the codec instance.
2272 * All control elements belonging to a codec are supposed to be added
2273 * by this function so that a proper clean-up works at the free or
2274 * reconfiguration time.
2275 *
2276 * If non-zero @nid is passed, the NID is assigned to the control element.
2277 * The assignment is shown in the codec proc file.
2278 *
2279 * snd_hda_ctl_add() checks the control subdev id field whether
2280 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002281 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2282 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002283 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002284int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2285 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002286{
2287 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002288 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002289 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002290
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002291 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002292 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002293 if (nid == 0)
2294 nid = get_amp_nid_(kctl->private_value);
2295 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002296 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2297 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002298 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002299 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002300 err = snd_ctl_add(codec->bus->card, kctl);
2301 if (err < 0)
2302 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002303 item = snd_array_new(&codec->mixers);
2304 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002305 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002306 item->kctl = kctl;
2307 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002308 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002309 return 0;
2310}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002311EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002312
Takashi Iwaid5191e52009-11-16 14:58:17 +01002313/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002314 * snd_hda_add_nid - Assign a NID to a control element
2315 * @codec: HD-audio codec
2316 * @nid: corresponding NID (optional)
2317 * @kctl: the control element to assign
2318 * @index: index to kctl
2319 *
2320 * Add the given control element to an array inside the codec instance.
2321 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2322 * NID:KCTL mapping - for example "Capture Source" selector.
2323 */
2324int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2325 unsigned int index, hda_nid_t nid)
2326{
2327 struct hda_nid_item *item;
2328
2329 if (nid > 0) {
2330 item = snd_array_new(&codec->nids);
2331 if (!item)
2332 return -ENOMEM;
2333 item->kctl = kctl;
2334 item->index = index;
2335 item->nid = nid;
2336 return 0;
2337 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01002338 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2339 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002340 return -EINVAL;
2341}
2342EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2343
2344/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002345 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2346 * @codec: HD-audio codec
2347 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002348void snd_hda_ctls_clear(struct hda_codec *codec)
2349{
2350 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002351 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002352 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002353 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002354 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002355 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002356}
2357
Takashi Iwaia65d6292009-02-23 16:57:04 +01002358/* pseudo device locking
2359 * toggle card->shutdown to allow/disallow the device access (as a hack)
2360 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002361int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002362{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002363 struct snd_card *card = bus->card;
2364 struct hda_codec *codec;
2365
Takashi Iwaia65d6292009-02-23 16:57:04 +01002366 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002367 if (card->shutdown)
2368 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002369 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002370 if (!list_empty(&card->ctl_files))
2371 goto err_clear;
2372
2373 list_for_each_entry(codec, &bus->codec_list, list) {
2374 int pcm;
2375 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2376 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2377 if (!cpcm->pcm)
2378 continue;
2379 if (cpcm->pcm->streams[0].substream_opened ||
2380 cpcm->pcm->streams[1].substream_opened)
2381 goto err_clear;
2382 }
2383 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01002384 spin_unlock(&card->files_lock);
2385 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002386
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002387 err_clear:
2388 card->shutdown = 0;
2389 err_unlock:
2390 spin_unlock(&card->files_lock);
2391 return -EINVAL;
2392}
2393EXPORT_SYMBOL_HDA(snd_hda_lock_devices);
2394
2395void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002396{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002397 struct snd_card *card = bus->card;
2398
2399 card = bus->card;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002400 spin_lock(&card->files_lock);
2401 card->shutdown = 0;
2402 spin_unlock(&card->files_lock);
2403}
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002404EXPORT_SYMBOL_HDA(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002405
Takashi Iwaid5191e52009-11-16 14:58:17 +01002406/**
2407 * snd_hda_codec_reset - Clear all objects assigned to the codec
2408 * @codec: HD-audio codec
2409 *
2410 * This frees the all PCM and control elements assigned to the codec, and
2411 * clears the caches and restores the pin default configurations.
2412 *
2413 * When a device is being used, it returns -EBSY. If successfully freed,
2414 * returns zero.
2415 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002416int snd_hda_codec_reset(struct hda_codec *codec)
2417{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002418 struct hda_bus *bus = codec->bus;
2419 struct snd_card *card = bus->card;
2420 int i;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002421
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002422 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002423 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002424
2425 /* OK, let it free */
David Henningsson26a6cb62012-10-09 15:04:21 +02002426 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai83012a72012-08-24 18:38:08 +02002427#ifdef CONFIG_PM
Takashi Iwaia2d96e72012-05-09 12:36:22 +02002428 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwai339876d2012-05-08 16:57:12 +02002429 codec->power_on = 0;
2430 codec->power_transition = 0;
2431 codec->power_jiffies = jiffies;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002432 flush_workqueue(bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002433#endif
2434 snd_hda_ctls_clear(codec);
2435 /* relase PCMs */
2436 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002437 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002438 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002439 clear_bit(codec->pcm_info[i].device,
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002440 bus->pcm_dev_bits);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002441 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002442 }
2443 if (codec->patch_ops.free)
2444 codec->patch_ops.free(codec);
Takashi Iwai07dc59f2012-09-10 09:39:31 +02002445 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002446 snd_hda_jack_tbl_clear(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002447 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002448 codec->spec = NULL;
2449 free_hda_cache(&codec->amp_cache);
2450 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002451 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2452 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002453 /* free only driver_pins so that init_pins + user_pins are restored */
2454 snd_array_free(&codec->driver_pins);
Takashi Iwai09a60712012-06-26 15:01:33 +02002455 snd_array_free(&codec->cvt_setups);
2456 snd_array_free(&codec->spdif_out);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01002457 snd_array_free(&codec->verbs);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002458 codec->num_pcms = 0;
2459 codec->pcm_info = NULL;
2460 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002461 codec->slave_dig_outs = NULL;
2462 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002463 module_put(codec->owner);
2464 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002465
2466 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002467 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002468 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002469}
2470
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002471typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2472
2473/* apply the function to all matching slave ctls in the mixer list */
2474static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002475 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002476{
2477 struct hda_nid_item *items;
2478 const char * const *s;
2479 int i, err;
2480
2481 items = codec->mixers.list;
2482 for (i = 0; i < codec->mixers.used; i++) {
2483 struct snd_kcontrol *sctl = items[i].kctl;
2484 if (!sctl || !sctl->id.name ||
2485 sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2486 continue;
2487 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002488 char tmpname[sizeof(sctl->id.name)];
2489 const char *name = *s;
2490 if (suffix) {
2491 snprintf(tmpname, sizeof(tmpname), "%s %s",
2492 name, suffix);
2493 name = tmpname;
2494 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002495 if (!strcmp(sctl->id.name, name)) {
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002496 err = func(data, sctl);
2497 if (err)
2498 return err;
2499 break;
2500 }
2501 }
2502 }
2503 return 0;
2504}
2505
2506static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2507{
2508 return 1;
2509}
2510
Takashi Iwai18478e82012-03-09 17:51:10 +01002511/* guess the value corresponding to 0dB */
2512static int get_kctl_0dB_offset(struct snd_kcontrol *kctl)
2513{
2514 int _tlv[4];
2515 const int *tlv = NULL;
2516 int val = -1;
2517
2518 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2519 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2520 mm_segment_t fs = get_fs();
2521 set_fs(get_ds());
2522 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2523 tlv = _tlv;
2524 set_fs(fs);
2525 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2526 tlv = kctl->tlv.p;
2527 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE)
2528 val = -tlv[2] / tlv[3];
2529 return val;
2530}
2531
2532/* call kctl->put with the given value(s) */
2533static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2534{
2535 struct snd_ctl_elem_value *ucontrol;
2536 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2537 if (!ucontrol)
2538 return -ENOMEM;
2539 ucontrol->value.integer.value[0] = val;
2540 ucontrol->value.integer.value[1] = val;
2541 kctl->put(kctl, ucontrol);
2542 kfree(ucontrol);
2543 return 0;
2544}
2545
2546/* initialize the slave volume with 0dB */
2547static int init_slave_0dB(void *data, struct snd_kcontrol *slave)
2548{
2549 int offset = get_kctl_0dB_offset(slave);
2550 if (offset > 0)
2551 put_kctl_with_value(slave, offset);
2552 return 0;
2553}
2554
2555/* unmute the slave */
2556static int init_slave_unmute(void *data, struct snd_kcontrol *slave)
2557{
2558 return put_kctl_with_value(slave, 1);
2559}
2560
Takashi Iwaid5191e52009-11-16 14:58:17 +01002561/**
2562 * snd_hda_add_vmaster - create a virtual master control and add slaves
2563 * @codec: HD-audio codec
2564 * @name: vmaster control name
2565 * @tlv: TLV data (optional)
2566 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002567 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002568 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002569 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002570 *
2571 * Create a virtual master control with the given name. The TLV data
2572 * must be either NULL or a valid data.
2573 *
2574 * @slaves is a NULL-terminated array of strings, each of which is a
2575 * slave control name. All controls with these names are assigned to
2576 * the new virtual master control.
2577 *
2578 * This function returns zero if successful or a negative error code.
2579 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002580int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002581 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002582 const char *suffix, bool init_slave_vol,
2583 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002584{
2585 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002586 int err;
2587
Takashi Iwai29e58532012-03-12 12:25:03 +01002588 if (ctl_ret)
2589 *ctl_ret = NULL;
2590
Takashi Iwai9322ca52012-02-03 14:28:01 +01002591 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002592 if (err != 1) {
Takashi Iwai2f085542008-02-22 18:43:50 +01002593 snd_printdd("No slave found for %s\n", name);
2594 return 0;
2595 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002596 kctl = snd_ctl_make_virtual_master(name, tlv);
2597 if (!kctl)
2598 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002599 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002600 if (err < 0)
2601 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002602
Takashi Iwai9322ca52012-02-03 14:28:01 +01002603 err = map_slaves(codec, slaves, suffix,
2604 (map_slave_func_t)snd_ctl_add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002605 if (err < 0)
2606 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002607
2608 /* init with master mute & zero volume */
2609 put_kctl_with_value(kctl, 0);
2610 if (init_slave_vol)
2611 map_slaves(codec, slaves, suffix,
2612 tlv ? init_slave_0dB : init_slave_unmute, kctl);
2613
Takashi Iwai29e58532012-03-12 12:25:03 +01002614 if (ctl_ret)
2615 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002616 return 0;
2617}
Takashi Iwai18478e82012-03-09 17:51:10 +01002618EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002619
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002620/*
2621 * mute-LED control using vmaster
2622 */
2623static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2624 struct snd_ctl_elem_info *uinfo)
2625{
2626 static const char * const texts[] = {
David Henningssonc86c2d42013-01-03 14:12:29 +01002627 "On", "Off", "Follow Master"
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002628 };
2629 unsigned int index;
2630
2631 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2632 uinfo->count = 1;
2633 uinfo->value.enumerated.items = 3;
2634 index = uinfo->value.enumerated.item;
2635 if (index >= 3)
2636 index = 2;
2637 strcpy(uinfo->value.enumerated.name, texts[index]);
2638 return 0;
2639}
2640
2641static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2642 struct snd_ctl_elem_value *ucontrol)
2643{
2644 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2645 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2646 return 0;
2647}
2648
2649static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2650 struct snd_ctl_elem_value *ucontrol)
2651{
2652 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2653 unsigned int old_mode = hook->mute_mode;
2654
2655 hook->mute_mode = ucontrol->value.enumerated.item[0];
2656 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2657 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2658 if (old_mode == hook->mute_mode)
2659 return 0;
2660 snd_hda_sync_vmaster_hook(hook);
2661 return 1;
2662}
2663
2664static struct snd_kcontrol_new vmaster_mute_mode = {
2665 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2666 .name = "Mute-LED Mode",
2667 .info = vmaster_mute_mode_info,
2668 .get = vmaster_mute_mode_get,
2669 .put = vmaster_mute_mode_put,
2670};
2671
2672/*
2673 * Add a mute-LED hook with the given vmaster switch kctl
2674 * "Mute-LED Mode" control is automatically created and associated with
2675 * the given hook.
2676 */
2677int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735cb2012-03-13 07:55:10 +01002678 struct hda_vmaster_mute_hook *hook,
2679 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002680{
2681 struct snd_kcontrol *kctl;
2682
2683 if (!hook->hook || !hook->sw_kctl)
2684 return 0;
2685 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2686 hook->codec = codec;
2687 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735cb2012-03-13 07:55:10 +01002688 if (!expose_enum_ctl)
2689 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002690 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2691 if (!kctl)
2692 return -ENOMEM;
2693 return snd_hda_ctl_add(codec, 0, kctl);
2694}
2695EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook);
2696
2697/*
2698 * Call the hook with the current value for synchronization
2699 * Should be called in init callback
2700 */
2701void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2702{
2703 if (!hook->hook || !hook->codec)
2704 return;
2705 switch (hook->mute_mode) {
2706 case HDA_VMUTE_FOLLOW_MASTER:
2707 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2708 break;
2709 default:
2710 hook->hook(hook->codec, hook->mute_mode);
2711 break;
2712 }
2713}
2714EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook);
2715
2716
Takashi Iwaid5191e52009-11-16 14:58:17 +01002717/**
2718 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2719 *
2720 * The control element is supposed to have the private_value field
2721 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2722 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002723int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2724 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725{
2726 int chs = get_amp_channels(kcontrol);
2727
2728 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2729 uinfo->count = chs == 3 ? 2 : 1;
2730 uinfo->value.integer.min = 0;
2731 uinfo->value.integer.max = 1;
2732 return 0;
2733}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002734EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735
Takashi Iwaid5191e52009-11-16 14:58:17 +01002736/**
2737 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2738 *
2739 * The control element is supposed to have the private_value field
2740 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2741 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002742int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2743 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744{
2745 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2746 hda_nid_t nid = get_amp_nid(kcontrol);
2747 int chs = get_amp_channels(kcontrol);
2748 int dir = get_amp_direction(kcontrol);
2749 int idx = get_amp_index(kcontrol);
2750 long *valp = ucontrol->value.integer.value;
2751
2752 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002753 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002754 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002756 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002757 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 return 0;
2759}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002760EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761
Takashi Iwaid5191e52009-11-16 14:58:17 +01002762/**
2763 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2764 *
2765 * The control element is supposed to have the private_value field
2766 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2767 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002768int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2769 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770{
2771 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2772 hda_nid_t nid = get_amp_nid(kcontrol);
2773 int chs = get_amp_channels(kcontrol);
2774 int dir = get_amp_direction(kcontrol);
2775 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 long *valp = ucontrol->value.integer.value;
2777 int change = 0;
2778
Takashi Iwaicb53c622007-08-10 17:21:45 +02002779 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002780 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002781 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002782 HDA_AMP_MUTE,
2783 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002784 valp++;
2785 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002786 if (chs & 2)
2787 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002788 HDA_AMP_MUTE,
2789 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002790 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002791 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 return change;
2793}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002794EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795
2796/*
Takashi Iwai985be542005-11-02 18:26:49 +01002797 * bound volume controls
2798 *
2799 * bind multiple volumes (# indices, from 0)
2800 */
2801
2802#define AMP_VAL_IDX_SHIFT 19
2803#define AMP_VAL_IDX_MASK (0x0f<<19)
2804
Takashi Iwaid5191e52009-11-16 14:58:17 +01002805/**
2806 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2807 *
2808 * The control element is supposed to have the private_value field
2809 * set up via HDA_BIND_MUTE*() macros.
2810 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002811int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2812 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002813{
2814 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2815 unsigned long pval;
2816 int err;
2817
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002818 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002819 pval = kcontrol->private_value;
2820 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2821 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2822 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002823 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002824 return err;
2825}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002826EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002827
Takashi Iwaid5191e52009-11-16 14:58:17 +01002828/**
2829 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2830 *
2831 * The control element is supposed to have the private_value field
2832 * set up via HDA_BIND_MUTE*() macros.
2833 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002834int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2835 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002836{
2837 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2838 unsigned long pval;
2839 int i, indices, err = 0, change = 0;
2840
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002841 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002842 pval = kcontrol->private_value;
2843 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2844 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002845 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2846 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002847 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2848 if (err < 0)
2849 break;
2850 change |= err;
2851 }
2852 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002853 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002854 return err < 0 ? err : change;
2855}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002856EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002857
Takashi Iwaid5191e52009-11-16 14:58:17 +01002858/**
2859 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2860 *
2861 * The control element is supposed to have the private_value field
2862 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002863 */
2864int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2865 struct snd_ctl_elem_info *uinfo)
2866{
2867 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2868 struct hda_bind_ctls *c;
2869 int err;
2870
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002871 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002872 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002873 kcontrol->private_value = *c->values;
2874 err = c->ops->info(kcontrol, uinfo);
2875 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002876 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002877 return err;
2878}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002879EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002880
Takashi Iwaid5191e52009-11-16 14:58:17 +01002881/**
2882 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2883 *
2884 * The control element is supposed to have the private_value field
2885 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2886 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002887int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2888 struct snd_ctl_elem_value *ucontrol)
2889{
2890 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2891 struct hda_bind_ctls *c;
2892 int err;
2893
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002894 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002895 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002896 kcontrol->private_value = *c->values;
2897 err = c->ops->get(kcontrol, ucontrol);
2898 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002899 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002900 return err;
2901}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002902EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002903
Takashi Iwaid5191e52009-11-16 14:58:17 +01002904/**
2905 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2906 *
2907 * The control element is supposed to have the private_value field
2908 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2909 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002910int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2911 struct snd_ctl_elem_value *ucontrol)
2912{
2913 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2914 struct hda_bind_ctls *c;
2915 unsigned long *vals;
2916 int err = 0, change = 0;
2917
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002918 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002919 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002920 for (vals = c->values; *vals; vals++) {
2921 kcontrol->private_value = *vals;
2922 err = c->ops->put(kcontrol, ucontrol);
2923 if (err < 0)
2924 break;
2925 change |= err;
2926 }
2927 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002928 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002929 return err < 0 ? err : change;
2930}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002931EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002932
Takashi Iwaid5191e52009-11-16 14:58:17 +01002933/**
2934 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2935 *
2936 * The control element is supposed to have the private_value field
2937 * set up via HDA_BIND_VOL() macro.
2938 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002939int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2940 unsigned int size, unsigned int __user *tlv)
2941{
2942 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2943 struct hda_bind_ctls *c;
2944 int err;
2945
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002946 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002947 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002948 kcontrol->private_value = *c->values;
2949 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2950 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002951 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002952 return err;
2953}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002954EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002955
2956struct hda_ctl_ops snd_hda_bind_vol = {
2957 .info = snd_hda_mixer_amp_volume_info,
2958 .get = snd_hda_mixer_amp_volume_get,
2959 .put = snd_hda_mixer_amp_volume_put,
2960 .tlv = snd_hda_mixer_amp_tlv
2961};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002962EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002963
2964struct hda_ctl_ops snd_hda_bind_sw = {
2965 .info = snd_hda_mixer_amp_switch_info,
2966 .get = snd_hda_mixer_amp_switch_get,
2967 .put = snd_hda_mixer_amp_switch_put,
2968 .tlv = snd_hda_mixer_amp_tlv
2969};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002970EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002971
2972/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 * SPDIF out controls
2974 */
2975
Takashi Iwai0ba21762007-04-16 11:29:14 +02002976static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2977 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978{
2979 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2980 uinfo->count = 1;
2981 return 0;
2982}
2983
Takashi Iwai0ba21762007-04-16 11:29:14 +02002984static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2985 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986{
2987 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2988 IEC958_AES0_NONAUDIO |
2989 IEC958_AES0_CON_EMPHASIS_5015 |
2990 IEC958_AES0_CON_NOT_COPYRIGHT;
2991 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2992 IEC958_AES1_CON_ORIGINAL;
2993 return 0;
2994}
2995
Takashi Iwai0ba21762007-04-16 11:29:14 +02002996static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2997 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998{
2999 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3000 IEC958_AES0_NONAUDIO |
3001 IEC958_AES0_PRO_EMPHASIS_5015;
3002 return 0;
3003}
3004
Takashi Iwai0ba21762007-04-16 11:29:14 +02003005static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
3006 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007{
3008 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003009 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003010 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003012 mutex_lock(&codec->spdif_mutex);
3013 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003014 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
3015 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
3016 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
3017 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003018 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019
3020 return 0;
3021}
3022
3023/* convert from SPDIF status bits to HDA SPDIF bits
3024 * bit 0 (DigEn) is always set zero (to be filled later)
3025 */
3026static unsigned short convert_from_spdif_status(unsigned int sbits)
3027{
3028 unsigned short val = 0;
3029
3030 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003031 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003033 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003035 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
3036 IEC958_AES0_PRO_EMPHASIS_5015)
3037 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003039 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
3040 IEC958_AES0_CON_EMPHASIS_5015)
3041 val |= AC_DIG1_EMPHASIS;
3042 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
3043 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02003045 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
3047 }
3048 return val;
3049}
3050
3051/* convert to SPDIF status bits from HDA SPDIF bits
3052 */
3053static unsigned int convert_to_spdif_status(unsigned short val)
3054{
3055 unsigned int sbits = 0;
3056
Takashi Iwai0ba21762007-04-16 11:29:14 +02003057 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003059 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 sbits |= IEC958_AES0_PROFESSIONAL;
3061 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003062 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
3064 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003065 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003067 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003069 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3071 sbits |= val & (0x7f << 8);
3072 }
3073 return sbits;
3074}
3075
Takashi Iwai2f728532008-09-25 16:32:41 +02003076/* set digital convert verbs both for the given NID and its slaves */
3077static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3078 int verb, int val)
3079{
Takashi Iwaidda14412011-05-02 11:29:30 +02003080 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02003081
Takashi Iwai9e976972008-11-25 08:17:20 +01003082 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003083 d = codec->slave_dig_outs;
3084 if (!d)
3085 return;
3086 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01003087 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003088}
3089
3090static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3091 int dig1, int dig2)
3092{
3093 if (dig1 != -1)
3094 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3095 if (dig2 != -1)
3096 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3097}
3098
Takashi Iwai0ba21762007-04-16 11:29:14 +02003099static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3100 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101{
3102 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003103 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003104 struct hda_spdif_out *spdif;
3105 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 unsigned short val;
3107 int change;
3108
Ingo Molnar62932df2006-01-16 16:34:20 +01003109 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003110 spdif = snd_array_elem(&codec->spdif_out, idx);
3111 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003112 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3114 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3115 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c935972011-06-01 11:14:17 -06003116 val = convert_from_spdif_status(spdif->status);
3117 val |= spdif->ctls & 1;
3118 change = spdif->ctls != val;
3119 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003120 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02003121 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01003122 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 return change;
3124}
3125
Takashi Iwaia5ce8892007-07-23 15:42:26 +02003126#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127
Takashi Iwai0ba21762007-04-16 11:29:14 +02003128static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3129 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130{
3131 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003132 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003133 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003135 mutex_lock(&codec->spdif_mutex);
3136 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003137 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003138 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 return 0;
3140}
3141
Stephen Warren74b654c2011-06-01 11:14:18 -06003142static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3143 int dig1, int dig2)
3144{
3145 set_dig_out_convert(codec, nid, dig1, dig2);
3146 /* unmute amp switch (if any) */
3147 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3148 (dig1 & AC_DIG1_ENABLE))
3149 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3150 HDA_AMP_MUTE, 0);
3151}
3152
Takashi Iwai0ba21762007-04-16 11:29:14 +02003153static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3154 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155{
3156 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003157 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003158 struct hda_spdif_out *spdif;
3159 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 unsigned short val;
3161 int change;
3162
Ingo Molnar62932df2006-01-16 16:34:20 +01003163 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003164 spdif = snd_array_elem(&codec->spdif_out, idx);
3165 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003166 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003168 val |= AC_DIG1_ENABLE;
Stephen Warren7c935972011-06-01 11:14:17 -06003169 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003170 spdif->ctls = val;
3171 if (change && nid != (u16)-1)
3172 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003173 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 return change;
3175}
3176
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003177static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 {
3179 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3180 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003181 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 .info = snd_hda_spdif_mask_info,
3183 .get = snd_hda_spdif_cmask_get,
3184 },
3185 {
3186 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3187 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003188 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 .info = snd_hda_spdif_mask_info,
3190 .get = snd_hda_spdif_pmask_get,
3191 },
3192 {
3193 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003194 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 .info = snd_hda_spdif_mask_info,
3196 .get = snd_hda_spdif_default_get,
3197 .put = snd_hda_spdif_default_put,
3198 },
3199 {
3200 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003201 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 .info = snd_hda_spdif_out_switch_info,
3203 .get = snd_hda_spdif_out_switch_get,
3204 .put = snd_hda_spdif_out_switch_put,
3205 },
3206 { } /* end */
3207};
3208
3209/**
Takashi Iwaidcda5802012-10-12 17:24:51 +02003210 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 * @codec: the HDA codec
Takashi Iwaidcda5802012-10-12 17:24:51 +02003212 * @associated_nid: NID that new ctls associated with
3213 * @cvt_nid: converter NID
3214 * @type: HDA_PCM_TYPE_*
3215 * Creates controls related with the digital output.
3216 * Called from each patch supporting the digital out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 *
3218 * Returns 0 if successful, or a negative error code.
3219 */
Takashi Iwaidcda5802012-10-12 17:24:51 +02003220int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3221 hda_nid_t associated_nid,
3222 hda_nid_t cvt_nid,
3223 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224{
3225 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003226 struct snd_kcontrol *kctl;
3227 struct snd_kcontrol_new *dig_mix;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003228 int idx, dev = 0;
3229 const int spdif_pcm_dev = 1;
Stephen Warren7c935972011-06-01 11:14:17 -06003230 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231
Takashi Iwaidcda5802012-10-12 17:24:51 +02003232 if (codec->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
3233 type == HDA_PCM_TYPE_SPDIF) {
3234 dev = spdif_pcm_dev;
3235 } else if (codec->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
3236 type == HDA_PCM_TYPE_HDMI) {
3237 for (idx = 0; idx < codec->spdif_out.used; idx++) {
3238 spdif = snd_array_elem(&codec->spdif_out, idx);
3239 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3240 kctl = find_mixer_ctl(codec, dig_mix->name, 0, idx);
3241 if (!kctl)
3242 break;
3243 kctl->id.device = spdif_pcm_dev;
3244 }
3245 }
3246 codec->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
3247 }
3248 if (!codec->primary_dig_out_type)
3249 codec->primary_dig_out_type = type;
3250
3251 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", dev);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003252 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003253 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
3254 return -EBUSY;
3255 }
Stephen Warren7c935972011-06-01 11:14:17 -06003256 spdif = snd_array_new(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3258 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003259 if (!kctl)
3260 return -ENOMEM;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003261 kctl->id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01003262 kctl->id.index = idx;
Stephen Warren7c935972011-06-01 11:14:17 -06003263 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003264 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003265 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 return err;
3267 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003268 spdif->nid = cvt_nid;
3269 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c935972011-06-01 11:14:17 -06003270 AC_VERB_GET_DIGI_CONVERT_1, 0);
3271 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 return 0;
3273}
Takashi Iwaidcda5802012-10-12 17:24:51 +02003274EXPORT_SYMBOL_HDA(snd_hda_create_dig_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003276/* get the hda_spdif_out entry from the given NID
3277 * call within spdif_mutex lock
3278 */
Stephen Warren7c935972011-06-01 11:14:17 -06003279struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3280 hda_nid_t nid)
3281{
3282 int i;
3283 for (i = 0; i < codec->spdif_out.used; i++) {
3284 struct hda_spdif_out *spdif =
3285 snd_array_elem(&codec->spdif_out, i);
3286 if (spdif->nid == nid)
3287 return spdif;
3288 }
3289 return NULL;
3290}
3291EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
3292
Stephen Warren74b654c2011-06-01 11:14:18 -06003293void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3294{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003295 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003296
3297 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003298 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003299 spdif->nid = (u16)-1;
3300 mutex_unlock(&codec->spdif_mutex);
3301}
3302EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
3303
3304void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3305{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003306 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003307 unsigned short val;
3308
3309 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003310 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003311 if (spdif->nid != nid) {
3312 spdif->nid = nid;
3313 val = spdif->ctls;
3314 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3315 }
3316 mutex_unlock(&codec->spdif_mutex);
3317}
3318EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
3319
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003321 * SPDIF sharing with analog output
3322 */
3323static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3324 struct snd_ctl_elem_value *ucontrol)
3325{
3326 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3327 ucontrol->value.integer.value[0] = mout->share_spdif;
3328 return 0;
3329}
3330
3331static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3332 struct snd_ctl_elem_value *ucontrol)
3333{
3334 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3335 mout->share_spdif = !!ucontrol->value.integer.value[0];
3336 return 0;
3337}
3338
3339static struct snd_kcontrol_new spdif_share_sw = {
3340 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3341 .name = "IEC958 Default PCM Playback Switch",
3342 .info = snd_ctl_boolean_mono_info,
3343 .get = spdif_share_sw_get,
3344 .put = spdif_share_sw_put,
3345};
3346
Takashi Iwaid5191e52009-11-16 14:58:17 +01003347/**
3348 * snd_hda_create_spdif_share_sw - create Default PCM switch
3349 * @codec: the HDA codec
3350 * @mout: multi-out instance
3351 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003352int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3353 struct hda_multi_out *mout)
3354{
3355 if (!mout->dig_out_nid)
3356 return 0;
3357 /* ATTENTION: here mout is passed as private_data, instead of codec */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003358 return snd_hda_ctl_add(codec, mout->dig_out_nid,
3359 snd_ctl_new1(&spdif_share_sw, mout));
Takashi Iwai9a081602008-02-12 18:37:26 +01003360}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003361EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003362
3363/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 * SPDIF input
3365 */
3366
3367#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3368
Takashi Iwai0ba21762007-04-16 11:29:14 +02003369static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3370 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371{
3372 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3373
3374 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3375 return 0;
3376}
3377
Takashi Iwai0ba21762007-04-16 11:29:14 +02003378static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3379 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380{
3381 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3382 hda_nid_t nid = kcontrol->private_value;
3383 unsigned int val = !!ucontrol->value.integer.value[0];
3384 int change;
3385
Ingo Molnar62932df2006-01-16 16:34:20 +01003386 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003388 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003390 snd_hda_codec_write_cache(codec, nid, 0,
3391 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003393 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 return change;
3395}
3396
Takashi Iwai0ba21762007-04-16 11:29:14 +02003397static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3398 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399{
3400 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3401 hda_nid_t nid = kcontrol->private_value;
3402 unsigned short val;
3403 unsigned int sbits;
3404
Andrew Paprocki3982d172007-12-19 12:13:44 +01003405 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 sbits = convert_to_spdif_status(val);
3407 ucontrol->value.iec958.status[0] = sbits;
3408 ucontrol->value.iec958.status[1] = sbits >> 8;
3409 ucontrol->value.iec958.status[2] = sbits >> 16;
3410 ucontrol->value.iec958.status[3] = sbits >> 24;
3411 return 0;
3412}
3413
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003414static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 {
3416 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003417 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 .info = snd_hda_spdif_in_switch_info,
3419 .get = snd_hda_spdif_in_switch_get,
3420 .put = snd_hda_spdif_in_switch_put,
3421 },
3422 {
3423 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3424 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003425 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 .info = snd_hda_spdif_mask_info,
3427 .get = snd_hda_spdif_in_status_get,
3428 },
3429 { } /* end */
3430};
3431
3432/**
3433 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3434 * @codec: the HDA codec
3435 * @nid: audio in widget NID
3436 *
3437 * Creates controls related with the SPDIF input.
3438 * Called from each patch supporting the SPDIF in.
3439 *
3440 * Returns 0 if successful, or a negative error code.
3441 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003442int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443{
3444 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003445 struct snd_kcontrol *kctl;
3446 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003447 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448
Takashi Iwaidcda5802012-10-12 17:24:51 +02003449 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003450 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003451 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3452 return -EBUSY;
3453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3455 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003456 if (!kctl)
3457 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003459 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003460 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 return err;
3462 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003463 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003464 snd_hda_codec_read(codec, nid, 0,
3465 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003466 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 return 0;
3468}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003469EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003471/*
3472 * command cache
3473 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474
Takashi Iwaic370dd62012-12-13 18:30:04 +01003475/* build a 31bit cache key with the widget id and the command parameter */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003476#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3477#define get_cmd_cache_nid(key) ((key) & 0xff)
3478#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3479
3480/**
3481 * snd_hda_codec_write_cache - send a single command with caching
3482 * @codec: the HDA codec
3483 * @nid: NID to send the command
3484 * @direct: direct flag
3485 * @verb: the verb to send
3486 * @parm: the parameter for the verb
3487 *
3488 * Send a single command without waiting for response.
3489 *
3490 * Returns 0 if successful, or a negative error code.
3491 */
3492int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3493 int direct, unsigned int verb, unsigned int parm)
3494{
Takashi Iwaic370dd62012-12-13 18:30:04 +01003495 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003496 struct hda_cache_head *c;
3497 u32 key;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003498 unsigned int cache_only;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003499
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003500 cache_only = codec->cached_write;
3501 if (!cache_only) {
Takashi Iwaic370dd62012-12-13 18:30:04 +01003502 err = snd_hda_codec_write(codec, nid, direct, verb, parm);
3503 if (err < 0)
3504 return err;
3505 }
3506
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003507 /* parm may contain the verb stuff for get/set amp */
3508 verb = verb | (parm >> 8);
3509 parm &= 0xff;
3510 key = build_cmd_cache_key(nid, verb);
3511 mutex_lock(&codec->bus->cmd_mutex);
3512 c = get_alloc_hash(&codec->cmd_cache, key);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003513 if (c) {
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003514 c->val = parm;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003515 c->dirty = cache_only;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003516 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003517 mutex_unlock(&codec->bus->cmd_mutex);
3518 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003519}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003520EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003521
Takashi Iwaid5191e52009-11-16 14:58:17 +01003522/**
Takashi Iwaia68d5a542010-03-30 18:03:44 +02003523 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3524 * @codec: the HDA codec
3525 * @nid: NID to send the command
3526 * @direct: direct flag
3527 * @verb: the verb to send
3528 * @parm: the parameter for the verb
3529 *
3530 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3531 * command if the parameter is already identical with the cached value.
3532 * If not, it sends the command and refreshes the cache.
3533 *
3534 * Returns 0 if successful, or a negative error code.
3535 */
3536int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3537 int direct, unsigned int verb, unsigned int parm)
3538{
3539 struct hda_cache_head *c;
3540 u32 key;
3541
3542 /* parm may contain the verb stuff for get/set amp */
3543 verb = verb | (parm >> 8);
3544 parm &= 0xff;
3545 key = build_cmd_cache_key(nid, verb);
3546 mutex_lock(&codec->bus->cmd_mutex);
3547 c = get_hash(&codec->cmd_cache, key);
3548 if (c && c->val == parm) {
3549 mutex_unlock(&codec->bus->cmd_mutex);
3550 return 0;
3551 }
3552 mutex_unlock(&codec->bus->cmd_mutex);
3553 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3554}
3555EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3556
3557/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003558 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3559 * @codec: HD-audio codec
3560 *
3561 * Execute all verbs recorded in the command caches to resume.
3562 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003563void snd_hda_codec_resume_cache(struct hda_codec *codec)
3564{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003565 int i;
3566
Takashi Iwaic370dd62012-12-13 18:30:04 +01003567 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01003568 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003569 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3570 struct hda_cache_head *buffer;
3571 u32 key;
3572
3573 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3574 key = buffer->key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003575 if (!key)
3576 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003577 if (!buffer->dirty)
3578 continue;
3579 buffer->dirty = 0;
3580 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003581 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3582 get_cmd_cache_cmd(key), buffer->val);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003583 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003584 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01003585 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003586}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003587EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003588
3589/**
3590 * snd_hda_sequence_write_cache - sequence writes with caching
3591 * @codec: the HDA codec
3592 * @seq: VERB array to send
3593 *
3594 * Send the commands sequentially from the given array.
3595 * Thte commands are recorded on cache for power-save and resume.
3596 * The array must be terminated with NID=0.
3597 */
3598void snd_hda_sequence_write_cache(struct hda_codec *codec,
3599 const struct hda_verb *seq)
3600{
3601 for (; seq->nid; seq++)
3602 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3603 seq->param);
3604}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003605EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003606
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003607void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
3608 unsigned int power_state,
3609 bool eapd_workaround)
Takashi Iwai54d17402005-11-21 16:33:22 +01003610{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003611 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003612 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003613
Takashi Iwaicb53c622007-08-10 17:21:45 +02003614 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003615 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003616 if (!(wcaps & AC_WCAP_POWER))
3617 continue;
3618 /* don't power down the widget if it controls eapd and
3619 * EAPD_BTLENABLE is set.
3620 */
3621 if (eapd_workaround && power_state == AC_PWRST_D3 &&
3622 get_wcaps_type(wcaps) == AC_WID_PIN &&
3623 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3624 int eapd = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003625 AC_VERB_GET_EAPD_BTLENABLE, 0);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003626 if (eapd & 0x02)
3627 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003628 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003629 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
3630 power_state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003631 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003632}
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003633EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3634
3635/*
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003636 * supported power states check
3637 */
3638static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
3639 unsigned int power_state)
3640{
3641 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
3642
Mengdong Line037cb42012-08-10 14:11:58 +02003643 if (sup == -1)
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003644 return false;
3645 if (sup & power_state)
3646 return true;
3647 else
3648 return false;
3649}
3650
3651/*
Takashi Iwai432c6412012-08-28 09:59:20 -07003652 * wait until the state is reached, returns the current state
3653 */
3654static unsigned int hda_sync_power_state(struct hda_codec *codec,
3655 hda_nid_t fg,
3656 unsigned int power_state)
3657{
3658 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3659 unsigned int state, actual_state;
3660
3661 for (;;) {
3662 state = snd_hda_codec_read(codec, fg, 0,
3663 AC_VERB_GET_POWER_STATE, 0);
3664 if (state & AC_PWRST_ERROR)
3665 break;
3666 actual_state = (state >> 4) & 0x0f;
3667 if (actual_state == power_state)
3668 break;
3669 if (time_after_eq(jiffies, end_time))
3670 break;
3671 /* wait until the codec reachs to the target state */
3672 msleep(1);
3673 }
3674 return state;
3675}
3676
3677/*
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003678 * set power state of the codec, and return the power state
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003679 */
Takashi Iwaid8193872012-08-31 07:54:38 -07003680static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003681 unsigned int power_state)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003682{
Takashi Iwaid8193872012-08-31 07:54:38 -07003683 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003684 int count;
3685 unsigned int state;
3686
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003687 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003688 if (power_state == AC_PWRST_D3) {
3689 /* transition time less than 10ms for power down */
Takashi Iwai983f6b92012-08-28 09:18:01 -07003690 msleep(codec->epss ? 10 : 100);
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003691 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08003692
3693 /* repeat power states setting at most 10 times*/
3694 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07003695 if (codec->patch_ops.set_power_state)
3696 codec->patch_ops.set_power_state(codec, fg,
3697 power_state);
3698 else {
3699 snd_hda_codec_read(codec, fg, 0,
3700 AC_VERB_SET_POWER_STATE,
3701 power_state);
3702 snd_hda_codec_set_power_to_all(codec, fg, power_state,
3703 true);
3704 }
3705 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08003706 if (!(state & AC_PWRST_ERROR))
3707 break;
3708 }
Mengdong Linb8dfc4622012-08-23 17:32:30 +08003709
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003710 return state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003711}
Takashi Iwai54d17402005-11-21 16:33:22 +01003712
Takashi Iwai11aeff02008-07-30 15:01:46 +02003713#ifdef CONFIG_SND_HDA_HWDEP
3714/* execute additional init verbs */
3715static void hda_exec_init_verbs(struct hda_codec *codec)
3716{
3717 if (codec->init_verbs.list)
3718 snd_hda_sequence_write(codec, codec->init_verbs.list);
3719}
3720#else
3721static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3722#endif
3723
Takashi Iwai2a439522011-07-26 09:52:50 +02003724#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003725/*
3726 * call suspend and power-down; used both from PM and power-save
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003727 * this function returns the power state in the end
Takashi Iwaicb53c622007-08-10 17:21:45 +02003728 */
Dylan Reidd17344b2012-09-28 15:57:01 -07003729static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003730{
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003731 unsigned int state;
3732
Takashi Iwai989c3182012-11-19 14:14:58 +01003733 codec->in_pm = 1;
3734
Takashi Iwaicb53c622007-08-10 17:21:45 +02003735 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02003736 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02003737 hda_cleanup_all_streams(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003738 state = hda_set_power_state(codec, AC_PWRST_D3);
Dylan Reidd17344b2012-09-28 15:57:01 -07003739 /* Cancel delayed work if we aren't currently running from it. */
3740 if (!in_wq)
3741 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003742 spin_lock(&codec->power_lock);
3743 snd_hda_update_power_acct(codec);
3744 trace_hda_power_down(codec);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003745 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003746 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003747 codec->power_jiffies = jiffies;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003748 spin_unlock(&codec->power_lock);
Takashi Iwai989c3182012-11-19 14:14:58 +01003749 codec->in_pm = 0;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003750 return state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003751}
3752
Takashi Iwaic370dd62012-12-13 18:30:04 +01003753/* mark all entries of cmd and amp caches dirty */
3754static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
3755{
3756 int i;
3757 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3758 struct hda_cache_head *cmd;
3759 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
3760 cmd->dirty = 1;
3761 }
3762 for (i = 0; i < codec->amp_cache.buf.used; i++) {
3763 struct hda_amp_info *amp;
3764 amp = snd_array_elem(&codec->cmd_cache.buf, i);
3765 amp->head.dirty = 1;
3766 }
3767}
3768
Takashi Iwaicb53c622007-08-10 17:21:45 +02003769/*
3770 * kick up codec; used both from PM and power-save
3771 */
3772static void hda_call_codec_resume(struct hda_codec *codec)
3773{
Takashi Iwai989c3182012-11-19 14:14:58 +01003774 codec->in_pm = 1;
3775
Takashi Iwaic370dd62012-12-13 18:30:04 +01003776 hda_mark_cmd_cache_dirty(codec);
3777
Takashi Iwai7f308302012-05-08 16:52:23 +02003778 /* set as if powered on for avoiding re-entering the resume
3779 * in the resume / power-save sequence
3780 */
3781 hda_keep_power_on(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003782 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003783 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003784 hda_exec_init_verbs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003785 if (codec->patch_ops.resume)
3786 codec->patch_ops.resume(codec);
3787 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003788 if (codec->patch_ops.init)
3789 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003790 snd_hda_codec_resume_amp(codec);
3791 snd_hda_codec_resume_cache(codec);
3792 }
David Henningsson26a6cb62012-10-09 15:04:21 +02003793
3794 if (codec->jackpoll_interval)
3795 hda_jackpoll_work(&codec->jackpoll_work.work);
3796 else {
3797 snd_hda_jack_set_dirty_all(codec);
3798 snd_hda_jack_report_sync(codec);
3799 }
Takashi Iwai989c3182012-11-19 14:14:58 +01003800
3801 codec->in_pm = 0;
Takashi Iwai7f308302012-05-08 16:52:23 +02003802 snd_hda_power_down(codec); /* flag down before returning */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003803}
Takashi Iwai2a439522011-07-26 09:52:50 +02003804#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003805
Takashi Iwai54d17402005-11-21 16:33:22 +01003806
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807/**
3808 * snd_hda_build_controls - build mixer controls
3809 * @bus: the BUS
3810 *
3811 * Creates mixer controls for each codec included in the bus.
3812 *
3813 * Returns 0 if successful, otherwise a negative error code.
3814 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01003815int snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003817 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818
Takashi Iwai0ba21762007-04-16 11:29:14 +02003819 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003820 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003821 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01003822 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003823 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003824 err = snd_hda_codec_reset(codec);
3825 if (err < 0) {
3826 printk(KERN_ERR
3827 "hda_codec: cannot revert codec\n");
3828 return err;
3829 }
3830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003832 return 0;
3833}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003834EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003836/*
3837 * add standard channel maps if not specified
3838 */
3839static int add_std_chmaps(struct hda_codec *codec)
3840{
3841 int i, str, err;
3842
3843 for (i = 0; i < codec->num_pcms; i++) {
3844 for (str = 0; str < 2; str++) {
3845 struct snd_pcm *pcm = codec->pcm_info[i].pcm;
3846 struct hda_pcm_stream *hinfo =
3847 &codec->pcm_info[i].stream[str];
3848 struct snd_pcm_chmap *chmap;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003849 const struct snd_pcm_chmap_elem *elem;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003850
3851 if (codec->pcm_info[i].own_chmap)
3852 continue;
3853 if (!pcm || !hinfo->substreams)
3854 continue;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003855 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
3856 err = snd_pcm_add_chmap_ctls(pcm, str, elem,
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003857 hinfo->channels_max,
3858 0, &chmap);
3859 if (err < 0)
3860 return err;
3861 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
3862 }
3863 }
3864 return 0;
3865}
3866
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003867/* default channel maps for 2.1 speakers;
3868 * since HD-audio supports only stereo, odd number channels are omitted
3869 */
3870const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
3871 { .channels = 2,
3872 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
3873 { .channels = 4,
3874 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
3875 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
3876 { }
3877};
3878EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
3879
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003880int snd_hda_codec_build_controls(struct hda_codec *codec)
3881{
3882 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003883 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003884 /* continue to initialize... */
3885 if (codec->patch_ops.init)
3886 err = codec->patch_ops.init(codec);
3887 if (!err && codec->patch_ops.build_controls)
3888 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003889 if (err < 0)
3890 return err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003891
3892 /* we create chmaps here instead of build_pcms */
3893 err = add_std_chmaps(codec);
3894 if (err < 0)
3895 return err;
3896
David Henningsson26a6cb62012-10-09 15:04:21 +02003897 if (codec->jackpoll_interval)
3898 hda_jackpoll_work(&codec->jackpoll_work.work);
3899 else
3900 snd_hda_jack_report_sync(codec); /* call at the last init point */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 return 0;
3902}
3903
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904/*
3905 * stream formats
3906 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02003907struct hda_rate_tbl {
3908 unsigned int hz;
3909 unsigned int alsa_bits;
3910 unsigned int hda_fmt;
3911};
3912
Takashi Iwai92f10b32010-08-03 14:21:00 +02003913/* rate = base * mult / div */
3914#define HDA_RATE(base, mult, div) \
3915 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3916 (((div) - 1) << AC_FMT_DIV_SHIFT))
3917
Takashi Iwaibefdf312005-08-22 13:57:55 +02003918static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003920
3921 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003922 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3923 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3924 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3925 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3926 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3927 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3928 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3929 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3930 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3931 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3932 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003933#define AC_PAR_PCM_RATE_BITS 11
3934 /* up to bits 10, 384kHZ isn't supported properly */
3935
3936 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003937 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003938
Takashi Iwaibefdf312005-08-22 13:57:55 +02003939 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940};
3941
3942/**
3943 * snd_hda_calc_stream_format - calculate format bitset
3944 * @rate: the sample rate
3945 * @channels: the number of channels
3946 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3947 * @maxbps: the max. bps
3948 *
3949 * Calculate the format bitset from the given rate, channels and th PCM format.
3950 *
3951 * Return zero if invalid.
3952 */
3953unsigned int snd_hda_calc_stream_format(unsigned int rate,
3954 unsigned int channels,
3955 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03003956 unsigned int maxbps,
3957 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958{
3959 int i;
3960 unsigned int val = 0;
3961
Takashi Iwaibefdf312005-08-22 13:57:55 +02003962 for (i = 0; rate_bits[i].hz; i++)
3963 if (rate_bits[i].hz == rate) {
3964 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 break;
3966 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003967 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 snd_printdd("invalid rate %d\n", rate);
3969 return 0;
3970 }
3971
3972 if (channels == 0 || channels > 8) {
3973 snd_printdd("invalid channels %d\n", channels);
3974 return 0;
3975 }
3976 val |= channels - 1;
3977
3978 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003979 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003980 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003981 break;
3982 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02003983 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003984 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 case 20:
3986 case 24:
3987 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02003988 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003989 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02003991 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02003993 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 break;
3995 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02003996 snd_printdd("invalid format width %d\n",
3997 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 return 0;
3999 }
4000
Anssi Hannula32c168c2010-08-03 13:28:57 +03004001 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004002 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03004003
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 return val;
4005}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004006EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004008static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
4009 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004010{
4011 unsigned int val = 0;
4012 if (nid != codec->afg &&
4013 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
4014 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
4015 if (!val || val == -1)
4016 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
4017 if (!val || val == -1)
4018 return 0;
4019 return val;
4020}
4021
4022static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
4023{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004024 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004025 get_pcm_param);
4026}
4027
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004028static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
4029 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004030{
4031 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
4032 if (!streams || streams == -1)
4033 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
4034 if (!streams || streams == -1)
4035 return 0;
4036 return streams;
4037}
4038
4039static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
4040{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004041 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004042 get_stream_param);
4043}
4044
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045/**
4046 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4047 * @codec: the HDA codec
4048 * @nid: NID to query
4049 * @ratesp: the pointer to store the detected rate bitflags
4050 * @formatsp: the pointer to store the detected formats
4051 * @bpsp: the pointer to store the detected format widths
4052 *
4053 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4054 * or @bsps argument is ignored.
4055 *
4056 * Returns 0 if successful, otherwise a negative error code.
4057 */
Stephen Warren384a48d2011-06-01 11:14:21 -06004058int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
4060{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004061 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004063 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004064 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065
4066 if (ratesp) {
4067 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004068 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02004070 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004072 if (rates == 0) {
4073 snd_printk(KERN_ERR "hda_codec: rates == 0 "
4074 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
4075 nid, val,
4076 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
4077 return -EIO;
4078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 *ratesp = rates;
4080 }
4081
4082 if (formatsp || bpsp) {
4083 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004084 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004086 streams = query_stream_param(codec, nid);
4087 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089
4090 bps = 0;
4091 if (streams & AC_SUPFMT_PCM) {
4092 if (val & AC_SUPPCM_BITS_8) {
4093 formats |= SNDRV_PCM_FMTBIT_U8;
4094 bps = 8;
4095 }
4096 if (val & AC_SUPPCM_BITS_16) {
4097 formats |= SNDRV_PCM_FMTBIT_S16_LE;
4098 bps = 16;
4099 }
4100 if (wcaps & AC_WCAP_DIGITAL) {
4101 if (val & AC_SUPPCM_BITS_32)
4102 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4103 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4104 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4105 if (val & AC_SUPPCM_BITS_24)
4106 bps = 24;
4107 else if (val & AC_SUPPCM_BITS_20)
4108 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004109 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4110 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4112 if (val & AC_SUPPCM_BITS_32)
4113 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 else if (val & AC_SUPPCM_BITS_24)
4115 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02004116 else if (val & AC_SUPPCM_BITS_20)
4117 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 }
4119 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004120#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02004121 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004123 if (!bps)
4124 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02004125 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004126#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02004127 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02004128 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 /* temporary hack: we have still no proper support
4130 * for the direct AC3 stream...
4131 */
4132 formats |= SNDRV_PCM_FMTBIT_U8;
4133 bps = 8;
4134 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004135 if (formats == 0) {
4136 snd_printk(KERN_ERR "hda_codec: formats == 0 "
4137 "(nid=0x%x, val=0x%x, ovrd=%i, "
4138 "streams=0x%x)\n",
4139 nid, val,
4140 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4141 streams);
4142 return -EIO;
4143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 if (formatsp)
4145 *formatsp = formats;
4146 if (bpsp)
4147 *bpsp = bps;
4148 }
4149
4150 return 0;
4151}
Stephen Warren384a48d2011-06-01 11:14:21 -06004152EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153
4154/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004155 * snd_hda_is_supported_format - Check the validity of the format
4156 * @codec: HD-audio codec
4157 * @nid: NID to check
4158 * @format: the HD-audio format value to check
4159 *
4160 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 *
4162 * Returns 1 if supported, 0 if not.
4163 */
4164int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4165 unsigned int format)
4166{
4167 int i;
4168 unsigned int val = 0, rate, stream;
4169
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004170 val = query_pcm_param(codec, nid);
4171 if (!val)
4172 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173
4174 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004175 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02004176 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 if (val & (1 << i))
4178 break;
4179 return 0;
4180 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004181 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 return 0;
4183
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004184 stream = query_stream_param(codec, nid);
4185 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 return 0;
4187
4188 if (stream & AC_SUPFMT_PCM) {
4189 switch (format & 0xf0) {
4190 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004191 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 return 0;
4193 break;
4194 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004195 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 return 0;
4197 break;
4198 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004199 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200 return 0;
4201 break;
4202 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004203 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204 return 0;
4205 break;
4206 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004207 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 return 0;
4209 break;
4210 default:
4211 return 0;
4212 }
4213 } else {
4214 /* FIXME: check for float32 and AC3? */
4215 }
4216
4217 return 1;
4218}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004219EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220
4221/*
4222 * PCM stuff
4223 */
4224static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4225 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004226 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227{
4228 return 0;
4229}
4230
4231static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4232 struct hda_codec *codec,
4233 unsigned int stream_tag,
4234 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004235 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236{
4237 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4238 return 0;
4239}
4240
4241static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4242 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004243 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244{
Takashi Iwai888afa12008-03-18 09:57:50 +01004245 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 return 0;
4247}
4248
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004249static int set_pcm_default_values(struct hda_codec *codec,
4250 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004252 int err;
4253
Takashi Iwai0ba21762007-04-16 11:29:14 +02004254 /* query support PCM information from the given NID */
4255 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004256 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02004257 info->rates ? NULL : &info->rates,
4258 info->formats ? NULL : &info->formats,
4259 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004260 if (err < 0)
4261 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 }
4263 if (info->ops.open == NULL)
4264 info->ops.open = hda_pcm_default_open_close;
4265 if (info->ops.close == NULL)
4266 info->ops.close = hda_pcm_default_open_close;
4267 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004268 if (snd_BUG_ON(!info->nid))
4269 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270 info->ops.prepare = hda_pcm_default_prepare;
4271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004273 if (snd_BUG_ON(!info->nid))
4274 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 info->ops.cleanup = hda_pcm_default_cleanup;
4276 }
4277 return 0;
4278}
4279
Takashi Iwaieb541332010-08-06 13:48:11 +02004280/*
4281 * codec prepare/cleanup entries
4282 */
4283int snd_hda_codec_prepare(struct hda_codec *codec,
4284 struct hda_pcm_stream *hinfo,
4285 unsigned int stream,
4286 unsigned int format,
4287 struct snd_pcm_substream *substream)
4288{
4289 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004290 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004291 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4292 if (ret >= 0)
4293 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004294 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004295 return ret;
4296}
4297EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
4298
4299void snd_hda_codec_cleanup(struct hda_codec *codec,
4300 struct hda_pcm_stream *hinfo,
4301 struct snd_pcm_substream *substream)
4302{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004303 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004304 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004305 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004306}
4307EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
4308
Takashi Iwaid5191e52009-11-16 14:58:17 +01004309/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01004310const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4311 "Audio", "SPDIF", "HDMI", "Modem"
4312};
4313
Takashi Iwai176d5332008-07-30 15:01:44 +02004314/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004315 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01004316 *
4317 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004318 */
4319static int get_empty_pcm_device(struct hda_bus *bus, int type)
4320{
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004321 /* audio device indices; not linear to keep compatibility */
4322 static int audio_idx[HDA_PCM_NTYPES][5] = {
4323 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4324 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01004325 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004326 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004327 };
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004328 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004329
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004330 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004331 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
4332 return -EINVAL;
4333 }
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004334
4335 for (i = 0; audio_idx[type][i] >= 0 ; i++)
4336 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4337 return audio_idx[type][i];
4338
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004339 /* non-fixed slots starting from 10 */
4340 for (i = 10; i < 32; i++) {
4341 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4342 return i;
4343 }
4344
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004345 snd_printk(KERN_WARNING "Too many %s devices\n",
4346 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004347 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004348}
4349
4350/*
Takashi Iwai176d5332008-07-30 15:01:44 +02004351 * attach a new PCM stream
4352 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004353static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02004354{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004355 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02004356 struct hda_pcm_stream *info;
4357 int stream, err;
4358
Takashi Iwaib91f0802008-11-04 08:43:08 +01004359 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02004360 return -EINVAL;
4361 for (stream = 0; stream < 2; stream++) {
4362 info = &pcm->stream[stream];
4363 if (info->substreams) {
4364 err = set_pcm_default_values(codec, info);
4365 if (err < 0)
4366 return err;
4367 }
4368 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004369 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02004370}
4371
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004372/* assign all PCMs of the given codec */
4373int snd_hda_codec_build_pcms(struct hda_codec *codec)
4374{
4375 unsigned int pcm;
4376 int err;
4377
4378 if (!codec->num_pcms) {
4379 if (!codec->patch_ops.build_pcms)
4380 return 0;
4381 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004382 if (err < 0) {
4383 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004384 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004385 err = snd_hda_codec_reset(codec);
4386 if (err < 0) {
4387 printk(KERN_ERR
4388 "hda_codec: cannot revert codec\n");
4389 return err;
4390 }
4391 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004392 }
4393 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4394 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4395 int dev;
4396
4397 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004398 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004399
4400 if (!cpcm->pcm) {
4401 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4402 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004403 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004404 cpcm->device = dev;
4405 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004406 if (err < 0) {
4407 printk(KERN_ERR "hda_codec: cannot attach "
4408 "PCM stream %d for codec #%d\n",
4409 dev, codec->addr);
4410 continue; /* no fatal error */
4411 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004412 }
4413 }
4414 return 0;
4415}
4416
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417/**
4418 * snd_hda_build_pcms - build PCM information
4419 * @bus: the BUS
4420 *
4421 * Create PCM information for each codec included in the bus.
4422 *
4423 * The build_pcms codec patch is requested to set up codec->num_pcms and
4424 * codec->pcm_info properly. The array is referred by the top-level driver
4425 * to create its PCM instances.
4426 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4427 * callback.
4428 *
4429 * At least, substreams, channels_min and channels_max must be filled for
4430 * each stream. substreams = 0 indicates that the stream doesn't exist.
4431 * When rates and/or formats are zero, the supported values are queried
4432 * from the given nid. The nid is used also by the default ops.prepare
4433 * and ops.cleanup callbacks.
4434 *
4435 * The driver needs to call ops.open in its open callback. Similarly,
4436 * ops.close is supposed to be called in the close callback.
4437 * ops.prepare should be called in the prepare or hw_params callback
4438 * with the proper parameters for set up.
4439 * ops.cleanup should be called in hw_free for clean up of streams.
4440 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004441 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 */
Takashi Iwai5cb543d2012-08-09 13:49:23 +02004443int snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004445 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446
Takashi Iwai0ba21762007-04-16 11:29:14 +02004447 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004448 int err = snd_hda_codec_build_pcms(codec);
4449 if (err < 0)
4450 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 }
4452 return 0;
4453}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004454EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456/**
4457 * snd_hda_check_board_config - compare the current codec with the config table
4458 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004459 * @num_configs: number of config enums
4460 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 * @tbl: configuration table, terminated by null entries
4462 *
4463 * Compares the modelname or PCI subsystem id of the current codec with the
4464 * given configuration table. If a matching entry is found, returns its
4465 * config value (supposed to be 0 or positive).
4466 *
4467 * If no entries are matching, the function returns a negative value.
4468 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004469int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004470 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02004471 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472{
Takashi Iwaif44ac832008-07-30 15:01:45 +02004473 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004474 int i;
4475 for (i = 0; i < num_configs; i++) {
4476 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02004477 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004478 snd_printd(KERN_INFO "hda_codec: model '%s' is "
4479 "selected\n", models[i]);
4480 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 }
4482 }
4483 }
4484
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004485 if (!codec->bus->pci || !tbl)
4486 return -1;
4487
4488 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
4489 if (!tbl)
4490 return -1;
4491 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02004492#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004493 char tmp[10];
4494 const char *model = NULL;
4495 if (models)
4496 model = models[tbl->value];
4497 if (!model) {
4498 sprintf(tmp, "#%d", tbl->value);
4499 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004501 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4502 "for config %x:%x (%s)\n",
4503 model, tbl->subvendor, tbl->subdevice,
4504 (tbl->name ? tbl->name : "Unknown device"));
4505#endif
4506 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 }
4508 return -1;
4509}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004510EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511
4512/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004513 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004514 subsystem ID with the
4515 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004516
4517 This is important for Gateway notebooks with SB450 HDA Audio
4518 where the vendor ID of the PCI device is:
4519 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4520 and the vendor/subvendor are found only at the codec.
4521
4522 * @codec: the HDA codec
4523 * @num_configs: number of config enums
4524 * @models: array of model name strings
4525 * @tbl: configuration table, terminated by null entries
4526 *
4527 * Compares the modelname or PCI subsystem id of the current codec with the
4528 * given configuration table. If a matching entry is found, returns its
4529 * config value (supposed to be 0 or positive).
4530 *
4531 * If no entries are matching, the function returns a negative value.
4532 */
4533int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004534 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004535 const struct snd_pci_quirk *tbl)
4536{
4537 const struct snd_pci_quirk *q;
4538
4539 /* Search for codec ID */
4540 for (q = tbl; q->subvendor; q++) {
Takashi Iwaie2301a42011-11-22 19:58:56 +01004541 unsigned int mask = 0xffff0000 | q->subdevice_mask;
4542 unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
4543 if ((codec->subsystem_id & mask) == id)
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004544 break;
4545 }
4546
4547 if (!q->subvendor)
4548 return -1;
4549
4550 tbl = q;
4551
4552 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02004553#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004554 char tmp[10];
4555 const char *model = NULL;
4556 if (models)
4557 model = models[tbl->value];
4558 if (!model) {
4559 sprintf(tmp, "#%d", tbl->value);
4560 model = tmp;
4561 }
4562 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4563 "for config %x:%x (%s)\n",
4564 model, tbl->subvendor, tbl->subdevice,
4565 (tbl->name ? tbl->name : "Unknown device"));
4566#endif
4567 return tbl->value;
4568 }
4569 return -1;
4570}
4571EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4572
4573/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574 * snd_hda_add_new_ctls - create controls from the array
4575 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004576 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 *
4578 * This helper function creates and add new controls in the given array.
4579 * The array must be terminated with an empty entry as terminator.
4580 *
4581 * Returns 0 if successful, or a negative error code.
4582 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004583int snd_hda_add_new_ctls(struct hda_codec *codec,
4584 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004586 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587
4588 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004589 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004590 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004591 if (knew->iface == -1) /* skip this codec private value */
4592 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004593 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004594 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004595 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004596 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004597 if (addr > 0)
4598 kctl->id.device = addr;
4599 if (idx > 0)
4600 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004601 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004602 if (!err)
4603 break;
4604 /* try first with another device index corresponding to
4605 * the codec addr; if it still fails (or it's the
4606 * primary codec), then try another control index
4607 */
4608 if (!addr && codec->addr)
4609 addr = codec->addr;
4610 else if (!idx && !knew->index) {
4611 idx = find_empty_mixer_ctl_idx(codec,
Takashi Iwaidcda5802012-10-12 17:24:51 +02004612 knew->name, 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004613 if (idx <= 0)
4614 return err;
4615 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004616 return err;
4617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 }
4619 return 0;
4620}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004621EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622
Takashi Iwai83012a72012-08-24 18:38:08 +02004623#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02004624static void hda_power_work(struct work_struct *work)
4625{
4626 struct hda_codec *codec =
4627 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004628 struct hda_bus *bus = codec->bus;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004629 unsigned int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004630
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004631 spin_lock(&codec->power_lock);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004632 if (codec->power_transition > 0) { /* during power-up sequence? */
4633 spin_unlock(&codec->power_lock);
4634 return;
4635 }
Maxim Levitsky2e492462007-09-03 15:26:57 +02004636 if (!codec->power_on || codec->power_count) {
4637 codec->power_transition = 0;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004638 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004639 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02004640 }
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004641 spin_unlock(&codec->power_lock);
4642
Dylan Reidd17344b2012-09-28 15:57:01 -07004643 state = hda_call_codec_suspend(codec, true);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004644 codec->pm_down_notified = 0;
4645 if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK)) {
4646 codec->pm_down_notified = 1;
Takashi Iwai68467f52012-08-28 09:14:29 -07004647 hda_call_pm_notify(bus, false);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004648 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004649}
4650
4651static void hda_keep_power_on(struct hda_codec *codec)
4652{
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004653 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004654 codec->power_count++;
4655 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004656 codec->power_jiffies = jiffies;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004657 spin_unlock(&codec->power_lock);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004658}
4659
Takashi Iwaid5191e52009-11-16 14:58:17 +01004660/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01004661void snd_hda_update_power_acct(struct hda_codec *codec)
4662{
4663 unsigned long delta = jiffies - codec->power_jiffies;
4664 if (codec->power_on)
4665 codec->power_on_acct += delta;
4666 else
4667 codec->power_off_acct += delta;
4668 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004669}
4670
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004671/* Transition to powered up, if wait_power_down then wait for a pending
4672 * transition to D3 to complete. A pending D3 transition is indicated
4673 * with power_transition == -1. */
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004674/* call this with codec->power_lock held! */
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004675static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004676{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004677 struct hda_bus *bus = codec->bus;
4678
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004679 /* Return if power_on or transitioning to power_on, unless currently
4680 * powering down. */
4681 if ((codec->power_on || codec->power_transition > 0) &&
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004682 !(wait_power_down && codec->power_transition < 0))
Takashi Iwaicb53c622007-08-10 17:21:45 +02004683 return;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004684 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004685
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004686 cancel_delayed_work_sync(&codec->power_work);
4687
4688 spin_lock(&codec->power_lock);
Dylan Reidb43d2242012-06-21 21:51:22 -07004689 /* If the power down delayed work was cancelled above before starting,
4690 * then there is no need to go through power up here.
4691 */
4692 if (codec->power_on) {
Takashi Iwai535b6c52012-08-20 21:25:22 +02004693 if (codec->power_transition < 0)
4694 codec->power_transition = 0;
Dylan Reidb43d2242012-06-21 21:51:22 -07004695 return;
4696 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004697
Takashi Iwaid66fee52011-08-02 15:39:31 +02004698 trace_hda_power_up(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004699 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004700 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004701 codec->power_jiffies = jiffies;
Takashi Iwai7f308302012-05-08 16:52:23 +02004702 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004703 spin_unlock(&codec->power_lock);
4704
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004705 if (codec->pm_down_notified) {
4706 codec->pm_down_notified = 0;
Takashi Iwai68467f52012-08-28 09:14:29 -07004707 hda_call_pm_notify(bus, true);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004708 }
4709
Takashi Iwaicb53c622007-08-10 17:21:45 +02004710 hda_call_codec_resume(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004711
4712 spin_lock(&codec->power_lock);
Takashi Iwaia221e282007-08-16 16:35:33 +02004713 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004714}
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004715
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004716#define power_save(codec) \
4717 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004718
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004719/* Transition to powered down */
4720static void __snd_hda_power_down(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004721{
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004722 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004723 return;
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004724
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004725 if (power_save(codec)) {
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004726 codec->power_transition = -1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01004727 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004728 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02004729 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004730}
4731
4732/**
4733 * snd_hda_power_save - Power-up/down/sync the codec
4734 * @codec: HD-audio codec
4735 * @delta: the counter delta to change
4736 *
4737 * Change the power-up counter via @delta, and power up or down the hardware
4738 * appropriately. For the power-down, queue to the delayed action.
4739 * Passing zero to @delta means to synchronize the power state.
4740 */
4741void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
4742{
4743 spin_lock(&codec->power_lock);
4744 codec->power_count += delta;
4745 trace_hda_power_count(codec);
4746 if (delta > 0)
4747 __snd_hda_power_up(codec, d3wait);
4748 else
4749 __snd_hda_power_down(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004750 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004751}
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004752EXPORT_SYMBOL_HDA(snd_hda_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004753
Takashi Iwaid5191e52009-11-16 14:58:17 +01004754/**
4755 * snd_hda_check_amp_list_power - Check the amp list and update the power
4756 * @codec: HD-audio codec
4757 * @check: the object containing an AMP list and the status
4758 * @nid: NID to check / update
4759 *
4760 * Check whether the given NID is in the amp list. If it's in the list,
4761 * check the current AMP status, and update the the power-status according
4762 * to the mute status.
4763 *
4764 * This function is supposed to be set or called from the check_power_status
4765 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004766 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004767int snd_hda_check_amp_list_power(struct hda_codec *codec,
4768 struct hda_loopback_check *check,
4769 hda_nid_t nid)
4770{
Takashi Iwai031024e2011-05-02 11:29:30 +02004771 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004772 int ch, v;
4773
4774 if (!check->amplist)
4775 return 0;
4776 for (p = check->amplist; p->nid; p++) {
4777 if (p->nid == nid)
4778 break;
4779 }
4780 if (!p->nid)
4781 return 0; /* nothing changed */
4782
4783 for (p = check->amplist; p->nid; p++) {
4784 for (ch = 0; ch < 2; ch++) {
4785 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4786 p->idx);
4787 if (!(v & HDA_AMP_MUTE) && v > 0) {
4788 if (!check->power_on) {
4789 check->power_on = 1;
4790 snd_hda_power_up(codec);
4791 }
4792 return 1;
4793 }
4794 }
4795 }
4796 if (check->power_on) {
4797 check->power_on = 0;
4798 snd_hda_power_down(codec);
4799 }
4800 return 0;
4801}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004802EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004803#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004805/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004806 * Channel mode helper
4807 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004808
4809/**
4810 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4811 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004812int snd_hda_ch_mode_info(struct hda_codec *codec,
4813 struct snd_ctl_elem_info *uinfo,
4814 const struct hda_channel_mode *chmode,
4815 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004816{
4817 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4818 uinfo->count = 1;
4819 uinfo->value.enumerated.items = num_chmodes;
4820 if (uinfo->value.enumerated.item >= num_chmodes)
4821 uinfo->value.enumerated.item = num_chmodes - 1;
4822 sprintf(uinfo->value.enumerated.name, "%dch",
4823 chmode[uinfo->value.enumerated.item].channels);
4824 return 0;
4825}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004826EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004827
Takashi Iwaid5191e52009-11-16 14:58:17 +01004828/**
4829 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4830 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004831int snd_hda_ch_mode_get(struct hda_codec *codec,
4832 struct snd_ctl_elem_value *ucontrol,
4833 const struct hda_channel_mode *chmode,
4834 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004835 int max_channels)
4836{
4837 int i;
4838
4839 for (i = 0; i < num_chmodes; i++) {
4840 if (max_channels == chmode[i].channels) {
4841 ucontrol->value.enumerated.item[0] = i;
4842 break;
4843 }
4844 }
4845 return 0;
4846}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004847EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004848
Takashi Iwaid5191e52009-11-16 14:58:17 +01004849/**
4850 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4851 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004852int snd_hda_ch_mode_put(struct hda_codec *codec,
4853 struct snd_ctl_elem_value *ucontrol,
4854 const struct hda_channel_mode *chmode,
4855 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004856 int *max_channelsp)
4857{
4858 unsigned int mode;
4859
4860 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01004861 if (mode >= num_chmodes)
4862 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004863 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004864 return 0;
4865 /* change the current channel setting */
4866 *max_channelsp = chmode[mode].channels;
4867 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004868 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004869 return 1;
4870}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004871EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004872
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873/*
4874 * input MUX helper
4875 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004876
4877/**
4878 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4879 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004880int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4881 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882{
4883 unsigned int index;
4884
4885 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4886 uinfo->count = 1;
4887 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004888 if (!imux->num_items)
4889 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890 index = uinfo->value.enumerated.item;
4891 if (index >= imux->num_items)
4892 index = imux->num_items - 1;
4893 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4894 return 0;
4895}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004896EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897
Takashi Iwaid5191e52009-11-16 14:58:17 +01004898/**
4899 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4900 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004901int snd_hda_input_mux_put(struct hda_codec *codec,
4902 const struct hda_input_mux *imux,
4903 struct snd_ctl_elem_value *ucontrol,
4904 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905 unsigned int *cur_val)
4906{
4907 unsigned int idx;
4908
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004909 if (!imux->num_items)
4910 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911 idx = ucontrol->value.enumerated.item[0];
4912 if (idx >= imux->num_items)
4913 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004914 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004916 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4917 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918 *cur_val = idx;
4919 return 1;
4920}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004921EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922
4923
4924/*
Takashi Iwaidda415d2012-11-30 18:34:38 +01004925 * process kcontrol info callback of a simple string enum array
4926 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
4927 */
4928int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
4929 struct snd_ctl_elem_info *uinfo,
4930 int num_items, const char * const *texts)
4931{
4932 static const char * const texts_default[] = {
4933 "Disabled", "Enabled"
4934 };
4935
4936 if (!texts || !num_items) {
4937 num_items = 2;
4938 texts = texts_default;
4939 }
4940
4941 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4942 uinfo->count = 1;
4943 uinfo->value.enumerated.items = num_items;
4944 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
4945 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
4946 strcpy(uinfo->value.enumerated.name,
4947 texts[uinfo->value.enumerated.item]);
4948 return 0;
4949}
4950EXPORT_SYMBOL_HDA(snd_hda_enum_helper_info);
4951
4952/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953 * Multi-channel / digital-out PCM helper functions
4954 */
4955
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004956/* setup SPDIF output stream */
4957static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4958 unsigned int stream_tag, unsigned int format)
4959{
Laurence Darby3bef1c32012-11-03 17:00:06 +00004960 struct hda_spdif_out *spdif;
4961 unsigned int curr_fmt;
4962 bool reset;
Stephen Warren7c935972011-06-01 11:14:17 -06004963
Laurence Darby3bef1c32012-11-03 17:00:06 +00004964 spdif = snd_hda_spdif_out_of_nid(codec, nid);
4965 curr_fmt = snd_hda_codec_read(codec, nid, 0,
4966 AC_VERB_GET_STREAM_FORMAT, 0);
4967 reset = codec->spdif_status_reset &&
4968 (spdif->ctls & AC_DIG1_ENABLE) &&
4969 curr_fmt != format;
4970
4971 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
4972 updated */
4973 if (reset)
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004974 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06004975 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02004976 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004977 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02004978 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004979 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004980 for (d = codec->slave_dig_outs; *d; d++)
4981 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4982 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004983 }
Takashi Iwai2f728532008-09-25 16:32:41 +02004984 /* turn on again (if needed) */
Laurence Darby3bef1c32012-11-03 17:00:06 +00004985 if (reset)
Takashi Iwai2f728532008-09-25 16:32:41 +02004986 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06004987 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02004988}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04004989
Takashi Iwai2f728532008-09-25 16:32:41 +02004990static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4991{
4992 snd_hda_codec_cleanup_stream(codec, nid);
4993 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02004994 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02004995 for (d = codec->slave_dig_outs; *d; d++)
4996 snd_hda_codec_cleanup_stream(codec, *d);
4997 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004998}
4999
Takashi Iwaid5191e52009-11-16 14:58:17 +01005000/**
5001 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
5002 * @bus: HD-audio bus
5003 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005004void snd_hda_bus_reboot_notify(struct hda_bus *bus)
5005{
5006 struct hda_codec *codec;
5007
5008 if (!bus)
5009 return;
5010 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005011 if (hda_codec_is_power_on(codec) &&
5012 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005013 codec->patch_ops.reboot_notify(codec);
5014 }
5015}
Takashi Iwai8f217a22009-11-10 18:26:12 +01005016EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005017
Takashi Iwaid5191e52009-11-16 14:58:17 +01005018/**
5019 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005021int snd_hda_multi_out_dig_open(struct hda_codec *codec,
5022 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023{
Ingo Molnar62932df2006-01-16 16:34:20 +01005024 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02005025 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
5026 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02005027 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01005029 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030 return 0;
5031}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005032EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033
Takashi Iwaid5191e52009-11-16 14:58:17 +01005034/**
5035 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
5036 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005037int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
5038 struct hda_multi_out *mout,
5039 unsigned int stream_tag,
5040 unsigned int format,
5041 struct snd_pcm_substream *substream)
5042{
5043 mutex_lock(&codec->spdif_mutex);
5044 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
5045 mutex_unlock(&codec->spdif_mutex);
5046 return 0;
5047}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005048EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005049
Takashi Iwaid5191e52009-11-16 14:58:17 +01005050/**
5051 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
5052 */
Takashi Iwai9411e212009-02-13 11:32:28 +01005053int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
5054 struct hda_multi_out *mout)
5055{
5056 mutex_lock(&codec->spdif_mutex);
5057 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5058 mutex_unlock(&codec->spdif_mutex);
5059 return 0;
5060}
5061EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
5062
Takashi Iwaid5191e52009-11-16 14:58:17 +01005063/**
5064 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005066int snd_hda_multi_out_dig_close(struct hda_codec *codec,
5067 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068{
Ingo Molnar62932df2006-01-16 16:34:20 +01005069 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01005071 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072 return 0;
5073}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005074EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075
Takashi Iwaid5191e52009-11-16 14:58:17 +01005076/**
5077 * snd_hda_multi_out_analog_open - open analog outputs
5078 *
5079 * Open analog outputs and set up the hw-constraints.
5080 * If the digital outputs can be opened as slave, open the digital
5081 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005083int snd_hda_multi_out_analog_open(struct hda_codec *codec,
5084 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01005085 struct snd_pcm_substream *substream,
5086 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087{
Takashi Iwai9a081602008-02-12 18:37:26 +01005088 struct snd_pcm_runtime *runtime = substream->runtime;
5089 runtime->hw.channels_max = mout->max_channels;
5090 if (mout->dig_out_nid) {
5091 if (!mout->analog_rates) {
5092 mout->analog_rates = hinfo->rates;
5093 mout->analog_formats = hinfo->formats;
5094 mout->analog_maxbps = hinfo->maxbps;
5095 } else {
5096 runtime->hw.rates = mout->analog_rates;
5097 runtime->hw.formats = mout->analog_formats;
5098 hinfo->maxbps = mout->analog_maxbps;
5099 }
5100 if (!mout->spdif_rates) {
5101 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
5102 &mout->spdif_rates,
5103 &mout->spdif_formats,
5104 &mout->spdif_maxbps);
5105 }
5106 mutex_lock(&codec->spdif_mutex);
5107 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02005108 if ((runtime->hw.rates & mout->spdif_rates) &&
5109 (runtime->hw.formats & mout->spdif_formats)) {
5110 runtime->hw.rates &= mout->spdif_rates;
5111 runtime->hw.formats &= mout->spdif_formats;
5112 if (mout->spdif_maxbps < hinfo->maxbps)
5113 hinfo->maxbps = mout->spdif_maxbps;
5114 } else {
5115 mout->share_spdif = 0;
5116 /* FIXME: need notify? */
5117 }
Takashi Iwai9a081602008-02-12 18:37:26 +01005118 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02005119 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01005120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121 return snd_pcm_hw_constraint_step(substream->runtime, 0,
5122 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
5123}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005124EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125
Takashi Iwaid5191e52009-11-16 14:58:17 +01005126/**
5127 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
5128 *
5129 * Set up the i/o for analog out.
5130 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005132int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5133 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134 unsigned int stream_tag,
5135 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01005136 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137{
Takashi Iwaidda14412011-05-02 11:29:30 +02005138 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005140 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141 int i;
5142
Ingo Molnar62932df2006-01-16 16:34:20 +01005143 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005144 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01005145 if (mout->dig_out_nid && mout->share_spdif &&
5146 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02005148 snd_hda_is_supported_format(codec, mout->dig_out_nid,
5149 format) &&
Stephen Warren7c935972011-06-01 11:14:17 -06005150 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005152 setup_dig_out_stream(codec, mout->dig_out_nid,
5153 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154 } else {
5155 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02005156 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157 }
5158 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005159 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160
5161 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005162 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5163 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005164 if (!mout->no_share_stream &&
5165 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005167 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5168 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005169 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005170 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5171 if (!mout->no_share_stream && mout->hp_out_nid[i])
5172 snd_hda_codec_setup_stream(codec,
5173 mout->hp_out_nid[i],
5174 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005175 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02005176 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01005177 snd_hda_codec_setup_stream(codec,
5178 mout->extra_out_nid[i],
5179 stream_tag, 0, format);
5180
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181 /* surrounds */
5182 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02005183 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005184 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5185 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005186 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005187 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5188 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189 }
5190 return 0;
5191}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005192EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193
Takashi Iwaid5191e52009-11-16 14:58:17 +01005194/**
5195 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005197int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5198 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199{
Takashi Iwaidda14412011-05-02 11:29:30 +02005200 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201 int i;
5202
5203 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01005204 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01005206 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005207 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5208 if (mout->hp_out_nid[i])
5209 snd_hda_codec_cleanup_stream(codec,
5210 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005211 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5212 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01005213 snd_hda_codec_cleanup_stream(codec,
5214 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01005215 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02005217 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218 mout->dig_out_used = 0;
5219 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005220 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221 return 0;
5222}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005223EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224
Takashi Iwai47408602012-04-20 13:06:53 +02005225/**
5226 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
5227 *
5228 * Guess the suitable VREF pin bits to be set as the pin-control value.
5229 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5230 */
5231unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5232{
5233 unsigned int pincap;
5234 unsigned int oldval;
5235 oldval = snd_hda_codec_read(codec, pin, 0,
5236 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5237 pincap = snd_hda_query_pin_caps(codec, pin);
5238 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5239 /* Exception: if the default pin setup is vref50, we give it priority */
5240 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5241 return AC_PINCTL_VREF_80;
5242 else if (pincap & AC_PINCAP_VREF_50)
5243 return AC_PINCTL_VREF_50;
5244 else if (pincap & AC_PINCAP_VREF_100)
5245 return AC_PINCTL_VREF_100;
5246 else if (pincap & AC_PINCAP_VREF_GRD)
5247 return AC_PINCTL_VREF_GRD;
5248 return AC_PINCTL_VREF_HIZ;
5249}
5250EXPORT_SYMBOL_HDA(snd_hda_get_default_vref);
5251
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005252int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5253 unsigned int val, bool cached)
5254{
5255 if (val) {
5256 unsigned int cap = snd_hda_query_pin_caps(codec, pin);
Takashi Iwai6942c102012-04-20 13:08:40 +02005257 if (cap && (val & AC_PINCTL_OUT_EN)) {
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005258 if (!(cap & AC_PINCAP_OUT))
5259 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5260 else if ((val & AC_PINCTL_HP_EN) &&
5261 !(cap & AC_PINCAP_HP_DRV))
5262 val &= ~AC_PINCTL_HP_EN;
5263 }
Takashi Iwai6942c102012-04-20 13:08:40 +02005264 if (cap && (val & AC_PINCTL_IN_EN)) {
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005265 if (!(cap & AC_PINCAP_IN))
5266 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5267 }
5268 }
5269 if (cached)
5270 return snd_hda_codec_update_cache(codec, pin, 0,
5271 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5272 else
5273 return snd_hda_codec_write(codec, pin, 0,
5274 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5275}
5276EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl);
5277
Takashi Iwai990061c2010-09-09 22:08:44 +02005278/**
5279 * snd_hda_add_imux_item - Add an item to input_mux
5280 *
5281 * When the same label is used already in the existing items, the number
5282 * suffix is appended to the label. This label index number is stored
5283 * to type_idx when non-NULL pointer is given.
5284 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005285int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5286 int index, int *type_idx)
5287{
5288 int i, label_idx = 0;
5289 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5290 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5291 return -EINVAL;
5292 }
5293 for (i = 0; i < imux->num_items; i++) {
5294 if (!strncmp(label, imux->items[i].label, strlen(label)))
5295 label_idx++;
5296 }
5297 if (type_idx)
5298 *type_idx = label_idx;
5299 if (label_idx > 0)
5300 snprintf(imux->items[imux->num_items].label,
5301 sizeof(imux->items[imux->num_items].label),
5302 "%s %d", label, label_idx);
5303 else
5304 strlcpy(imux->items[imux->num_items].label, label,
5305 sizeof(imux->items[imux->num_items].label));
5306 imux->items[imux->num_items].index = index;
5307 imux->num_items++;
5308 return 0;
5309}
5310EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005311
Takashi Iwai4a471b72005-12-07 13:56:29 +01005312
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313#ifdef CONFIG_PM
5314/*
5315 * power management
5316 */
5317
5318/**
5319 * snd_hda_suspend - suspend the codecs
5320 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321 *
5322 * Returns 0 if successful.
5323 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005324int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005326 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327
Takashi Iwai0ba21762007-04-16 11:29:14 +02005328 list_for_each_entry(codec, &bus->codec_list, list) {
David Henningsson26a6cb62012-10-09 15:04:21 +02005329 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005330 if (hda_codec_is_power_on(codec))
Dylan Reidd17344b2012-09-28 15:57:01 -07005331 hda_call_codec_suspend(codec, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332 }
5333 return 0;
5334}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005335EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336
5337/**
5338 * snd_hda_resume - resume the codecs
5339 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340 *
5341 * Returns 0 if successful.
5342 */
5343int snd_hda_resume(struct hda_bus *bus)
5344{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005345 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346
Takashi Iwai0ba21762007-04-16 11:29:14 +02005347 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai7f308302012-05-08 16:52:23 +02005348 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350 return 0;
5351}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005352EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005353#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005354
5355/*
5356 * generic arrays
5357 */
5358
Takashi Iwaid5191e52009-11-16 14:58:17 +01005359/**
5360 * snd_array_new - get a new element from the given array
5361 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005362 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005363 * Get a new element from the given array. If it exceeds the
5364 * pre-allocated array size, re-allocate the array.
5365 *
5366 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005367 */
5368void *snd_array_new(struct snd_array *array)
5369{
Takashi Iwai12f17712012-10-10 08:59:14 +02005370 if (snd_BUG_ON(!array->elem_size))
5371 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005372 if (array->used >= array->alloced) {
5373 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005374 int size = (num + 1) * array->elem_size;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005375 int oldsize = array->alloced * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005376 void *nlist;
5377 if (snd_BUG_ON(num >= 4096))
5378 return NULL;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005379 nlist = krealloc(array->list, size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005380 if (!nlist)
5381 return NULL;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005382 memset(nlist + oldsize, 0, size - oldsize);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005383 array->list = nlist;
5384 array->alloced = num;
5385 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005386 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005387}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005388EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005389
Takashi Iwaid5191e52009-11-16 14:58:17 +01005390/**
5391 * snd_array_free - free the given array elements
5392 * @array: the array object
5393 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005394void snd_array_free(struct snd_array *array)
5395{
5396 kfree(array->list);
5397 array->used = 0;
5398 array->alloced = 0;
5399 array->list = NULL;
5400}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005401EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005402
Takashi Iwaid5191e52009-11-16 14:58:17 +01005403/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005404 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5405 * @pcm: PCM caps bits
5406 * @buf: the string buffer to write
5407 * @buflen: the max buffer length
5408 *
5409 * used by hda_proc.c and hda_eld.c
5410 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005411void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5412{
5413 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5414 int i, j;
5415
5416 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5417 if (pcm & (AC_SUPPCM_BITS_8 << i))
5418 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5419
5420 buf[j] = '\0'; /* necessary when j == 0 */
5421}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005422EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005423
5424MODULE_DESCRIPTION("HDA codec core");
5425MODULE_LICENSE("GPL");