blob: e6cdad713734dc9a1af62a31511ca81440775848 [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 Iwai09b70e82013-01-10 18:21:56 +01001089 {
1090 unsigned int cfg = 0;
1091 mutex_lock(&codec->user_mutex);
1092 pin = look_up_pincfg(codec, &codec->user_pins, nid);
1093 if (pin)
1094 cfg = pin->cfg;
1095 mutex_unlock(&codec->user_mutex);
1096 if (cfg)
1097 return cfg;
1098 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001099#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001100 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1101 if (pin)
1102 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001103 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1104 if (pin)
1105 return pin->cfg;
1106 return 0;
1107}
1108EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
1109
Takashi Iwaid7fdc002013-01-10 08:38:04 +01001110/* remember the current pinctl target value */
1111int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
1112 unsigned int val)
1113{
1114 struct hda_pincfg *pin;
1115
1116 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1117 if (!pin)
1118 return -EINVAL;
1119 pin->target = val;
1120 return 0;
1121}
1122EXPORT_SYMBOL_HDA(snd_hda_codec_set_pin_target);
1123
1124/* return the current pinctl target value */
1125int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
1126{
1127 struct hda_pincfg *pin;
1128
1129 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1130 if (!pin)
1131 return 0;
1132 return pin->target;
1133}
1134EXPORT_SYMBOL_HDA(snd_hda_codec_get_pin_target);
1135
Takashi Iwai92ee6162009-12-27 11:18:59 +01001136/**
1137 * snd_hda_shutup_pins - Shut up all pins
1138 * @codec: the HDA codec
1139 *
1140 * Clear all pin controls to shup up before suspend for avoiding click noise.
1141 * The controls aren't cached so that they can be resumed properly.
1142 */
1143void snd_hda_shutup_pins(struct hda_codec *codec)
1144{
1145 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001146 /* don't shut up pins when unloading the driver; otherwise it breaks
1147 * the default pin setup at the next load of the driver
1148 */
1149 if (codec->bus->shutdown)
1150 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001151 for (i = 0; i < codec->init_pins.used; i++) {
1152 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1153 /* use read here for syncing after issuing each verb */
1154 snd_hda_codec_read(codec, pin->nid, 0,
1155 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1156 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001157 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001158}
1159EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
1160
Takashi Iwai2a439522011-07-26 09:52:50 +02001161#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001162/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1163static void restore_shutup_pins(struct hda_codec *codec)
1164{
1165 int i;
1166 if (!codec->pins_shutup)
1167 return;
1168 if (codec->bus->shutdown)
1169 return;
1170 for (i = 0; i < codec->init_pins.used; i++) {
1171 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1172 snd_hda_codec_write(codec, pin->nid, 0,
1173 AC_VERB_SET_PIN_WIDGET_CONTROL,
1174 pin->ctrl);
1175 }
1176 codec->pins_shutup = 0;
1177}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001178#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001179
David Henningsson26a6cb62012-10-09 15:04:21 +02001180static void hda_jackpoll_work(struct work_struct *work)
1181{
1182 struct hda_codec *codec =
1183 container_of(work, struct hda_codec, jackpoll_work.work);
1184 if (!codec->jackpoll_interval)
1185 return;
1186
1187 snd_hda_jack_set_dirty_all(codec);
1188 snd_hda_jack_poll_all(codec);
1189 queue_delayed_work(codec->bus->workq, &codec->jackpoll_work,
1190 codec->jackpoll_interval);
1191}
1192
Takashi Iwai01751f52007-08-10 16:59:39 +02001193static void init_hda_cache(struct hda_cache_rec *cache,
1194 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001195static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001196
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001197/* release all pincfg lists */
1198static void free_init_pincfgs(struct hda_codec *codec)
Takashi Iwai3be14142009-02-20 14:11:16 +01001199{
Takashi Iwai346ff702009-02-23 09:42:57 +01001200 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001201#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001202 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001203#endif
Takashi Iwai3be14142009-02-20 14:11:16 +01001204 snd_array_free(&codec->init_pins);
1205}
1206
Takashi Iwai54d17402005-11-21 16:33:22 +01001207/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001208 * audio-converter setup caches
1209 */
1210struct hda_cvt_setup {
1211 hda_nid_t nid;
1212 u8 stream_tag;
1213 u8 channel_id;
1214 u16 format_id;
1215 unsigned char active; /* cvt is currently used */
1216 unsigned char dirty; /* setups should be cleared */
1217};
1218
1219/* get or create a cache entry for the given audio converter NID */
1220static struct hda_cvt_setup *
1221get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1222{
1223 struct hda_cvt_setup *p;
1224 int i;
1225
1226 for (i = 0; i < codec->cvt_setups.used; i++) {
1227 p = snd_array_elem(&codec->cvt_setups, i);
1228 if (p->nid == nid)
1229 return p;
1230 }
1231 p = snd_array_new(&codec->cvt_setups);
1232 if (p)
1233 p->nid = nid;
1234 return p;
1235}
1236
1237/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 * codec destructor
1239 */
1240static void snd_hda_codec_free(struct hda_codec *codec)
1241{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001242 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 return;
David Henningsson26a6cb62012-10-09 15:04:21 +02001244 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai59cad162012-06-26 15:00:20 +02001245 snd_hda_jack_tbl_clear(codec);
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001246 free_init_pincfgs(codec);
Takashi Iwai83012a72012-08-24 18:38:08 +02001247#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02001248 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001249 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001250#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001252 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001253 snd_array_free(&codec->nids);
Takashi Iwai59cad162012-06-26 15:00:20 +02001254 snd_array_free(&codec->cvt_setups);
Stephen Warren7c935972011-06-01 11:14:17 -06001255 snd_array_free(&codec->spdif_out);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001256 remove_conn_list(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 codec->bus->caddr_tbl[codec->addr] = NULL;
1258 if (codec->patch_ops.free)
1259 codec->patch_ops.free(codec);
Takashi Iwai83012a72012-08-24 18:38:08 +02001260#ifdef CONFIG_PM
Takashi Iwai08fa20a2012-08-31 07:46:56 -07001261 if (!codec->pm_down_notified) /* cancel leftover refcounts */
Takashi Iwai68467f52012-08-28 09:14:29 -07001262 hda_call_pm_notify(codec->bus, false);
1263#endif
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001264 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +02001265 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001266 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001267 kfree(codec->vendor_name);
1268 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001269 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001270 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 kfree(codec);
1272}
1273
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001274static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec,
1275 hda_nid_t fg, unsigned int power_state);
1276
Takashi Iwaid8193872012-08-31 07:54:38 -07001277static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001278 unsigned int power_state);
1279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280/**
1281 * snd_hda_codec_new - create a HDA codec
1282 * @bus: the bus to assign
1283 * @codec_addr: the codec address
1284 * @codecp: the pointer to store the generated codec
1285 *
1286 * Returns 0 if successful, or a negative error code.
1287 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001288int snd_hda_codec_new(struct hda_bus *bus,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001289 unsigned int codec_addr,
1290 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291{
1292 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001293 char component[31];
Takashi Iwaid8193872012-08-31 07:54:38 -07001294 hda_nid_t fg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 int err;
1296
Takashi Iwaida3cec32008-08-08 17:12:14 +02001297 if (snd_BUG_ON(!bus))
1298 return -EINVAL;
1299 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1300 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001303 snd_printk(KERN_ERR "hda_codec: "
1304 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 return -EBUSY;
1306 }
1307
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001308 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 if (codec == NULL) {
1310 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1311 return -ENOMEM;
1312 }
1313
1314 codec->bus = bus;
1315 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001316 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001317 mutex_init(&codec->control_mutex);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001318 mutex_init(&codec->hash_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001319 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001320 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001321 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1322 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001323 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001324 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001325 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Stephen Warren7c935972011-06-01 11:14:17 -06001326 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai361dab32012-05-09 14:35:27 +02001327 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01001328 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001329 INIT_LIST_HEAD(&codec->conn_list);
1330
David Henningsson26a6cb62012-10-09 15:04:21 +02001331 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
Takashi Iwai83012a72012-08-24 18:38:08 +02001333#ifdef CONFIG_PM
Takashi Iwai5536c6d2012-05-08 17:08:10 +02001334 spin_lock_init(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001335 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1336 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1337 * the caller has to power down appropriatley after initialization
1338 * phase.
1339 */
1340 hda_keep_power_on(codec);
Takashi Iwai68467f52012-08-28 09:14:29 -07001341 hda_call_pm_notify(bus, true);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001342#endif
1343
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001344 if (codec->bus->modelname) {
1345 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1346 if (!codec->modelname) {
1347 snd_hda_codec_free(codec);
1348 return -ENODEV;
1349 }
1350 }
1351
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 list_add_tail(&codec->list, &bus->codec_list);
1353 bus->caddr_tbl[codec_addr] = codec;
1354
Takashi Iwai0ba21762007-04-16 11:29:14 +02001355 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1356 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001357 if (codec->vendor_id == -1)
1358 /* read again, hopefully the access method was corrected
1359 * in the last read...
1360 */
1361 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1362 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001363 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1364 AC_PAR_SUBSYSTEM_ID);
1365 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1366 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001368 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001369 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001370 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001371 err = -ENODEV;
1372 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 }
1374
Takashi Iwaid8193872012-08-31 07:54:38 -07001375 fg = codec->afg ? codec->afg : codec->mfg;
1376 err = read_widget_caps(codec, fg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001377 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001378 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001379 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001380 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001381 err = read_pin_defaults(codec);
1382 if (err < 0)
1383 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001384
Takashi Iwai0ba21762007-04-16 11:29:14 +02001385 if (!codec->subsystem_id) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001386 codec->subsystem_id =
Takashi Iwaid8193872012-08-31 07:54:38 -07001387 snd_hda_codec_read(codec, fg, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001388 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001389 }
1390
Takashi Iwai83012a72012-08-24 18:38:08 +02001391#ifdef CONFIG_PM
Takashi Iwaid8193872012-08-31 07:54:38 -07001392 codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg,
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001393 AC_PWRST_CLKSTOP);
1394 if (!codec->d3_stop_clk)
1395 bus->power_keep_link_on = 1;
Mengdong Lin5d6147f2012-08-24 12:06:30 +08001396#endif
Takashi Iwaid8193872012-08-31 07:54:38 -07001397 codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
Takashi Iwai983f6b92012-08-28 09:18:01 -07001398 AC_PWRST_EPSS);
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001399
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001400 /* power-up all before initialization */
Takashi Iwaid8193872012-08-31 07:54:38 -07001401 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001402
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001403 snd_hda_codec_proc_new(codec);
1404
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001405 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001406
1407 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1408 codec->subsystem_id, codec->revision_id);
1409 snd_component_add(codec->bus->card, component);
1410
1411 if (codecp)
1412 *codecp = codec;
1413 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001414
1415 error:
1416 snd_hda_codec_free(codec);
1417 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001418}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001419EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001420
Takashi Iwaid5191e52009-11-16 14:58:17 +01001421/**
1422 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1423 * @codec: the HDA codec
1424 *
1425 * Start parsing of the given codec tree and (re-)initialize the whole
1426 * patch instance.
1427 *
1428 * Returns 0 if successful or a negative error code.
1429 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001430int snd_hda_codec_configure(struct hda_codec *codec)
1431{
1432 int err;
1433
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001434 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001435 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001436 err = get_codec_name(codec);
1437 if (err < 0)
1438 return err;
1439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
Takashi Iwai82467612007-07-27 19:15:54 +02001441 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001443 goto patched;
1444 }
Takashi Iwai82467612007-07-27 19:15:54 +02001445 if (codec->preset && codec->preset->patch) {
1446 err = codec->preset->patch(codec);
1447 goto patched;
1448 }
1449
1450 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001451 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001452 if (err < 0)
1453 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001454
1455 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001456 if (!err && codec->patch_ops.unsol_event)
1457 err = init_unsol_queue(codec->bus);
Takashi Iwaif62faed2009-12-23 09:27:51 +01001458 /* audio codec should override the mixer name */
1459 if (!err && (codec->afg || !*codec->bus->card->mixername))
1460 snprintf(codec->bus->card->mixername,
1461 sizeof(codec->bus->card->mixername),
1462 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001463 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001465EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
Takashi Iwaied360812012-08-08 17:12:52 +02001467/* update the stream-id if changed */
1468static void update_pcm_stream_id(struct hda_codec *codec,
1469 struct hda_cvt_setup *p, hda_nid_t nid,
1470 u32 stream_tag, int channel_id)
1471{
1472 unsigned int oldval, newval;
1473
1474 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1475 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1476 newval = (stream_tag << 4) | channel_id;
1477 if (oldval != newval)
1478 snd_hda_codec_write(codec, nid, 0,
1479 AC_VERB_SET_CHANNEL_STREAMID,
1480 newval);
1481 p->stream_tag = stream_tag;
1482 p->channel_id = channel_id;
1483 }
1484}
1485
1486/* update the format-id if changed */
1487static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1488 hda_nid_t nid, int format)
1489{
1490 unsigned int oldval;
1491
1492 if (p->format_id != format) {
1493 oldval = snd_hda_codec_read(codec, nid, 0,
1494 AC_VERB_GET_STREAM_FORMAT, 0);
1495 if (oldval != format) {
1496 msleep(1);
1497 snd_hda_codec_write(codec, nid, 0,
1498 AC_VERB_SET_STREAM_FORMAT,
1499 format);
1500 }
1501 p->format_id = format;
1502 }
1503}
1504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505/**
1506 * snd_hda_codec_setup_stream - set up the codec for streaming
1507 * @codec: the CODEC to set up
1508 * @nid: the NID to set up
1509 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1510 * @channel_id: channel id to pass, zero based.
1511 * @format: stream format.
1512 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001513void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1514 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 int channel_id, int format)
1516{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001517 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001518 struct hda_cvt_setup *p;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001519 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001520 int i;
1521
Takashi Iwai0ba21762007-04-16 11:29:14 +02001522 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001523 return;
1524
Takashi Iwai0ba21762007-04-16 11:29:14 +02001525 snd_printdd("hda_codec_setup_stream: "
1526 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001528 p = get_hda_cvt_setup(codec, nid);
Takashi Iwaiaffdb622013-01-07 15:36:45 +01001529 if (!p || p->active)
Takashi Iwaieb541332010-08-06 13:48:11 +02001530 return;
Takashi Iwaied360812012-08-08 17:12:52 +02001531
1532 if (codec->pcm_format_first)
1533 update_pcm_format(codec, p, nid, format);
1534 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1535 if (!codec->pcm_format_first)
1536 update_pcm_format(codec, p, nid, format);
1537
Takashi Iwaieb541332010-08-06 13:48:11 +02001538 p->active = 1;
1539 p->dirty = 0;
1540
1541 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001542 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001543 list_for_each_entry(c, &codec->bus->codec_list, list) {
1544 for (i = 0; i < c->cvt_setups.used; i++) {
1545 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001546 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001547 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001548 p->dirty = 1;
1549 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001552EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
Takashi Iwaif0cea792010-08-13 11:56:53 +02001554static void really_cleanup_stream(struct hda_codec *codec,
1555 struct hda_cvt_setup *q);
1556
Takashi Iwaid5191e52009-11-16 14:58:17 +01001557/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001558 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001559 * @codec: the CODEC to clean up
1560 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001561 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001562 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001563void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1564 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001565{
Takashi Iwaieb541332010-08-06 13:48:11 +02001566 struct hda_cvt_setup *p;
1567
Takashi Iwai888afa12008-03-18 09:57:50 +01001568 if (!nid)
1569 return;
1570
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001571 if (codec->no_sticky_stream)
1572 do_now = 1;
1573
Takashi Iwai888afa12008-03-18 09:57:50 +01001574 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001575 p = get_hda_cvt_setup(codec, nid);
Takashi Iwaiaffdb622013-01-07 15:36:45 +01001576 if (p && p->active) {
Takashi Iwaif0cea792010-08-13 11:56:53 +02001577 /* here we just clear the active flag when do_now isn't set;
1578 * actual clean-ups will be done later in
1579 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1580 */
1581 if (do_now)
1582 really_cleanup_stream(codec, p);
1583 else
1584 p->active = 0;
1585 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001586}
Takashi Iwaif0cea792010-08-13 11:56:53 +02001587EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001588
Takashi Iwaieb541332010-08-06 13:48:11 +02001589static void really_cleanup_stream(struct hda_codec *codec,
1590 struct hda_cvt_setup *q)
1591{
1592 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001593 if (q->stream_tag || q->channel_id)
1594 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1595 if (q->format_id)
1596 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1597);
Takashi Iwaieb541332010-08-06 13:48:11 +02001598 memset(q, 0, sizeof(*q));
1599 q->nid = nid;
1600}
1601
1602/* clean up the all conflicting obsolete streams */
1603static void purify_inactive_streams(struct hda_codec *codec)
1604{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001605 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001606 int i;
1607
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001608 list_for_each_entry(c, &codec->bus->codec_list, list) {
1609 for (i = 0; i < c->cvt_setups.used; i++) {
1610 struct hda_cvt_setup *p;
1611 p = snd_array_elem(&c->cvt_setups, i);
1612 if (p->dirty)
1613 really_cleanup_stream(c, p);
1614 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001615 }
1616}
1617
Takashi Iwai2a439522011-07-26 09:52:50 +02001618#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001619/* clean up all streams; called from suspend */
1620static void hda_cleanup_all_streams(struct hda_codec *codec)
1621{
1622 int i;
1623
1624 for (i = 0; i < codec->cvt_setups.used; i++) {
1625 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1626 if (p->stream_tag)
1627 really_cleanup_stream(codec, p);
1628 }
1629}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001630#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001631
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632/*
1633 * amp access functions
1634 */
1635
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001636/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001637#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001638#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001639#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1640#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001642#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
1644/* initialize the hash table */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001645static void init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001646 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647{
Takashi Iwai01751f52007-08-10 16:59:39 +02001648 memset(cache, 0, sizeof(*cache));
1649 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001650 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001651}
1652
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001653static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001654{
Takashi Iwai603c4012008-07-30 15:01:44 +02001655 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656}
1657
1658/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a542010-03-30 18:03:44 +02001659static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660{
Takashi Iwai01751f52007-08-10 16:59:39 +02001661 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1662 u16 cur = cache->hash[idx];
1663 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
1665 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001666 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 if (info->key == key)
1668 return info;
1669 cur = info->next;
1670 }
Takashi Iwaia68d5a542010-03-30 18:03:44 +02001671 return NULL;
1672}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
Takashi Iwaia68d5a542010-03-30 18:03:44 +02001674/* query the hash. allocate an entry if not found. */
1675static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1676 u32 key)
1677{
1678 struct hda_cache_head *info = get_hash(cache, key);
1679 if (!info) {
1680 u16 idx, cur;
1681 /* add a new hash entry */
1682 info = snd_array_new(&cache->buf);
1683 if (!info)
1684 return NULL;
1685 cur = snd_array_index(&cache->buf, info);
1686 info->key = key;
1687 info->val = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001688 info->dirty = 0;
Takashi Iwaia68d5a542010-03-30 18:03:44 +02001689 idx = key % (u16)ARRAY_SIZE(cache->hash);
1690 info->next = cache->hash[idx];
1691 cache->hash[idx] = cur;
1692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 return info;
1694}
1695
Takashi Iwai01751f52007-08-10 16:59:39 +02001696/* query and allocate an amp hash entry */
1697static inline struct hda_amp_info *
1698get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1699{
1700 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1701}
1702
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001703/* overwrite the value with the key in the caps hash */
1704static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1705{
1706 struct hda_amp_info *info;
1707
1708 mutex_lock(&codec->hash_mutex);
1709 info = get_alloc_amp_hash(codec, key);
1710 if (!info) {
1711 mutex_unlock(&codec->hash_mutex);
1712 return -EINVAL;
1713 }
1714 info->amp_caps = val;
1715 info->head.val |= INFO_AMP_CAPS;
1716 mutex_unlock(&codec->hash_mutex);
1717 return 0;
1718}
1719
1720/* query the value from the caps hash; if not found, fetch the current
1721 * value from the given function and store in the hash
1722 */
1723static unsigned int
1724query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1725 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1726{
1727 struct hda_amp_info *info;
1728 unsigned int val;
1729
1730 mutex_lock(&codec->hash_mutex);
1731 info = get_alloc_amp_hash(codec, key);
1732 if (!info) {
1733 mutex_unlock(&codec->hash_mutex);
1734 return 0;
1735 }
1736 if (!(info->head.val & INFO_AMP_CAPS)) {
1737 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1738 val = func(codec, nid, dir);
1739 write_caps_hash(codec, key, val);
1740 } else {
1741 val = info->amp_caps;
1742 mutex_unlock(&codec->hash_mutex);
1743 }
1744 return val;
1745}
1746
1747static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1748 int direction)
1749{
1750 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1751 nid = codec->afg;
1752 return snd_hda_param_read(codec, nid,
1753 direction == HDA_OUTPUT ?
1754 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1755}
1756
Takashi Iwaid5191e52009-11-16 14:58:17 +01001757/**
1758 * query_amp_caps - query AMP capabilities
1759 * @codec: the HD-auio codec
1760 * @nid: the NID to query
1761 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1762 *
1763 * Query AMP capabilities for the given widget and direction.
1764 * Returns the obtained capability bits.
1765 *
1766 * When cap bits have been already read, this doesn't read again but
1767 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001769u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001771 return query_caps_hash(codec, nid, direction,
1772 HDA_HASH_KEY(nid, direction, 0),
1773 read_amp_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001775EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
Takashi Iwaid5191e52009-11-16 14:58:17 +01001777/**
1778 * snd_hda_override_amp_caps - Override the AMP capabilities
1779 * @codec: the CODEC to clean up
1780 * @nid: the NID to clean up
1781 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1782 * @caps: the capability bits to set
1783 *
1784 * Override the cached AMP caps bits value by the given one.
1785 * This function is useful if the driver needs to adjust the AMP ranges,
1786 * e.g. limit to 0dB, etc.
1787 *
1788 * Returns zero if successful or a negative error code.
1789 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001790int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1791 unsigned int caps)
1792{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001793 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001794}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001795EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001796
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001797static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
1798 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001799{
1800 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1801}
1802
Takashi Iwaid5191e52009-11-16 14:58:17 +01001803/**
1804 * snd_hda_query_pin_caps - Query PIN capabilities
1805 * @codec: the HD-auio codec
1806 * @nid: the NID to query
1807 *
1808 * Query PIN capabilities for the given widget.
1809 * Returns the obtained capability bits.
1810 *
1811 * When cap bits have been already read, this doesn't read again but
1812 * returns the cached value.
1813 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001814u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1815{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001816 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001817 read_pin_cap);
1818}
Takashi Iwai1327a322009-03-23 13:07:47 +01001819EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1820
Wu Fengguang864f92b2009-11-18 12:38:02 +08001821/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02001822 * snd_hda_override_pin_caps - Override the pin capabilities
1823 * @codec: the CODEC
1824 * @nid: the NID to override
1825 * @caps: the capability bits to set
1826 *
1827 * Override the cached PIN capabilitiy bits value by the given one.
1828 *
1829 * Returns zero if successful or a negative error code.
1830 */
1831int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1832 unsigned int caps)
1833{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001834 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02001835}
1836EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps);
1837
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001838/* read or sync the hash value with the current value;
1839 * call within hash_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 */
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001841static struct hda_amp_info *
1842update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
Takashi Iwai280e57d2012-12-14 10:32:21 +01001843 int direction, int index, bool init_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001845 struct hda_amp_info *info;
1846 unsigned int parm, val = 0;
1847 bool val_read = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001849 retry:
1850 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1851 if (!info)
1852 return NULL;
1853 if (!(info->head.val & INFO_AMP_VOL(ch))) {
1854 if (!val_read) {
1855 mutex_unlock(&codec->hash_mutex);
1856 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1857 parm |= direction == HDA_OUTPUT ?
1858 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1859 parm |= index;
1860 val = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001861 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001862 val &= 0xff;
1863 val_read = true;
1864 mutex_lock(&codec->hash_mutex);
1865 goto retry;
1866 }
1867 info->vol[ch] = val;
1868 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001869 } else if (init_only)
1870 return NULL;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001871 return info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872}
1873
1874/*
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001875 * write the current volume in info to the h/w
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 */
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001877static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001878 hda_nid_t nid, int ch, int direction, int index,
1879 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880{
1881 u32 parm;
1882
1883 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1884 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1885 parm |= index << AC_AMP_SET_INDEX_SHIFT;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001886 if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) &&
1887 (amp_caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwai38681372012-02-27 15:00:58 +01001888 ; /* set the zero value as a fake mute */
1889 else
1890 parm |= val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1892}
1893
Takashi Iwaid5191e52009-11-16 14:58:17 +01001894/**
1895 * snd_hda_codec_amp_read - Read AMP value
1896 * @codec: HD-audio codec
1897 * @nid: NID to read the AMP value
1898 * @ch: channel (left=0 or right=1)
1899 * @direction: #HDA_INPUT or #HDA_OUTPUT
1900 * @index: the index value (only for input direction)
1901 *
1902 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 */
Takashi Iwai834be882006-03-01 14:16:17 +01001904int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1905 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001907 struct hda_amp_info *info;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001908 unsigned int val = 0;
1909
1910 mutex_lock(&codec->hash_mutex);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001911 info = update_amp_hash(codec, nid, ch, direction, index, false);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001912 if (info)
1913 val = info->vol[ch];
1914 mutex_unlock(&codec->hash_mutex);
1915 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001917EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
Takashi Iwai280e57d2012-12-14 10:32:21 +01001919static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1920 int direction, int idx, int mask, int val,
1921 bool init_only)
1922{
1923 struct hda_amp_info *info;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001924 unsigned int caps;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01001925 unsigned int cache_only;
Takashi Iwai280e57d2012-12-14 10:32:21 +01001926
1927 if (snd_BUG_ON(mask & ~0xff))
1928 mask &= 0xff;
1929 val &= mask;
1930
1931 mutex_lock(&codec->hash_mutex);
1932 info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
1933 if (!info) {
1934 mutex_unlock(&codec->hash_mutex);
1935 return 0;
1936 }
1937 val |= info->vol[ch] & ~mask;
1938 if (info->vol[ch] == val) {
1939 mutex_unlock(&codec->hash_mutex);
1940 return 0;
1941 }
1942 info->vol[ch] = val;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01001943 cache_only = info->head.dirty = codec->cached_write;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001944 caps = info->amp_caps;
Takashi Iwai280e57d2012-12-14 10:32:21 +01001945 mutex_unlock(&codec->hash_mutex);
Takashi Iwaide1e37b2012-12-20 11:00:21 +01001946 if (!cache_only)
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001947 put_vol_mute(codec, caps, nid, ch, direction, idx, val);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001948 return 1;
1949}
1950
Takashi Iwaid5191e52009-11-16 14:58:17 +01001951/**
1952 * snd_hda_codec_amp_update - update the AMP value
1953 * @codec: HD-audio codec
1954 * @nid: NID to read the AMP value
1955 * @ch: channel (left=0 or right=1)
1956 * @direction: #HDA_INPUT or #HDA_OUTPUT
1957 * @idx: the index value (only for input direction)
1958 * @mask: bit mask to set
1959 * @val: the bits value to set
1960 *
1961 * Update the AMP value with a bit mask.
1962 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001963 */
Takashi Iwai834be882006-03-01 14:16:17 +01001964int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1965 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966{
Takashi Iwai280e57d2012-12-14 10:32:21 +01001967 return codec_amp_update(codec, nid, ch, direction, idx, mask, val, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001969EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
Takashi Iwaid5191e52009-11-16 14:58:17 +01001971/**
1972 * snd_hda_codec_amp_stereo - update the AMP stereo values
1973 * @codec: HD-audio codec
1974 * @nid: NID to read the AMP value
1975 * @direction: #HDA_INPUT or #HDA_OUTPUT
1976 * @idx: the index value (only for input direction)
1977 * @mask: bit mask to set
1978 * @val: the bits value to set
1979 *
1980 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1981 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001982 */
1983int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1984 int direction, int idx, int mask, int val)
1985{
1986 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001987
1988 if (snd_BUG_ON(mask & ~0xff))
1989 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001990 for (ch = 0; ch < 2; ch++)
1991 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1992 idx, mask, val);
1993 return ret;
1994}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001995EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001996
Takashi Iwai280e57d2012-12-14 10:32:21 +01001997/* Works like snd_hda_codec_amp_update() but it writes the value only at
1998 * the first access. If the amp was already initialized / updated beforehand,
1999 * this does nothing.
2000 */
2001int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
2002 int dir, int idx, int mask, int val)
2003{
2004 return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true);
2005}
2006EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init);
2007
2008int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
2009 int dir, int idx, int mask, int val)
2010{
2011 int ch, ret = 0;
2012
2013 if (snd_BUG_ON(mask & ~0xff))
2014 mask &= 0xff;
2015 for (ch = 0; ch < 2; ch++)
2016 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
2017 idx, mask, val);
2018 return ret;
2019}
2020EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init_stereo);
2021
Takashi Iwaid5191e52009-11-16 14:58:17 +01002022/**
2023 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
2024 * @codec: HD-audio codec
2025 *
2026 * Resume the all amp commands from the cache.
2027 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002028void snd_hda_codec_resume_amp(struct hda_codec *codec)
2029{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002030 int i;
2031
Takashi Iwaic370dd62012-12-13 18:30:04 +01002032 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01002033 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002034 for (i = 0; i < codec->amp_cache.buf.used; i++) {
2035 struct hda_amp_info *buffer;
2036 u32 key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002037 hda_nid_t nid;
2038 unsigned int idx, dir, ch;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002039 struct hda_amp_info info;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002040
2041 buffer = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwai8565f052012-12-20 12:54:18 +01002042 if (!buffer->head.dirty)
2043 continue;
2044 buffer->head.dirty = 0;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002045 info = *buffer;
2046 key = info.head.key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002047 if (!key)
2048 continue;
2049 nid = key & 0xff;
2050 idx = (key >> 16) & 0xff;
2051 dir = (key >> 24) & 0xff;
2052 for (ch = 0; ch < 2; ch++) {
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002053 if (!(info.head.val & INFO_AMP_VOL(ch)))
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002054 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002055 mutex_unlock(&codec->hash_mutex);
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002056 put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx,
2057 info.vol[ch]);
Takashi Iwaic370dd62012-12-13 18:30:04 +01002058 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002059 }
2060 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01002061 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002062}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002063EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002065static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
2066 unsigned int ofs)
2067{
2068 u32 caps = query_amp_caps(codec, nid, dir);
2069 /* get num steps */
2070 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2071 if (ofs < caps)
2072 caps -= ofs;
2073 return caps;
2074}
2075
Takashi Iwaid5191e52009-11-16 14:58:17 +01002076/**
2077 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
2078 *
2079 * The control element is supposed to have the private_value field
2080 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2081 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002082int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
2083 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084{
2085 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2086 u16 nid = get_amp_nid(kcontrol);
2087 u8 chs = get_amp_channels(kcontrol);
2088 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002089 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002091 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2092 uinfo->count = chs == 3 ? 2 : 1;
2093 uinfo->value.integer.min = 0;
2094 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
2095 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002096 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01002097 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
2098 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 return -EINVAL;
2100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 return 0;
2102}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002103EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002105
2106static inline unsigned int
2107read_amp_value(struct hda_codec *codec, hda_nid_t nid,
2108 int ch, int dir, int idx, unsigned int ofs)
2109{
2110 unsigned int val;
2111 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
2112 val &= HDA_AMP_VOLMASK;
2113 if (val >= ofs)
2114 val -= ofs;
2115 else
2116 val = 0;
2117 return val;
2118}
2119
2120static inline int
2121update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2122 int ch, int dir, int idx, unsigned int ofs,
2123 unsigned int val)
2124{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002125 unsigned int maxval;
2126
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002127 if (val > 0)
2128 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02002129 /* ofs = 0: raw max value */
2130 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002131 if (val > maxval)
2132 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002133 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
2134 HDA_AMP_VOLMASK, val);
2135}
2136
Takashi Iwaid5191e52009-11-16 14:58:17 +01002137/**
2138 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
2139 *
2140 * The control element is supposed to have the private_value field
2141 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2142 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002143int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2144 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145{
2146 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2147 hda_nid_t nid = get_amp_nid(kcontrol);
2148 int chs = get_amp_channels(kcontrol);
2149 int dir = get_amp_direction(kcontrol);
2150 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002151 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 long *valp = ucontrol->value.integer.value;
2153
2154 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002155 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002157 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 return 0;
2159}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002160EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
Takashi Iwaid5191e52009-11-16 14:58:17 +01002162/**
2163 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2164 *
2165 * The control element is supposed to have the private_value field
2166 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2167 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002168int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2169 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170{
2171 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2172 hda_nid_t nid = get_amp_nid(kcontrol);
2173 int chs = get_amp_channels(kcontrol);
2174 int dir = get_amp_direction(kcontrol);
2175 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002176 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 long *valp = ucontrol->value.integer.value;
2178 int change = 0;
2179
Takashi Iwaicb53c622007-08-10 17:21:45 +02002180 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002181 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002182 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002183 valp++;
2184 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002185 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002186 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002187 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 return change;
2189}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002190EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
Takashi Iwaid5191e52009-11-16 14:58:17 +01002192/**
2193 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2194 *
2195 * The control element is supposed to have the private_value field
2196 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2197 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002198int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2199 unsigned int size, unsigned int __user *_tlv)
2200{
2201 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2202 hda_nid_t nid = get_amp_nid(kcontrol);
2203 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002204 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002205 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002206 u32 caps, val1, val2;
2207
2208 if (size < 4 * sizeof(unsigned int))
2209 return -ENOMEM;
2210 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002211 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2212 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002213 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002214 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002215 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01002216 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02002217 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002218 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2219 return -EFAULT;
2220 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2221 return -EFAULT;
2222 if (put_user(val1, _tlv + 2))
2223 return -EFAULT;
2224 if (put_user(val2, _tlv + 3))
2225 return -EFAULT;
2226 return 0;
2227}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002228EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002229
Takashi Iwaid5191e52009-11-16 14:58:17 +01002230/**
2231 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2232 * @codec: HD-audio codec
2233 * @nid: NID of a reference widget
2234 * @dir: #HDA_INPUT or #HDA_OUTPUT
2235 * @tlv: TLV data to be stored, at least 4 elements
2236 *
2237 * Set (static) TLV data for a virtual master volume using the AMP caps
2238 * obtained from the reference NID.
2239 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002240 */
2241void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2242 unsigned int *tlv)
2243{
2244 u32 caps;
2245 int nums, step;
2246
2247 caps = query_amp_caps(codec, nid, dir);
2248 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2249 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2250 step = (step + 1) * 25;
2251 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2252 tlv[1] = 2 * sizeof(unsigned int);
2253 tlv[2] = -nums * step;
2254 tlv[3] = step;
2255}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002256EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002257
2258/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002259static struct snd_kcontrol *
Takashi Iwaidcda5802012-10-12 17:24:51 +02002260find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002261{
2262 struct snd_ctl_elem_id id;
2263 memset(&id, 0, sizeof(id));
2264 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002265 id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01002266 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02002267 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2268 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002269 strcpy(id.name, name);
2270 return snd_ctl_find_id(codec->bus->card, &id);
2271}
2272
Takashi Iwaid5191e52009-11-16 14:58:17 +01002273/**
2274 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2275 * @codec: HD-audio codec
2276 * @name: ctl id name string
2277 *
2278 * Get the control element with the given id string and IFACE_MIXER.
2279 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002280struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2281 const char *name)
2282{
Takashi Iwaidcda5802012-10-12 17:24:51 +02002283 return find_mixer_ctl(codec, name, 0, 0);
Takashi Iwai09f99702008-02-04 12:31:13 +01002284}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002285EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002286
Takashi Iwaidcda5802012-10-12 17:24:51 +02002287static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
2288 int dev)
Takashi Iwai1afe2062010-12-23 10:17:52 +01002289{
2290 int idx;
2291 for (idx = 0; idx < 16; idx++) { /* 16 ctlrs should be large enough */
Takashi Iwaidcda5802012-10-12 17:24:51 +02002292 if (!find_mixer_ctl(codec, name, dev, idx))
Takashi Iwai1afe2062010-12-23 10:17:52 +01002293 return idx;
2294 }
2295 return -EBUSY;
2296}
2297
Takashi Iwaid5191e52009-11-16 14:58:17 +01002298/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002299 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002300 * @codec: HD-audio codec
2301 * @nid: corresponding NID (optional)
2302 * @kctl: the control element to assign
2303 *
2304 * Add the given control element to an array inside the codec instance.
2305 * All control elements belonging to a codec are supposed to be added
2306 * by this function so that a proper clean-up works at the free or
2307 * reconfiguration time.
2308 *
2309 * If non-zero @nid is passed, the NID is assigned to the control element.
2310 * The assignment is shown in the codec proc file.
2311 *
2312 * snd_hda_ctl_add() checks the control subdev id field whether
2313 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002314 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2315 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002316 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002317int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2318 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002319{
2320 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002321 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002322 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002323
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002324 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002325 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002326 if (nid == 0)
2327 nid = get_amp_nid_(kctl->private_value);
2328 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002329 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2330 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002331 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002332 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002333 err = snd_ctl_add(codec->bus->card, kctl);
2334 if (err < 0)
2335 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002336 item = snd_array_new(&codec->mixers);
2337 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002338 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002339 item->kctl = kctl;
2340 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002341 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002342 return 0;
2343}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002344EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002345
Takashi Iwaid5191e52009-11-16 14:58:17 +01002346/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002347 * snd_hda_add_nid - Assign a NID to a control element
2348 * @codec: HD-audio codec
2349 * @nid: corresponding NID (optional)
2350 * @kctl: the control element to assign
2351 * @index: index to kctl
2352 *
2353 * Add the given control element to an array inside the codec instance.
2354 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2355 * NID:KCTL mapping - for example "Capture Source" selector.
2356 */
2357int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2358 unsigned int index, hda_nid_t nid)
2359{
2360 struct hda_nid_item *item;
2361
2362 if (nid > 0) {
2363 item = snd_array_new(&codec->nids);
2364 if (!item)
2365 return -ENOMEM;
2366 item->kctl = kctl;
2367 item->index = index;
2368 item->nid = nid;
2369 return 0;
2370 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01002371 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2372 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002373 return -EINVAL;
2374}
2375EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2376
2377/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002378 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2379 * @codec: HD-audio codec
2380 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002381void snd_hda_ctls_clear(struct hda_codec *codec)
2382{
2383 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002384 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002385 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002386 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002387 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002388 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002389}
2390
Takashi Iwaia65d6292009-02-23 16:57:04 +01002391/* pseudo device locking
2392 * toggle card->shutdown to allow/disallow the device access (as a hack)
2393 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002394int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002395{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002396 struct snd_card *card = bus->card;
2397 struct hda_codec *codec;
2398
Takashi Iwaia65d6292009-02-23 16:57:04 +01002399 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002400 if (card->shutdown)
2401 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002402 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002403 if (!list_empty(&card->ctl_files))
2404 goto err_clear;
2405
2406 list_for_each_entry(codec, &bus->codec_list, list) {
2407 int pcm;
2408 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2409 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2410 if (!cpcm->pcm)
2411 continue;
2412 if (cpcm->pcm->streams[0].substream_opened ||
2413 cpcm->pcm->streams[1].substream_opened)
2414 goto err_clear;
2415 }
2416 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01002417 spin_unlock(&card->files_lock);
2418 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002419
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002420 err_clear:
2421 card->shutdown = 0;
2422 err_unlock:
2423 spin_unlock(&card->files_lock);
2424 return -EINVAL;
2425}
2426EXPORT_SYMBOL_HDA(snd_hda_lock_devices);
2427
2428void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002429{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002430 struct snd_card *card = bus->card;
2431
2432 card = bus->card;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002433 spin_lock(&card->files_lock);
2434 card->shutdown = 0;
2435 spin_unlock(&card->files_lock);
2436}
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002437EXPORT_SYMBOL_HDA(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002438
Takashi Iwaid5191e52009-11-16 14:58:17 +01002439/**
2440 * snd_hda_codec_reset - Clear all objects assigned to the codec
2441 * @codec: HD-audio codec
2442 *
2443 * This frees the all PCM and control elements assigned to the codec, and
2444 * clears the caches and restores the pin default configurations.
2445 *
2446 * When a device is being used, it returns -EBSY. If successfully freed,
2447 * returns zero.
2448 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002449int snd_hda_codec_reset(struct hda_codec *codec)
2450{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002451 struct hda_bus *bus = codec->bus;
2452 struct snd_card *card = bus->card;
2453 int i;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002454
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002455 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002456 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002457
2458 /* OK, let it free */
David Henningsson26a6cb62012-10-09 15:04:21 +02002459 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai83012a72012-08-24 18:38:08 +02002460#ifdef CONFIG_PM
Takashi Iwaia2d96e72012-05-09 12:36:22 +02002461 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwai339876d2012-05-08 16:57:12 +02002462 codec->power_on = 0;
2463 codec->power_transition = 0;
2464 codec->power_jiffies = jiffies;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002465 flush_workqueue(bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002466#endif
2467 snd_hda_ctls_clear(codec);
2468 /* relase PCMs */
2469 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002470 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002471 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002472 clear_bit(codec->pcm_info[i].device,
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002473 bus->pcm_dev_bits);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002474 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002475 }
2476 if (codec->patch_ops.free)
2477 codec->patch_ops.free(codec);
Takashi Iwai07dc59f2012-09-10 09:39:31 +02002478 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002479 snd_hda_jack_tbl_clear(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002480 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002481 codec->spec = NULL;
2482 free_hda_cache(&codec->amp_cache);
2483 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002484 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2485 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002486 /* free only driver_pins so that init_pins + user_pins are restored */
2487 snd_array_free(&codec->driver_pins);
Takashi Iwai09a60712012-06-26 15:01:33 +02002488 snd_array_free(&codec->cvt_setups);
2489 snd_array_free(&codec->spdif_out);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01002490 snd_array_free(&codec->verbs);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002491 codec->num_pcms = 0;
2492 codec->pcm_info = NULL;
2493 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002494 codec->slave_dig_outs = NULL;
2495 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002496 module_put(codec->owner);
2497 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002498
2499 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002500 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002501 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002502}
2503
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002504typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2505
2506/* apply the function to all matching slave ctls in the mixer list */
2507static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002508 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002509{
2510 struct hda_nid_item *items;
2511 const char * const *s;
2512 int i, err;
2513
2514 items = codec->mixers.list;
2515 for (i = 0; i < codec->mixers.used; i++) {
2516 struct snd_kcontrol *sctl = items[i].kctl;
2517 if (!sctl || !sctl->id.name ||
2518 sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2519 continue;
2520 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002521 char tmpname[sizeof(sctl->id.name)];
2522 const char *name = *s;
2523 if (suffix) {
2524 snprintf(tmpname, sizeof(tmpname), "%s %s",
2525 name, suffix);
2526 name = tmpname;
2527 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002528 if (!strcmp(sctl->id.name, name)) {
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002529 err = func(data, sctl);
2530 if (err)
2531 return err;
2532 break;
2533 }
2534 }
2535 }
2536 return 0;
2537}
2538
2539static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2540{
2541 return 1;
2542}
2543
Takashi Iwai18478e82012-03-09 17:51:10 +01002544/* guess the value corresponding to 0dB */
2545static int get_kctl_0dB_offset(struct snd_kcontrol *kctl)
2546{
2547 int _tlv[4];
2548 const int *tlv = NULL;
2549 int val = -1;
2550
2551 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2552 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2553 mm_segment_t fs = get_fs();
2554 set_fs(get_ds());
2555 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2556 tlv = _tlv;
2557 set_fs(fs);
2558 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2559 tlv = kctl->tlv.p;
2560 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE)
2561 val = -tlv[2] / tlv[3];
2562 return val;
2563}
2564
2565/* call kctl->put with the given value(s) */
2566static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2567{
2568 struct snd_ctl_elem_value *ucontrol;
2569 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2570 if (!ucontrol)
2571 return -ENOMEM;
2572 ucontrol->value.integer.value[0] = val;
2573 ucontrol->value.integer.value[1] = val;
2574 kctl->put(kctl, ucontrol);
2575 kfree(ucontrol);
2576 return 0;
2577}
2578
2579/* initialize the slave volume with 0dB */
2580static int init_slave_0dB(void *data, struct snd_kcontrol *slave)
2581{
2582 int offset = get_kctl_0dB_offset(slave);
2583 if (offset > 0)
2584 put_kctl_with_value(slave, offset);
2585 return 0;
2586}
2587
2588/* unmute the slave */
2589static int init_slave_unmute(void *data, struct snd_kcontrol *slave)
2590{
2591 return put_kctl_with_value(slave, 1);
2592}
2593
Takashi Iwaid5191e52009-11-16 14:58:17 +01002594/**
2595 * snd_hda_add_vmaster - create a virtual master control and add slaves
2596 * @codec: HD-audio codec
2597 * @name: vmaster control name
2598 * @tlv: TLV data (optional)
2599 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002600 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002601 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002602 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002603 *
2604 * Create a virtual master control with the given name. The TLV data
2605 * must be either NULL or a valid data.
2606 *
2607 * @slaves is a NULL-terminated array of strings, each of which is a
2608 * slave control name. All controls with these names are assigned to
2609 * the new virtual master control.
2610 *
2611 * This function returns zero if successful or a negative error code.
2612 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002613int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002614 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002615 const char *suffix, bool init_slave_vol,
2616 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002617{
2618 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002619 int err;
2620
Takashi Iwai29e58532012-03-12 12:25:03 +01002621 if (ctl_ret)
2622 *ctl_ret = NULL;
2623
Takashi Iwai9322ca52012-02-03 14:28:01 +01002624 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002625 if (err != 1) {
Takashi Iwai2f085542008-02-22 18:43:50 +01002626 snd_printdd("No slave found for %s\n", name);
2627 return 0;
2628 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002629 kctl = snd_ctl_make_virtual_master(name, tlv);
2630 if (!kctl)
2631 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002632 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002633 if (err < 0)
2634 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002635
Takashi Iwai9322ca52012-02-03 14:28:01 +01002636 err = map_slaves(codec, slaves, suffix,
2637 (map_slave_func_t)snd_ctl_add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002638 if (err < 0)
2639 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002640
2641 /* init with master mute & zero volume */
2642 put_kctl_with_value(kctl, 0);
2643 if (init_slave_vol)
2644 map_slaves(codec, slaves, suffix,
2645 tlv ? init_slave_0dB : init_slave_unmute, kctl);
2646
Takashi Iwai29e58532012-03-12 12:25:03 +01002647 if (ctl_ret)
2648 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002649 return 0;
2650}
Takashi Iwai18478e82012-03-09 17:51:10 +01002651EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002652
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002653/*
2654 * mute-LED control using vmaster
2655 */
2656static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2657 struct snd_ctl_elem_info *uinfo)
2658{
2659 static const char * const texts[] = {
David Henningssonc86c2d42013-01-03 14:12:29 +01002660 "On", "Off", "Follow Master"
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002661 };
2662 unsigned int index;
2663
2664 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2665 uinfo->count = 1;
2666 uinfo->value.enumerated.items = 3;
2667 index = uinfo->value.enumerated.item;
2668 if (index >= 3)
2669 index = 2;
2670 strcpy(uinfo->value.enumerated.name, texts[index]);
2671 return 0;
2672}
2673
2674static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2675 struct snd_ctl_elem_value *ucontrol)
2676{
2677 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2678 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2679 return 0;
2680}
2681
2682static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2683 struct snd_ctl_elem_value *ucontrol)
2684{
2685 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2686 unsigned int old_mode = hook->mute_mode;
2687
2688 hook->mute_mode = ucontrol->value.enumerated.item[0];
2689 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2690 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2691 if (old_mode == hook->mute_mode)
2692 return 0;
2693 snd_hda_sync_vmaster_hook(hook);
2694 return 1;
2695}
2696
2697static struct snd_kcontrol_new vmaster_mute_mode = {
2698 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2699 .name = "Mute-LED Mode",
2700 .info = vmaster_mute_mode_info,
2701 .get = vmaster_mute_mode_get,
2702 .put = vmaster_mute_mode_put,
2703};
2704
2705/*
2706 * Add a mute-LED hook with the given vmaster switch kctl
2707 * "Mute-LED Mode" control is automatically created and associated with
2708 * the given hook.
2709 */
2710int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735cb2012-03-13 07:55:10 +01002711 struct hda_vmaster_mute_hook *hook,
2712 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002713{
2714 struct snd_kcontrol *kctl;
2715
2716 if (!hook->hook || !hook->sw_kctl)
2717 return 0;
2718 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2719 hook->codec = codec;
2720 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735cb2012-03-13 07:55:10 +01002721 if (!expose_enum_ctl)
2722 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002723 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2724 if (!kctl)
2725 return -ENOMEM;
2726 return snd_hda_ctl_add(codec, 0, kctl);
2727}
2728EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook);
2729
2730/*
2731 * Call the hook with the current value for synchronization
2732 * Should be called in init callback
2733 */
2734void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2735{
2736 if (!hook->hook || !hook->codec)
2737 return;
2738 switch (hook->mute_mode) {
2739 case HDA_VMUTE_FOLLOW_MASTER:
2740 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2741 break;
2742 default:
2743 hook->hook(hook->codec, hook->mute_mode);
2744 break;
2745 }
2746}
2747EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook);
2748
2749
Takashi Iwaid5191e52009-11-16 14:58:17 +01002750/**
2751 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2752 *
2753 * The control element is supposed to have the private_value field
2754 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2755 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002756int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2757 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758{
2759 int chs = get_amp_channels(kcontrol);
2760
2761 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2762 uinfo->count = chs == 3 ? 2 : 1;
2763 uinfo->value.integer.min = 0;
2764 uinfo->value.integer.max = 1;
2765 return 0;
2766}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002767EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768
Takashi Iwaid5191e52009-11-16 14:58:17 +01002769/**
2770 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2771 *
2772 * The control element is supposed to have the private_value field
2773 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2774 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002775int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2776 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777{
2778 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2779 hda_nid_t nid = get_amp_nid(kcontrol);
2780 int chs = get_amp_channels(kcontrol);
2781 int dir = get_amp_direction(kcontrol);
2782 int idx = get_amp_index(kcontrol);
2783 long *valp = ucontrol->value.integer.value;
2784
2785 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002786 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002787 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002789 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002790 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 return 0;
2792}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002793EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794
Takashi Iwaid5191e52009-11-16 14:58:17 +01002795/**
2796 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2797 *
2798 * The control element is supposed to have the private_value field
2799 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2800 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002801int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2802 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803{
2804 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2805 hda_nid_t nid = get_amp_nid(kcontrol);
2806 int chs = get_amp_channels(kcontrol);
2807 int dir = get_amp_direction(kcontrol);
2808 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 long *valp = ucontrol->value.integer.value;
2810 int change = 0;
2811
Takashi Iwaicb53c622007-08-10 17:21:45 +02002812 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002813 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002814 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002815 HDA_AMP_MUTE,
2816 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002817 valp++;
2818 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002819 if (chs & 2)
2820 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002821 HDA_AMP_MUTE,
2822 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002823 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002824 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 return change;
2826}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002827EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828
2829/*
Takashi Iwai985be542005-11-02 18:26:49 +01002830 * bound volume controls
2831 *
2832 * bind multiple volumes (# indices, from 0)
2833 */
2834
2835#define AMP_VAL_IDX_SHIFT 19
2836#define AMP_VAL_IDX_MASK (0x0f<<19)
2837
Takashi Iwaid5191e52009-11-16 14:58:17 +01002838/**
2839 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2840 *
2841 * The control element is supposed to have the private_value field
2842 * set up via HDA_BIND_MUTE*() macros.
2843 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002844int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2845 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002846{
2847 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2848 unsigned long pval;
2849 int err;
2850
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002851 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002852 pval = kcontrol->private_value;
2853 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2854 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2855 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002856 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002857 return err;
2858}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002859EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002860
Takashi Iwaid5191e52009-11-16 14:58:17 +01002861/**
2862 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2863 *
2864 * The control element is supposed to have the private_value field
2865 * set up via HDA_BIND_MUTE*() macros.
2866 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002867int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2868 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002869{
2870 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2871 unsigned long pval;
2872 int i, indices, err = 0, change = 0;
2873
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002874 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002875 pval = kcontrol->private_value;
2876 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2877 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002878 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2879 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002880 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2881 if (err < 0)
2882 break;
2883 change |= err;
2884 }
2885 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002886 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002887 return err < 0 ? err : change;
2888}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002889EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002890
Takashi Iwaid5191e52009-11-16 14:58:17 +01002891/**
2892 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2893 *
2894 * The control element is supposed to have the private_value field
2895 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002896 */
2897int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2898 struct snd_ctl_elem_info *uinfo)
2899{
2900 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2901 struct hda_bind_ctls *c;
2902 int err;
2903
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002904 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002905 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002906 kcontrol->private_value = *c->values;
2907 err = c->ops->info(kcontrol, uinfo);
2908 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002909 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002910 return err;
2911}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002912EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002913
Takashi Iwaid5191e52009-11-16 14:58:17 +01002914/**
2915 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2916 *
2917 * The control element is supposed to have the private_value field
2918 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2919 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002920int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2921 struct snd_ctl_elem_value *ucontrol)
2922{
2923 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2924 struct hda_bind_ctls *c;
2925 int err;
2926
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002927 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002928 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002929 kcontrol->private_value = *c->values;
2930 err = c->ops->get(kcontrol, ucontrol);
2931 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002932 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002933 return err;
2934}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002935EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002936
Takashi Iwaid5191e52009-11-16 14:58:17 +01002937/**
2938 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2939 *
2940 * The control element is supposed to have the private_value field
2941 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2942 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002943int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2944 struct snd_ctl_elem_value *ucontrol)
2945{
2946 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2947 struct hda_bind_ctls *c;
2948 unsigned long *vals;
2949 int err = 0, change = 0;
2950
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002951 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002952 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002953 for (vals = c->values; *vals; vals++) {
2954 kcontrol->private_value = *vals;
2955 err = c->ops->put(kcontrol, ucontrol);
2956 if (err < 0)
2957 break;
2958 change |= err;
2959 }
2960 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002961 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002962 return err < 0 ? err : change;
2963}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002964EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002965
Takashi Iwaid5191e52009-11-16 14:58:17 +01002966/**
2967 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2968 *
2969 * The control element is supposed to have the private_value field
2970 * set up via HDA_BIND_VOL() macro.
2971 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002972int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2973 unsigned int size, unsigned int __user *tlv)
2974{
2975 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2976 struct hda_bind_ctls *c;
2977 int err;
2978
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002979 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002980 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002981 kcontrol->private_value = *c->values;
2982 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2983 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002984 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002985 return err;
2986}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002987EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002988
2989struct hda_ctl_ops snd_hda_bind_vol = {
2990 .info = snd_hda_mixer_amp_volume_info,
2991 .get = snd_hda_mixer_amp_volume_get,
2992 .put = snd_hda_mixer_amp_volume_put,
2993 .tlv = snd_hda_mixer_amp_tlv
2994};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002995EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002996
2997struct hda_ctl_ops snd_hda_bind_sw = {
2998 .info = snd_hda_mixer_amp_switch_info,
2999 .get = snd_hda_mixer_amp_switch_get,
3000 .put = snd_hda_mixer_amp_switch_put,
3001 .tlv = snd_hda_mixer_amp_tlv
3002};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003003EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02003004
3005/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 * SPDIF out controls
3007 */
3008
Takashi Iwai0ba21762007-04-16 11:29:14 +02003009static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
3010 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011{
3012 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
3013 uinfo->count = 1;
3014 return 0;
3015}
3016
Takashi Iwai0ba21762007-04-16 11:29:14 +02003017static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
3018 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019{
3020 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3021 IEC958_AES0_NONAUDIO |
3022 IEC958_AES0_CON_EMPHASIS_5015 |
3023 IEC958_AES0_CON_NOT_COPYRIGHT;
3024 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
3025 IEC958_AES1_CON_ORIGINAL;
3026 return 0;
3027}
3028
Takashi Iwai0ba21762007-04-16 11:29:14 +02003029static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
3030 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031{
3032 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3033 IEC958_AES0_NONAUDIO |
3034 IEC958_AES0_PRO_EMPHASIS_5015;
3035 return 0;
3036}
3037
Takashi Iwai0ba21762007-04-16 11:29:14 +02003038static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
3039 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040{
3041 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003042 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003043 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003045 mutex_lock(&codec->spdif_mutex);
3046 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003047 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
3048 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
3049 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
3050 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003051 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052
3053 return 0;
3054}
3055
3056/* convert from SPDIF status bits to HDA SPDIF bits
3057 * bit 0 (DigEn) is always set zero (to be filled later)
3058 */
3059static unsigned short convert_from_spdif_status(unsigned int sbits)
3060{
3061 unsigned short val = 0;
3062
3063 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003064 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003066 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003068 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
3069 IEC958_AES0_PRO_EMPHASIS_5015)
3070 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003072 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
3073 IEC958_AES0_CON_EMPHASIS_5015)
3074 val |= AC_DIG1_EMPHASIS;
3075 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
3076 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02003078 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
3080 }
3081 return val;
3082}
3083
3084/* convert to SPDIF status bits from HDA SPDIF bits
3085 */
3086static unsigned int convert_to_spdif_status(unsigned short val)
3087{
3088 unsigned int sbits = 0;
3089
Takashi Iwai0ba21762007-04-16 11:29:14 +02003090 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003092 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 sbits |= IEC958_AES0_PROFESSIONAL;
3094 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003095 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
3097 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003098 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003100 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003102 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3104 sbits |= val & (0x7f << 8);
3105 }
3106 return sbits;
3107}
3108
Takashi Iwai2f728532008-09-25 16:32:41 +02003109/* set digital convert verbs both for the given NID and its slaves */
3110static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3111 int verb, int val)
3112{
Takashi Iwaidda14412011-05-02 11:29:30 +02003113 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02003114
Takashi Iwai9e976972008-11-25 08:17:20 +01003115 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003116 d = codec->slave_dig_outs;
3117 if (!d)
3118 return;
3119 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01003120 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003121}
3122
3123static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3124 int dig1, int dig2)
3125{
3126 if (dig1 != -1)
3127 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3128 if (dig2 != -1)
3129 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3130}
3131
Takashi Iwai0ba21762007-04-16 11:29:14 +02003132static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3133 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134{
3135 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003136 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003137 struct hda_spdif_out *spdif;
3138 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 unsigned short val;
3140 int change;
3141
Ingo Molnar62932df2006-01-16 16:34:20 +01003142 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003143 spdif = snd_array_elem(&codec->spdif_out, idx);
3144 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003145 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3147 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3148 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c935972011-06-01 11:14:17 -06003149 val = convert_from_spdif_status(spdif->status);
3150 val |= spdif->ctls & 1;
3151 change = spdif->ctls != val;
3152 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003153 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02003154 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01003155 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 return change;
3157}
3158
Takashi Iwaia5ce8892007-07-23 15:42:26 +02003159#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160
Takashi Iwai0ba21762007-04-16 11:29:14 +02003161static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3162 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163{
3164 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003165 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003166 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003168 mutex_lock(&codec->spdif_mutex);
3169 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003170 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003171 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 return 0;
3173}
3174
Stephen Warren74b654c2011-06-01 11:14:18 -06003175static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3176 int dig1, int dig2)
3177{
3178 set_dig_out_convert(codec, nid, dig1, dig2);
3179 /* unmute amp switch (if any) */
3180 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3181 (dig1 & AC_DIG1_ENABLE))
3182 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3183 HDA_AMP_MUTE, 0);
3184}
3185
Takashi Iwai0ba21762007-04-16 11:29:14 +02003186static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3187 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188{
3189 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003190 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003191 struct hda_spdif_out *spdif;
3192 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 unsigned short val;
3194 int change;
3195
Ingo Molnar62932df2006-01-16 16:34:20 +01003196 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003197 spdif = snd_array_elem(&codec->spdif_out, idx);
3198 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003199 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003201 val |= AC_DIG1_ENABLE;
Stephen Warren7c935972011-06-01 11:14:17 -06003202 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003203 spdif->ctls = val;
3204 if (change && nid != (u16)-1)
3205 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003206 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 return change;
3208}
3209
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003210static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 {
3212 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3213 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003214 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 .info = snd_hda_spdif_mask_info,
3216 .get = snd_hda_spdif_cmask_get,
3217 },
3218 {
3219 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3220 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003221 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 .info = snd_hda_spdif_mask_info,
3223 .get = snd_hda_spdif_pmask_get,
3224 },
3225 {
3226 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003227 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 .info = snd_hda_spdif_mask_info,
3229 .get = snd_hda_spdif_default_get,
3230 .put = snd_hda_spdif_default_put,
3231 },
3232 {
3233 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003234 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 .info = snd_hda_spdif_out_switch_info,
3236 .get = snd_hda_spdif_out_switch_get,
3237 .put = snd_hda_spdif_out_switch_put,
3238 },
3239 { } /* end */
3240};
3241
3242/**
Takashi Iwaidcda5802012-10-12 17:24:51 +02003243 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 * @codec: the HDA codec
Takashi Iwaidcda5802012-10-12 17:24:51 +02003245 * @associated_nid: NID that new ctls associated with
3246 * @cvt_nid: converter NID
3247 * @type: HDA_PCM_TYPE_*
3248 * Creates controls related with the digital output.
3249 * Called from each patch supporting the digital out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 *
3251 * Returns 0 if successful, or a negative error code.
3252 */
Takashi Iwaidcda5802012-10-12 17:24:51 +02003253int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3254 hda_nid_t associated_nid,
3255 hda_nid_t cvt_nid,
3256 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257{
3258 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003259 struct snd_kcontrol *kctl;
3260 struct snd_kcontrol_new *dig_mix;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003261 int idx, dev = 0;
3262 const int spdif_pcm_dev = 1;
Stephen Warren7c935972011-06-01 11:14:17 -06003263 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264
Takashi Iwaidcda5802012-10-12 17:24:51 +02003265 if (codec->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
3266 type == HDA_PCM_TYPE_SPDIF) {
3267 dev = spdif_pcm_dev;
3268 } else if (codec->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
3269 type == HDA_PCM_TYPE_HDMI) {
3270 for (idx = 0; idx < codec->spdif_out.used; idx++) {
3271 spdif = snd_array_elem(&codec->spdif_out, idx);
3272 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3273 kctl = find_mixer_ctl(codec, dig_mix->name, 0, idx);
3274 if (!kctl)
3275 break;
3276 kctl->id.device = spdif_pcm_dev;
3277 }
3278 }
3279 codec->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
3280 }
3281 if (!codec->primary_dig_out_type)
3282 codec->primary_dig_out_type = type;
3283
3284 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", dev);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003285 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003286 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
3287 return -EBUSY;
3288 }
Stephen Warren7c935972011-06-01 11:14:17 -06003289 spdif = snd_array_new(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3291 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003292 if (!kctl)
3293 return -ENOMEM;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003294 kctl->id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01003295 kctl->id.index = idx;
Stephen Warren7c935972011-06-01 11:14:17 -06003296 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003297 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003298 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 return err;
3300 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003301 spdif->nid = cvt_nid;
3302 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c935972011-06-01 11:14:17 -06003303 AC_VERB_GET_DIGI_CONVERT_1, 0);
3304 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 return 0;
3306}
Takashi Iwaidcda5802012-10-12 17:24:51 +02003307EXPORT_SYMBOL_HDA(snd_hda_create_dig_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003309/* get the hda_spdif_out entry from the given NID
3310 * call within spdif_mutex lock
3311 */
Stephen Warren7c935972011-06-01 11:14:17 -06003312struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3313 hda_nid_t nid)
3314{
3315 int i;
3316 for (i = 0; i < codec->spdif_out.used; i++) {
3317 struct hda_spdif_out *spdif =
3318 snd_array_elem(&codec->spdif_out, i);
3319 if (spdif->nid == nid)
3320 return spdif;
3321 }
3322 return NULL;
3323}
3324EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
3325
Stephen Warren74b654c2011-06-01 11:14:18 -06003326void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3327{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003328 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003329
3330 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003331 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003332 spdif->nid = (u16)-1;
3333 mutex_unlock(&codec->spdif_mutex);
3334}
3335EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
3336
3337void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3338{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003339 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003340 unsigned short val;
3341
3342 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003343 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003344 if (spdif->nid != nid) {
3345 spdif->nid = nid;
3346 val = spdif->ctls;
3347 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3348 }
3349 mutex_unlock(&codec->spdif_mutex);
3350}
3351EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
3352
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003354 * SPDIF sharing with analog output
3355 */
3356static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3357 struct snd_ctl_elem_value *ucontrol)
3358{
3359 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3360 ucontrol->value.integer.value[0] = mout->share_spdif;
3361 return 0;
3362}
3363
3364static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3365 struct snd_ctl_elem_value *ucontrol)
3366{
3367 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3368 mout->share_spdif = !!ucontrol->value.integer.value[0];
3369 return 0;
3370}
3371
3372static struct snd_kcontrol_new spdif_share_sw = {
3373 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3374 .name = "IEC958 Default PCM Playback Switch",
3375 .info = snd_ctl_boolean_mono_info,
3376 .get = spdif_share_sw_get,
3377 .put = spdif_share_sw_put,
3378};
3379
Takashi Iwaid5191e52009-11-16 14:58:17 +01003380/**
3381 * snd_hda_create_spdif_share_sw - create Default PCM switch
3382 * @codec: the HDA codec
3383 * @mout: multi-out instance
3384 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003385int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3386 struct hda_multi_out *mout)
3387{
3388 if (!mout->dig_out_nid)
3389 return 0;
3390 /* ATTENTION: here mout is passed as private_data, instead of codec */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003391 return snd_hda_ctl_add(codec, mout->dig_out_nid,
3392 snd_ctl_new1(&spdif_share_sw, mout));
Takashi Iwai9a081602008-02-12 18:37:26 +01003393}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003394EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003395
3396/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 * SPDIF input
3398 */
3399
3400#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3401
Takashi Iwai0ba21762007-04-16 11:29:14 +02003402static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3403 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404{
3405 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3406
3407 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3408 return 0;
3409}
3410
Takashi Iwai0ba21762007-04-16 11:29:14 +02003411static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3412 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413{
3414 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3415 hda_nid_t nid = kcontrol->private_value;
3416 unsigned int val = !!ucontrol->value.integer.value[0];
3417 int change;
3418
Ingo Molnar62932df2006-01-16 16:34:20 +01003419 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003421 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003423 snd_hda_codec_write_cache(codec, nid, 0,
3424 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003426 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 return change;
3428}
3429
Takashi Iwai0ba21762007-04-16 11:29:14 +02003430static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3431 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432{
3433 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3434 hda_nid_t nid = kcontrol->private_value;
3435 unsigned short val;
3436 unsigned int sbits;
3437
Andrew Paprocki3982d172007-12-19 12:13:44 +01003438 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 sbits = convert_to_spdif_status(val);
3440 ucontrol->value.iec958.status[0] = sbits;
3441 ucontrol->value.iec958.status[1] = sbits >> 8;
3442 ucontrol->value.iec958.status[2] = sbits >> 16;
3443 ucontrol->value.iec958.status[3] = sbits >> 24;
3444 return 0;
3445}
3446
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003447static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 {
3449 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003450 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 .info = snd_hda_spdif_in_switch_info,
3452 .get = snd_hda_spdif_in_switch_get,
3453 .put = snd_hda_spdif_in_switch_put,
3454 },
3455 {
3456 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3457 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003458 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 .info = snd_hda_spdif_mask_info,
3460 .get = snd_hda_spdif_in_status_get,
3461 },
3462 { } /* end */
3463};
3464
3465/**
3466 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3467 * @codec: the HDA codec
3468 * @nid: audio in widget NID
3469 *
3470 * Creates controls related with the SPDIF input.
3471 * Called from each patch supporting the SPDIF in.
3472 *
3473 * Returns 0 if successful, or a negative error code.
3474 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003475int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476{
3477 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003478 struct snd_kcontrol *kctl;
3479 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003480 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481
Takashi Iwaidcda5802012-10-12 17:24:51 +02003482 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003483 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003484 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3485 return -EBUSY;
3486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3488 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003489 if (!kctl)
3490 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003492 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003493 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 return err;
3495 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003496 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003497 snd_hda_codec_read(codec, nid, 0,
3498 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003499 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 return 0;
3501}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003502EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003504/*
3505 * command cache
3506 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507
Takashi Iwaic370dd62012-12-13 18:30:04 +01003508/* build a 31bit cache key with the widget id and the command parameter */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003509#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3510#define get_cmd_cache_nid(key) ((key) & 0xff)
3511#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3512
3513/**
3514 * snd_hda_codec_write_cache - send a single command with caching
3515 * @codec: the HDA codec
3516 * @nid: NID to send the command
3517 * @direct: direct flag
3518 * @verb: the verb to send
3519 * @parm: the parameter for the verb
3520 *
3521 * Send a single command without waiting for response.
3522 *
3523 * Returns 0 if successful, or a negative error code.
3524 */
3525int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3526 int direct, unsigned int verb, unsigned int parm)
3527{
Takashi Iwaic370dd62012-12-13 18:30:04 +01003528 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003529 struct hda_cache_head *c;
3530 u32 key;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003531 unsigned int cache_only;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003532
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003533 cache_only = codec->cached_write;
3534 if (!cache_only) {
Takashi Iwaic370dd62012-12-13 18:30:04 +01003535 err = snd_hda_codec_write(codec, nid, direct, verb, parm);
3536 if (err < 0)
3537 return err;
3538 }
3539
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003540 /* parm may contain the verb stuff for get/set amp */
3541 verb = verb | (parm >> 8);
3542 parm &= 0xff;
3543 key = build_cmd_cache_key(nid, verb);
3544 mutex_lock(&codec->bus->cmd_mutex);
3545 c = get_alloc_hash(&codec->cmd_cache, key);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003546 if (c) {
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003547 c->val = parm;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003548 c->dirty = cache_only;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003549 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003550 mutex_unlock(&codec->bus->cmd_mutex);
3551 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003552}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003553EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003554
Takashi Iwaid5191e52009-11-16 14:58:17 +01003555/**
Takashi Iwaia68d5a542010-03-30 18:03:44 +02003556 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3557 * @codec: the HDA codec
3558 * @nid: NID to send the command
3559 * @direct: direct flag
3560 * @verb: the verb to send
3561 * @parm: the parameter for the verb
3562 *
3563 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3564 * command if the parameter is already identical with the cached value.
3565 * If not, it sends the command and refreshes the cache.
3566 *
3567 * Returns 0 if successful, or a negative error code.
3568 */
3569int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3570 int direct, unsigned int verb, unsigned int parm)
3571{
3572 struct hda_cache_head *c;
3573 u32 key;
3574
3575 /* parm may contain the verb stuff for get/set amp */
3576 verb = verb | (parm >> 8);
3577 parm &= 0xff;
3578 key = build_cmd_cache_key(nid, verb);
3579 mutex_lock(&codec->bus->cmd_mutex);
3580 c = get_hash(&codec->cmd_cache, key);
3581 if (c && c->val == parm) {
3582 mutex_unlock(&codec->bus->cmd_mutex);
3583 return 0;
3584 }
3585 mutex_unlock(&codec->bus->cmd_mutex);
3586 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3587}
3588EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3589
3590/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003591 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3592 * @codec: HD-audio codec
3593 *
3594 * Execute all verbs recorded in the command caches to resume.
3595 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003596void snd_hda_codec_resume_cache(struct hda_codec *codec)
3597{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003598 int i;
3599
Takashi Iwaic370dd62012-12-13 18:30:04 +01003600 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01003601 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003602 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3603 struct hda_cache_head *buffer;
3604 u32 key;
3605
3606 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3607 key = buffer->key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003608 if (!key)
3609 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003610 if (!buffer->dirty)
3611 continue;
3612 buffer->dirty = 0;
3613 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003614 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3615 get_cmd_cache_cmd(key), buffer->val);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003616 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003617 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01003618 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003619}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003620EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003621
3622/**
3623 * snd_hda_sequence_write_cache - sequence writes with caching
3624 * @codec: the HDA codec
3625 * @seq: VERB array to send
3626 *
3627 * Send the commands sequentially from the given array.
3628 * Thte commands are recorded on cache for power-save and resume.
3629 * The array must be terminated with NID=0.
3630 */
3631void snd_hda_sequence_write_cache(struct hda_codec *codec,
3632 const struct hda_verb *seq)
3633{
3634 for (; seq->nid; seq++)
3635 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3636 seq->param);
3637}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003638EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003639
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003640void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
3641 unsigned int power_state,
3642 bool eapd_workaround)
Takashi Iwai54d17402005-11-21 16:33:22 +01003643{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003644 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003645 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003646
Takashi Iwaicb53c622007-08-10 17:21:45 +02003647 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003648 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003649 if (!(wcaps & AC_WCAP_POWER))
3650 continue;
3651 /* don't power down the widget if it controls eapd and
3652 * EAPD_BTLENABLE is set.
3653 */
3654 if (eapd_workaround && power_state == AC_PWRST_D3 &&
3655 get_wcaps_type(wcaps) == AC_WID_PIN &&
3656 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3657 int eapd = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003658 AC_VERB_GET_EAPD_BTLENABLE, 0);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003659 if (eapd & 0x02)
3660 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003661 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003662 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
3663 power_state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003664 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003665}
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003666EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3667
3668/*
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003669 * supported power states check
3670 */
3671static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
3672 unsigned int power_state)
3673{
3674 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
3675
Mengdong Line037cb42012-08-10 14:11:58 +02003676 if (sup == -1)
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003677 return false;
3678 if (sup & power_state)
3679 return true;
3680 else
3681 return false;
3682}
3683
3684/*
Takashi Iwai432c6412012-08-28 09:59:20 -07003685 * wait until the state is reached, returns the current state
3686 */
3687static unsigned int hda_sync_power_state(struct hda_codec *codec,
3688 hda_nid_t fg,
3689 unsigned int power_state)
3690{
3691 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3692 unsigned int state, actual_state;
3693
3694 for (;;) {
3695 state = snd_hda_codec_read(codec, fg, 0,
3696 AC_VERB_GET_POWER_STATE, 0);
3697 if (state & AC_PWRST_ERROR)
3698 break;
3699 actual_state = (state >> 4) & 0x0f;
3700 if (actual_state == power_state)
3701 break;
3702 if (time_after_eq(jiffies, end_time))
3703 break;
3704 /* wait until the codec reachs to the target state */
3705 msleep(1);
3706 }
3707 return state;
3708}
3709
3710/*
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003711 * set power state of the codec, and return the power state
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003712 */
Takashi Iwaid8193872012-08-31 07:54:38 -07003713static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003714 unsigned int power_state)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003715{
Takashi Iwaid8193872012-08-31 07:54:38 -07003716 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003717 int count;
3718 unsigned int state;
3719
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003720 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003721 if (power_state == AC_PWRST_D3) {
3722 /* transition time less than 10ms for power down */
Takashi Iwai983f6b92012-08-28 09:18:01 -07003723 msleep(codec->epss ? 10 : 100);
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003724 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08003725
3726 /* repeat power states setting at most 10 times*/
3727 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07003728 if (codec->patch_ops.set_power_state)
3729 codec->patch_ops.set_power_state(codec, fg,
3730 power_state);
3731 else {
3732 snd_hda_codec_read(codec, fg, 0,
3733 AC_VERB_SET_POWER_STATE,
3734 power_state);
3735 snd_hda_codec_set_power_to_all(codec, fg, power_state,
3736 true);
3737 }
3738 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08003739 if (!(state & AC_PWRST_ERROR))
3740 break;
3741 }
Mengdong Linb8dfc4622012-08-23 17:32:30 +08003742
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003743 return state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003744}
Takashi Iwai54d17402005-11-21 16:33:22 +01003745
Takashi Iwai11aeff02008-07-30 15:01:46 +02003746#ifdef CONFIG_SND_HDA_HWDEP
3747/* execute additional init verbs */
3748static void hda_exec_init_verbs(struct hda_codec *codec)
3749{
3750 if (codec->init_verbs.list)
3751 snd_hda_sequence_write(codec, codec->init_verbs.list);
3752}
3753#else
3754static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3755#endif
3756
Takashi Iwai2a439522011-07-26 09:52:50 +02003757#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003758/*
3759 * call suspend and power-down; used both from PM and power-save
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003760 * this function returns the power state in the end
Takashi Iwaicb53c622007-08-10 17:21:45 +02003761 */
Dylan Reidd17344b2012-09-28 15:57:01 -07003762static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003763{
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003764 unsigned int state;
3765
Takashi Iwai989c3182012-11-19 14:14:58 +01003766 codec->in_pm = 1;
3767
Takashi Iwaicb53c622007-08-10 17:21:45 +02003768 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02003769 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02003770 hda_cleanup_all_streams(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003771 state = hda_set_power_state(codec, AC_PWRST_D3);
Dylan Reidd17344b2012-09-28 15:57:01 -07003772 /* Cancel delayed work if we aren't currently running from it. */
3773 if (!in_wq)
3774 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003775 spin_lock(&codec->power_lock);
3776 snd_hda_update_power_acct(codec);
3777 trace_hda_power_down(codec);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003778 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003779 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003780 codec->power_jiffies = jiffies;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003781 spin_unlock(&codec->power_lock);
Takashi Iwai989c3182012-11-19 14:14:58 +01003782 codec->in_pm = 0;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003783 return state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003784}
3785
Takashi Iwaic370dd62012-12-13 18:30:04 +01003786/* mark all entries of cmd and amp caches dirty */
3787static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
3788{
3789 int i;
3790 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3791 struct hda_cache_head *cmd;
3792 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
3793 cmd->dirty = 1;
3794 }
3795 for (i = 0; i < codec->amp_cache.buf.used; i++) {
3796 struct hda_amp_info *amp;
David Henningssonf038fca2013-01-15 15:27:19 +01003797 amp = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003798 amp->head.dirty = 1;
3799 }
3800}
3801
Takashi Iwaicb53c622007-08-10 17:21:45 +02003802/*
3803 * kick up codec; used both from PM and power-save
3804 */
3805static void hda_call_codec_resume(struct hda_codec *codec)
3806{
Takashi Iwai989c3182012-11-19 14:14:58 +01003807 codec->in_pm = 1;
3808
Takashi Iwaic370dd62012-12-13 18:30:04 +01003809 hda_mark_cmd_cache_dirty(codec);
3810
Takashi Iwai7f308302012-05-08 16:52:23 +02003811 /* set as if powered on for avoiding re-entering the resume
3812 * in the resume / power-save sequence
3813 */
3814 hda_keep_power_on(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003815 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003816 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003817 hda_exec_init_verbs(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003818 if (codec->patch_ops.resume)
3819 codec->patch_ops.resume(codec);
3820 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003821 if (codec->patch_ops.init)
3822 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003823 snd_hda_codec_resume_amp(codec);
3824 snd_hda_codec_resume_cache(codec);
3825 }
David Henningsson26a6cb62012-10-09 15:04:21 +02003826
3827 if (codec->jackpoll_interval)
3828 hda_jackpoll_work(&codec->jackpoll_work.work);
3829 else {
3830 snd_hda_jack_set_dirty_all(codec);
3831 snd_hda_jack_report_sync(codec);
3832 }
Takashi Iwai989c3182012-11-19 14:14:58 +01003833
3834 codec->in_pm = 0;
Takashi Iwai7f308302012-05-08 16:52:23 +02003835 snd_hda_power_down(codec); /* flag down before returning */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003836}
Takashi Iwai2a439522011-07-26 09:52:50 +02003837#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003838
Takashi Iwai54d17402005-11-21 16:33:22 +01003839
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840/**
3841 * snd_hda_build_controls - build mixer controls
3842 * @bus: the BUS
3843 *
3844 * Creates mixer controls for each codec included in the bus.
3845 *
3846 * Returns 0 if successful, otherwise a negative error code.
3847 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01003848int snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003850 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851
Takashi Iwai0ba21762007-04-16 11:29:14 +02003852 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003853 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003854 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01003855 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003856 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003857 err = snd_hda_codec_reset(codec);
3858 if (err < 0) {
3859 printk(KERN_ERR
3860 "hda_codec: cannot revert codec\n");
3861 return err;
3862 }
3863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003865 return 0;
3866}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003867EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003869/*
3870 * add standard channel maps if not specified
3871 */
3872static int add_std_chmaps(struct hda_codec *codec)
3873{
3874 int i, str, err;
3875
3876 for (i = 0; i < codec->num_pcms; i++) {
3877 for (str = 0; str < 2; str++) {
3878 struct snd_pcm *pcm = codec->pcm_info[i].pcm;
3879 struct hda_pcm_stream *hinfo =
3880 &codec->pcm_info[i].stream[str];
3881 struct snd_pcm_chmap *chmap;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003882 const struct snd_pcm_chmap_elem *elem;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003883
3884 if (codec->pcm_info[i].own_chmap)
3885 continue;
3886 if (!pcm || !hinfo->substreams)
3887 continue;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003888 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
3889 err = snd_pcm_add_chmap_ctls(pcm, str, elem,
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003890 hinfo->channels_max,
3891 0, &chmap);
3892 if (err < 0)
3893 return err;
3894 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
3895 }
3896 }
3897 return 0;
3898}
3899
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003900/* default channel maps for 2.1 speakers;
3901 * since HD-audio supports only stereo, odd number channels are omitted
3902 */
3903const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
3904 { .channels = 2,
3905 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
3906 { .channels = 4,
3907 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
3908 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
3909 { }
3910};
3911EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
3912
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003913int snd_hda_codec_build_controls(struct hda_codec *codec)
3914{
3915 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003916 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003917 /* continue to initialize... */
3918 if (codec->patch_ops.init)
3919 err = codec->patch_ops.init(codec);
3920 if (!err && codec->patch_ops.build_controls)
3921 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003922 if (err < 0)
3923 return err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003924
3925 /* we create chmaps here instead of build_pcms */
3926 err = add_std_chmaps(codec);
3927 if (err < 0)
3928 return err;
3929
David Henningsson26a6cb62012-10-09 15:04:21 +02003930 if (codec->jackpoll_interval)
3931 hda_jackpoll_work(&codec->jackpoll_work.work);
3932 else
3933 snd_hda_jack_report_sync(codec); /* call at the last init point */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 return 0;
3935}
3936
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937/*
3938 * stream formats
3939 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02003940struct hda_rate_tbl {
3941 unsigned int hz;
3942 unsigned int alsa_bits;
3943 unsigned int hda_fmt;
3944};
3945
Takashi Iwai92f10b32010-08-03 14:21:00 +02003946/* rate = base * mult / div */
3947#define HDA_RATE(base, mult, div) \
3948 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3949 (((div) - 1) << AC_FMT_DIV_SHIFT))
3950
Takashi Iwaibefdf312005-08-22 13:57:55 +02003951static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003953
3954 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003955 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3956 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3957 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3958 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3959 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3960 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3961 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3962 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3963 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3964 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3965 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02003966#define AC_PAR_PCM_RATE_BITS 11
3967 /* up to bits 10, 384kHZ isn't supported properly */
3968
3969 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02003970 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02003971
Takashi Iwaibefdf312005-08-22 13:57:55 +02003972 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973};
3974
3975/**
3976 * snd_hda_calc_stream_format - calculate format bitset
3977 * @rate: the sample rate
3978 * @channels: the number of channels
3979 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3980 * @maxbps: the max. bps
3981 *
3982 * Calculate the format bitset from the given rate, channels and th PCM format.
3983 *
3984 * Return zero if invalid.
3985 */
3986unsigned int snd_hda_calc_stream_format(unsigned int rate,
3987 unsigned int channels,
3988 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03003989 unsigned int maxbps,
3990 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991{
3992 int i;
3993 unsigned int val = 0;
3994
Takashi Iwaibefdf312005-08-22 13:57:55 +02003995 for (i = 0; rate_bits[i].hz; i++)
3996 if (rate_bits[i].hz == rate) {
3997 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 break;
3999 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02004000 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 snd_printdd("invalid rate %d\n", rate);
4002 return 0;
4003 }
4004
4005 if (channels == 0 || channels > 8) {
4006 snd_printdd("invalid channels %d\n", channels);
4007 return 0;
4008 }
4009 val |= channels - 1;
4010
4011 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004012 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004013 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004014 break;
4015 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004016 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004017 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 case 20:
4019 case 24:
4020 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004021 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004022 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004024 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02004026 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 break;
4028 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004029 snd_printdd("invalid format width %d\n",
4030 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 return 0;
4032 }
4033
Anssi Hannula32c168c2010-08-03 13:28:57 +03004034 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004035 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03004036
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 return val;
4038}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004039EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004041static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
4042 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004043{
4044 unsigned int val = 0;
4045 if (nid != codec->afg &&
4046 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
4047 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
4048 if (!val || val == -1)
4049 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
4050 if (!val || val == -1)
4051 return 0;
4052 return val;
4053}
4054
4055static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
4056{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004057 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004058 get_pcm_param);
4059}
4060
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004061static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
4062 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004063{
4064 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
4065 if (!streams || streams == -1)
4066 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
4067 if (!streams || streams == -1)
4068 return 0;
4069 return streams;
4070}
4071
4072static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
4073{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004074 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004075 get_stream_param);
4076}
4077
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078/**
4079 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4080 * @codec: the HDA codec
4081 * @nid: NID to query
4082 * @ratesp: the pointer to store the detected rate bitflags
4083 * @formatsp: the pointer to store the detected formats
4084 * @bpsp: the pointer to store the detected format widths
4085 *
4086 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4087 * or @bsps argument is ignored.
4088 *
4089 * Returns 0 if successful, otherwise a negative error code.
4090 */
Stephen Warren384a48d2011-06-01 11:14:21 -06004091int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
4093{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004094 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004096 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004097 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098
4099 if (ratesp) {
4100 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004101 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02004103 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004105 if (rates == 0) {
4106 snd_printk(KERN_ERR "hda_codec: rates == 0 "
4107 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
4108 nid, val,
4109 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
4110 return -EIO;
4111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 *ratesp = rates;
4113 }
4114
4115 if (formatsp || bpsp) {
4116 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004117 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004119 streams = query_stream_param(codec, nid);
4120 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122
4123 bps = 0;
4124 if (streams & AC_SUPFMT_PCM) {
4125 if (val & AC_SUPPCM_BITS_8) {
4126 formats |= SNDRV_PCM_FMTBIT_U8;
4127 bps = 8;
4128 }
4129 if (val & AC_SUPPCM_BITS_16) {
4130 formats |= SNDRV_PCM_FMTBIT_S16_LE;
4131 bps = 16;
4132 }
4133 if (wcaps & AC_WCAP_DIGITAL) {
4134 if (val & AC_SUPPCM_BITS_32)
4135 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4136 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4137 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4138 if (val & AC_SUPPCM_BITS_24)
4139 bps = 24;
4140 else if (val & AC_SUPPCM_BITS_20)
4141 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004142 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4143 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4145 if (val & AC_SUPPCM_BITS_32)
4146 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147 else if (val & AC_SUPPCM_BITS_24)
4148 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02004149 else if (val & AC_SUPPCM_BITS_20)
4150 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 }
4152 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004153#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02004154 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004156 if (!bps)
4157 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02004158 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004159#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02004160 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02004161 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 /* temporary hack: we have still no proper support
4163 * for the direct AC3 stream...
4164 */
4165 formats |= SNDRV_PCM_FMTBIT_U8;
4166 bps = 8;
4167 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004168 if (formats == 0) {
4169 snd_printk(KERN_ERR "hda_codec: formats == 0 "
4170 "(nid=0x%x, val=0x%x, ovrd=%i, "
4171 "streams=0x%x)\n",
4172 nid, val,
4173 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4174 streams);
4175 return -EIO;
4176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 if (formatsp)
4178 *formatsp = formats;
4179 if (bpsp)
4180 *bpsp = bps;
4181 }
4182
4183 return 0;
4184}
Stephen Warren384a48d2011-06-01 11:14:21 -06004185EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186
4187/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004188 * snd_hda_is_supported_format - Check the validity of the format
4189 * @codec: HD-audio codec
4190 * @nid: NID to check
4191 * @format: the HD-audio format value to check
4192 *
4193 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 *
4195 * Returns 1 if supported, 0 if not.
4196 */
4197int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4198 unsigned int format)
4199{
4200 int i;
4201 unsigned int val = 0, rate, stream;
4202
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004203 val = query_pcm_param(codec, nid);
4204 if (!val)
4205 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206
4207 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004208 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02004209 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 if (val & (1 << i))
4211 break;
4212 return 0;
4213 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004214 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 return 0;
4216
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004217 stream = query_stream_param(codec, nid);
4218 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 return 0;
4220
4221 if (stream & AC_SUPFMT_PCM) {
4222 switch (format & 0xf0) {
4223 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004224 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225 return 0;
4226 break;
4227 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004228 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 return 0;
4230 break;
4231 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004232 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 return 0;
4234 break;
4235 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004236 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 return 0;
4238 break;
4239 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004240 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 return 0;
4242 break;
4243 default:
4244 return 0;
4245 }
4246 } else {
4247 /* FIXME: check for float32 and AC3? */
4248 }
4249
4250 return 1;
4251}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004252EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253
4254/*
4255 * PCM stuff
4256 */
4257static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4258 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004259 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260{
4261 return 0;
4262}
4263
4264static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4265 struct hda_codec *codec,
4266 unsigned int stream_tag,
4267 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004268 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269{
4270 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4271 return 0;
4272}
4273
4274static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4275 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004276 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277{
Takashi Iwai888afa12008-03-18 09:57:50 +01004278 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 return 0;
4280}
4281
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004282static int set_pcm_default_values(struct hda_codec *codec,
4283 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004285 int err;
4286
Takashi Iwai0ba21762007-04-16 11:29:14 +02004287 /* query support PCM information from the given NID */
4288 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004289 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02004290 info->rates ? NULL : &info->rates,
4291 info->formats ? NULL : &info->formats,
4292 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004293 if (err < 0)
4294 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 }
4296 if (info->ops.open == NULL)
4297 info->ops.open = hda_pcm_default_open_close;
4298 if (info->ops.close == NULL)
4299 info->ops.close = hda_pcm_default_open_close;
4300 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004301 if (snd_BUG_ON(!info->nid))
4302 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 info->ops.prepare = hda_pcm_default_prepare;
4304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004306 if (snd_BUG_ON(!info->nid))
4307 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 info->ops.cleanup = hda_pcm_default_cleanup;
4309 }
4310 return 0;
4311}
4312
Takashi Iwaieb541332010-08-06 13:48:11 +02004313/*
4314 * codec prepare/cleanup entries
4315 */
4316int snd_hda_codec_prepare(struct hda_codec *codec,
4317 struct hda_pcm_stream *hinfo,
4318 unsigned int stream,
4319 unsigned int format,
4320 struct snd_pcm_substream *substream)
4321{
4322 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004323 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004324 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4325 if (ret >= 0)
4326 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004327 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004328 return ret;
4329}
4330EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
4331
4332void snd_hda_codec_cleanup(struct hda_codec *codec,
4333 struct hda_pcm_stream *hinfo,
4334 struct snd_pcm_substream *substream)
4335{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004336 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004337 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004338 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004339}
4340EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
4341
Takashi Iwaid5191e52009-11-16 14:58:17 +01004342/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01004343const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4344 "Audio", "SPDIF", "HDMI", "Modem"
4345};
4346
Takashi Iwai176d5332008-07-30 15:01:44 +02004347/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004348 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01004349 *
4350 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004351 */
4352static int get_empty_pcm_device(struct hda_bus *bus, int type)
4353{
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004354 /* audio device indices; not linear to keep compatibility */
4355 static int audio_idx[HDA_PCM_NTYPES][5] = {
4356 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4357 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01004358 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004359 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004360 };
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004361 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004362
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004363 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004364 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
4365 return -EINVAL;
4366 }
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004367
4368 for (i = 0; audio_idx[type][i] >= 0 ; i++)
4369 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4370 return audio_idx[type][i];
4371
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004372 /* non-fixed slots starting from 10 */
4373 for (i = 10; i < 32; i++) {
4374 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4375 return i;
4376 }
4377
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004378 snd_printk(KERN_WARNING "Too many %s devices\n",
4379 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def52009-10-30 11:38:26 +01004380 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004381}
4382
4383/*
Takashi Iwai176d5332008-07-30 15:01:44 +02004384 * attach a new PCM stream
4385 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004386static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02004387{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004388 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02004389 struct hda_pcm_stream *info;
4390 int stream, err;
4391
Takashi Iwaib91f0802008-11-04 08:43:08 +01004392 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02004393 return -EINVAL;
4394 for (stream = 0; stream < 2; stream++) {
4395 info = &pcm->stream[stream];
4396 if (info->substreams) {
4397 err = set_pcm_default_values(codec, info);
4398 if (err < 0)
4399 return err;
4400 }
4401 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004402 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02004403}
4404
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004405/* assign all PCMs of the given codec */
4406int snd_hda_codec_build_pcms(struct hda_codec *codec)
4407{
4408 unsigned int pcm;
4409 int err;
4410
4411 if (!codec->num_pcms) {
4412 if (!codec->patch_ops.build_pcms)
4413 return 0;
4414 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004415 if (err < 0) {
4416 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004417 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004418 err = snd_hda_codec_reset(codec);
4419 if (err < 0) {
4420 printk(KERN_ERR
4421 "hda_codec: cannot revert codec\n");
4422 return err;
4423 }
4424 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004425 }
4426 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4427 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4428 int dev;
4429
4430 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004431 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004432
4433 if (!cpcm->pcm) {
4434 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4435 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004436 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004437 cpcm->device = dev;
4438 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004439 if (err < 0) {
4440 printk(KERN_ERR "hda_codec: cannot attach "
4441 "PCM stream %d for codec #%d\n",
4442 dev, codec->addr);
4443 continue; /* no fatal error */
4444 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004445 }
4446 }
4447 return 0;
4448}
4449
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450/**
4451 * snd_hda_build_pcms - build PCM information
4452 * @bus: the BUS
4453 *
4454 * Create PCM information for each codec included in the bus.
4455 *
4456 * The build_pcms codec patch is requested to set up codec->num_pcms and
4457 * codec->pcm_info properly. The array is referred by the top-level driver
4458 * to create its PCM instances.
4459 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4460 * callback.
4461 *
4462 * At least, substreams, channels_min and channels_max must be filled for
4463 * each stream. substreams = 0 indicates that the stream doesn't exist.
4464 * When rates and/or formats are zero, the supported values are queried
4465 * from the given nid. The nid is used also by the default ops.prepare
4466 * and ops.cleanup callbacks.
4467 *
4468 * The driver needs to call ops.open in its open callback. Similarly,
4469 * ops.close is supposed to be called in the close callback.
4470 * ops.prepare should be called in the prepare or hw_params callback
4471 * with the proper parameters for set up.
4472 * ops.cleanup should be called in hw_free for clean up of streams.
4473 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004474 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 */
Takashi Iwai5cb543d2012-08-09 13:49:23 +02004476int snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004478 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479
Takashi Iwai0ba21762007-04-16 11:29:14 +02004480 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004481 int err = snd_hda_codec_build_pcms(codec);
4482 if (err < 0)
4483 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 }
4485 return 0;
4486}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004487EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489/**
4490 * snd_hda_check_board_config - compare the current codec with the config table
4491 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004492 * @num_configs: number of config enums
4493 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494 * @tbl: configuration table, terminated by null entries
4495 *
4496 * Compares the modelname or PCI subsystem id of the current codec with the
4497 * given configuration table. If a matching entry is found, returns its
4498 * config value (supposed to be 0 or positive).
4499 *
4500 * If no entries are matching, the function returns a negative value.
4501 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004502int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004503 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02004504 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505{
Takashi Iwaif44ac832008-07-30 15:01:45 +02004506 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004507 int i;
4508 for (i = 0; i < num_configs; i++) {
4509 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02004510 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004511 snd_printd(KERN_INFO "hda_codec: model '%s' is "
4512 "selected\n", models[i]);
4513 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514 }
4515 }
4516 }
4517
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004518 if (!codec->bus->pci || !tbl)
4519 return -1;
4520
4521 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
4522 if (!tbl)
4523 return -1;
4524 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02004525#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004526 char tmp[10];
4527 const char *model = NULL;
4528 if (models)
4529 model = models[tbl->value];
4530 if (!model) {
4531 sprintf(tmp, "#%d", tbl->value);
4532 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004534 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4535 "for config %x:%x (%s)\n",
4536 model, tbl->subvendor, tbl->subdevice,
4537 (tbl->name ? tbl->name : "Unknown device"));
4538#endif
4539 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540 }
4541 return -1;
4542}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004543EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544
4545/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004546 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004547 subsystem ID with the
4548 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004549
4550 This is important for Gateway notebooks with SB450 HDA Audio
4551 where the vendor ID of the PCI device is:
4552 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4553 and the vendor/subvendor are found only at the codec.
4554
4555 * @codec: the HDA codec
4556 * @num_configs: number of config enums
4557 * @models: array of model name strings
4558 * @tbl: configuration table, terminated by null entries
4559 *
4560 * Compares the modelname or PCI subsystem id of the current codec with the
4561 * given configuration table. If a matching entry is found, returns its
4562 * config value (supposed to be 0 or positive).
4563 *
4564 * If no entries are matching, the function returns a negative value.
4565 */
4566int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004567 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004568 const struct snd_pci_quirk *tbl)
4569{
4570 const struct snd_pci_quirk *q;
4571
4572 /* Search for codec ID */
4573 for (q = tbl; q->subvendor; q++) {
Takashi Iwaie2301a42011-11-22 19:58:56 +01004574 unsigned int mask = 0xffff0000 | q->subdevice_mask;
4575 unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
4576 if ((codec->subsystem_id & mask) == id)
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004577 break;
4578 }
4579
4580 if (!q->subvendor)
4581 return -1;
4582
4583 tbl = q;
4584
4585 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02004586#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004587 char tmp[10];
4588 const char *model = NULL;
4589 if (models)
4590 model = models[tbl->value];
4591 if (!model) {
4592 sprintf(tmp, "#%d", tbl->value);
4593 model = tmp;
4594 }
4595 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4596 "for config %x:%x (%s)\n",
4597 model, tbl->subvendor, tbl->subdevice,
4598 (tbl->name ? tbl->name : "Unknown device"));
4599#endif
4600 return tbl->value;
4601 }
4602 return -1;
4603}
4604EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4605
4606/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607 * snd_hda_add_new_ctls - create controls from the array
4608 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004609 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610 *
4611 * This helper function creates and add new controls in the given array.
4612 * The array must be terminated with an empty entry as terminator.
4613 *
4614 * Returns 0 if successful, or a negative error code.
4615 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004616int snd_hda_add_new_ctls(struct hda_codec *codec,
4617 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004619 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620
4621 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004622 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004623 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004624 if (knew->iface == -1) /* skip this codec private value */
4625 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004626 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004627 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004628 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004629 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004630 if (addr > 0)
4631 kctl->id.device = addr;
4632 if (idx > 0)
4633 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004634 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004635 if (!err)
4636 break;
4637 /* try first with another device index corresponding to
4638 * the codec addr; if it still fails (or it's the
4639 * primary codec), then try another control index
4640 */
4641 if (!addr && codec->addr)
4642 addr = codec->addr;
4643 else if (!idx && !knew->index) {
4644 idx = find_empty_mixer_ctl_idx(codec,
Takashi Iwaidcda5802012-10-12 17:24:51 +02004645 knew->name, 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004646 if (idx <= 0)
4647 return err;
4648 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004649 return err;
4650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 }
4652 return 0;
4653}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004654EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655
Takashi Iwai83012a72012-08-24 18:38:08 +02004656#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02004657static void hda_power_work(struct work_struct *work)
4658{
4659 struct hda_codec *codec =
4660 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004661 struct hda_bus *bus = codec->bus;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004662 unsigned int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004663
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004664 spin_lock(&codec->power_lock);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004665 if (codec->power_transition > 0) { /* during power-up sequence? */
4666 spin_unlock(&codec->power_lock);
4667 return;
4668 }
Maxim Levitsky2e492462007-09-03 15:26:57 +02004669 if (!codec->power_on || codec->power_count) {
4670 codec->power_transition = 0;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004671 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004672 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02004673 }
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004674 spin_unlock(&codec->power_lock);
4675
Dylan Reidd17344b2012-09-28 15:57:01 -07004676 state = hda_call_codec_suspend(codec, true);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004677 codec->pm_down_notified = 0;
4678 if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK)) {
4679 codec->pm_down_notified = 1;
Takashi Iwai68467f52012-08-28 09:14:29 -07004680 hda_call_pm_notify(bus, false);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004681 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004682}
4683
4684static void hda_keep_power_on(struct hda_codec *codec)
4685{
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004686 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004687 codec->power_count++;
4688 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004689 codec->power_jiffies = jiffies;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004690 spin_unlock(&codec->power_lock);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004691}
4692
Takashi Iwaid5191e52009-11-16 14:58:17 +01004693/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01004694void snd_hda_update_power_acct(struct hda_codec *codec)
4695{
4696 unsigned long delta = jiffies - codec->power_jiffies;
4697 if (codec->power_on)
4698 codec->power_on_acct += delta;
4699 else
4700 codec->power_off_acct += delta;
4701 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004702}
4703
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004704/* Transition to powered up, if wait_power_down then wait for a pending
4705 * transition to D3 to complete. A pending D3 transition is indicated
4706 * with power_transition == -1. */
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004707/* call this with codec->power_lock held! */
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004708static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004709{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004710 struct hda_bus *bus = codec->bus;
4711
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004712 /* Return if power_on or transitioning to power_on, unless currently
4713 * powering down. */
4714 if ((codec->power_on || codec->power_transition > 0) &&
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004715 !(wait_power_down && codec->power_transition < 0))
Takashi Iwaicb53c622007-08-10 17:21:45 +02004716 return;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004717 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004718
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004719 cancel_delayed_work_sync(&codec->power_work);
4720
4721 spin_lock(&codec->power_lock);
Dylan Reidb43d2242012-06-21 21:51:22 -07004722 /* If the power down delayed work was cancelled above before starting,
4723 * then there is no need to go through power up here.
4724 */
4725 if (codec->power_on) {
Takashi Iwai535b6c52012-08-20 21:25:22 +02004726 if (codec->power_transition < 0)
4727 codec->power_transition = 0;
Dylan Reidb43d2242012-06-21 21:51:22 -07004728 return;
4729 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004730
Takashi Iwaid66fee52011-08-02 15:39:31 +02004731 trace_hda_power_up(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004732 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004733 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004734 codec->power_jiffies = jiffies;
Takashi Iwai7f308302012-05-08 16:52:23 +02004735 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004736 spin_unlock(&codec->power_lock);
4737
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004738 if (codec->pm_down_notified) {
4739 codec->pm_down_notified = 0;
Takashi Iwai68467f52012-08-28 09:14:29 -07004740 hda_call_pm_notify(bus, true);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004741 }
4742
Takashi Iwaicb53c622007-08-10 17:21:45 +02004743 hda_call_codec_resume(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004744
4745 spin_lock(&codec->power_lock);
Takashi Iwaia221e282007-08-16 16:35:33 +02004746 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004747}
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004748
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004749#define power_save(codec) \
4750 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004751
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004752/* Transition to powered down */
4753static void __snd_hda_power_down(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004754{
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004755 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004756 return;
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004757
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004758 if (power_save(codec)) {
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004759 codec->power_transition = -1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01004760 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004761 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02004762 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004763}
4764
4765/**
4766 * snd_hda_power_save - Power-up/down/sync the codec
4767 * @codec: HD-audio codec
4768 * @delta: the counter delta to change
4769 *
4770 * Change the power-up counter via @delta, and power up or down the hardware
4771 * appropriately. For the power-down, queue to the delayed action.
4772 * Passing zero to @delta means to synchronize the power state.
4773 */
4774void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
4775{
4776 spin_lock(&codec->power_lock);
4777 codec->power_count += delta;
4778 trace_hda_power_count(codec);
4779 if (delta > 0)
4780 __snd_hda_power_up(codec, d3wait);
4781 else
4782 __snd_hda_power_down(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004783 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004784}
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004785EXPORT_SYMBOL_HDA(snd_hda_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004786
Takashi Iwaid5191e52009-11-16 14:58:17 +01004787/**
4788 * snd_hda_check_amp_list_power - Check the amp list and update the power
4789 * @codec: HD-audio codec
4790 * @check: the object containing an AMP list and the status
4791 * @nid: NID to check / update
4792 *
4793 * Check whether the given NID is in the amp list. If it's in the list,
4794 * check the current AMP status, and update the the power-status according
4795 * to the mute status.
4796 *
4797 * This function is supposed to be set or called from the check_power_status
4798 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004799 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004800int snd_hda_check_amp_list_power(struct hda_codec *codec,
4801 struct hda_loopback_check *check,
4802 hda_nid_t nid)
4803{
Takashi Iwai031024e2011-05-02 11:29:30 +02004804 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004805 int ch, v;
4806
4807 if (!check->amplist)
4808 return 0;
4809 for (p = check->amplist; p->nid; p++) {
4810 if (p->nid == nid)
4811 break;
4812 }
4813 if (!p->nid)
4814 return 0; /* nothing changed */
4815
4816 for (p = check->amplist; p->nid; p++) {
4817 for (ch = 0; ch < 2; ch++) {
4818 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4819 p->idx);
4820 if (!(v & HDA_AMP_MUTE) && v > 0) {
4821 if (!check->power_on) {
4822 check->power_on = 1;
4823 snd_hda_power_up(codec);
4824 }
4825 return 1;
4826 }
4827 }
4828 }
4829 if (check->power_on) {
4830 check->power_on = 0;
4831 snd_hda_power_down(codec);
4832 }
4833 return 0;
4834}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004835EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004836#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004838/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004839 * Channel mode helper
4840 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004841
4842/**
4843 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4844 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004845int snd_hda_ch_mode_info(struct hda_codec *codec,
4846 struct snd_ctl_elem_info *uinfo,
4847 const struct hda_channel_mode *chmode,
4848 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004849{
4850 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4851 uinfo->count = 1;
4852 uinfo->value.enumerated.items = num_chmodes;
4853 if (uinfo->value.enumerated.item >= num_chmodes)
4854 uinfo->value.enumerated.item = num_chmodes - 1;
4855 sprintf(uinfo->value.enumerated.name, "%dch",
4856 chmode[uinfo->value.enumerated.item].channels);
4857 return 0;
4858}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004859EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004860
Takashi Iwaid5191e52009-11-16 14:58:17 +01004861/**
4862 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4863 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004864int snd_hda_ch_mode_get(struct hda_codec *codec,
4865 struct snd_ctl_elem_value *ucontrol,
4866 const struct hda_channel_mode *chmode,
4867 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004868 int max_channels)
4869{
4870 int i;
4871
4872 for (i = 0; i < num_chmodes; i++) {
4873 if (max_channels == chmode[i].channels) {
4874 ucontrol->value.enumerated.item[0] = i;
4875 break;
4876 }
4877 }
4878 return 0;
4879}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004880EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004881
Takashi Iwaid5191e52009-11-16 14:58:17 +01004882/**
4883 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4884 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004885int snd_hda_ch_mode_put(struct hda_codec *codec,
4886 struct snd_ctl_elem_value *ucontrol,
4887 const struct hda_channel_mode *chmode,
4888 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004889 int *max_channelsp)
4890{
4891 unsigned int mode;
4892
4893 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01004894 if (mode >= num_chmodes)
4895 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004896 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004897 return 0;
4898 /* change the current channel setting */
4899 *max_channelsp = chmode[mode].channels;
4900 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004901 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004902 return 1;
4903}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004904EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004905
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906/*
4907 * input MUX helper
4908 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004909
4910/**
4911 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4912 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004913int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4914 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915{
4916 unsigned int index;
4917
4918 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4919 uinfo->count = 1;
4920 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004921 if (!imux->num_items)
4922 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 index = uinfo->value.enumerated.item;
4924 if (index >= imux->num_items)
4925 index = imux->num_items - 1;
4926 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4927 return 0;
4928}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004929EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930
Takashi Iwaid5191e52009-11-16 14:58:17 +01004931/**
4932 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4933 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004934int snd_hda_input_mux_put(struct hda_codec *codec,
4935 const struct hda_input_mux *imux,
4936 struct snd_ctl_elem_value *ucontrol,
4937 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938 unsigned int *cur_val)
4939{
4940 unsigned int idx;
4941
Takashi Iwai5513b0c2007-10-09 11:58:41 +02004942 if (!imux->num_items)
4943 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 idx = ucontrol->value.enumerated.item[0];
4945 if (idx >= imux->num_items)
4946 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004947 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004949 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4950 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951 *cur_val = idx;
4952 return 1;
4953}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004954EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955
4956
4957/*
Takashi Iwaidda415d2012-11-30 18:34:38 +01004958 * process kcontrol info callback of a simple string enum array
4959 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
4960 */
4961int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
4962 struct snd_ctl_elem_info *uinfo,
4963 int num_items, const char * const *texts)
4964{
4965 static const char * const texts_default[] = {
4966 "Disabled", "Enabled"
4967 };
4968
4969 if (!texts || !num_items) {
4970 num_items = 2;
4971 texts = texts_default;
4972 }
4973
4974 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4975 uinfo->count = 1;
4976 uinfo->value.enumerated.items = num_items;
4977 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
4978 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
4979 strcpy(uinfo->value.enumerated.name,
4980 texts[uinfo->value.enumerated.item]);
4981 return 0;
4982}
4983EXPORT_SYMBOL_HDA(snd_hda_enum_helper_info);
4984
4985/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986 * Multi-channel / digital-out PCM helper functions
4987 */
4988
Takashi Iwai6b97eb42007-04-05 14:51:48 +02004989/* setup SPDIF output stream */
4990static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4991 unsigned int stream_tag, unsigned int format)
4992{
Laurence Darby3bef1c32012-11-03 17:00:06 +00004993 struct hda_spdif_out *spdif;
4994 unsigned int curr_fmt;
4995 bool reset;
Stephen Warren7c935972011-06-01 11:14:17 -06004996
Laurence Darby3bef1c32012-11-03 17:00:06 +00004997 spdif = snd_hda_spdif_out_of_nid(codec, nid);
4998 curr_fmt = snd_hda_codec_read(codec, nid, 0,
4999 AC_VERB_GET_STREAM_FORMAT, 0);
5000 reset = codec->spdif_status_reset &&
5001 (spdif->ctls & AC_DIG1_ENABLE) &&
5002 curr_fmt != format;
5003
5004 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
5005 updated */
5006 if (reset)
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005007 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06005008 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02005009 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005010 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02005011 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02005012 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02005013 for (d = codec->slave_dig_outs; *d; d++)
5014 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
5015 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005016 }
Takashi Iwai2f728532008-09-25 16:32:41 +02005017 /* turn on again (if needed) */
Laurence Darby3bef1c32012-11-03 17:00:06 +00005018 if (reset)
Takashi Iwai2f728532008-09-25 16:32:41 +02005019 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06005020 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02005021}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005022
Takashi Iwai2f728532008-09-25 16:32:41 +02005023static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
5024{
5025 snd_hda_codec_cleanup_stream(codec, nid);
5026 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02005027 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02005028 for (d = codec->slave_dig_outs; *d; d++)
5029 snd_hda_codec_cleanup_stream(codec, *d);
5030 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005031}
5032
Takashi Iwaid5191e52009-11-16 14:58:17 +01005033/**
5034 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
5035 * @bus: HD-audio bus
5036 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005037void snd_hda_bus_reboot_notify(struct hda_bus *bus)
5038{
5039 struct hda_codec *codec;
5040
5041 if (!bus)
5042 return;
5043 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005044 if (hda_codec_is_power_on(codec) &&
5045 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005046 codec->patch_ops.reboot_notify(codec);
5047 }
5048}
Takashi Iwai8f217a22009-11-10 18:26:12 +01005049EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005050
Takashi Iwaid5191e52009-11-16 14:58:17 +01005051/**
5052 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005054int snd_hda_multi_out_dig_open(struct hda_codec *codec,
5055 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056{
Ingo Molnar62932df2006-01-16 16:34:20 +01005057 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02005058 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
5059 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02005060 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01005062 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063 return 0;
5064}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005065EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066
Takashi Iwaid5191e52009-11-16 14:58:17 +01005067/**
5068 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
5069 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005070int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
5071 struct hda_multi_out *mout,
5072 unsigned int stream_tag,
5073 unsigned int format,
5074 struct snd_pcm_substream *substream)
5075{
5076 mutex_lock(&codec->spdif_mutex);
5077 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
5078 mutex_unlock(&codec->spdif_mutex);
5079 return 0;
5080}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005081EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005082
Takashi Iwaid5191e52009-11-16 14:58:17 +01005083/**
5084 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
5085 */
Takashi Iwai9411e212009-02-13 11:32:28 +01005086int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
5087 struct hda_multi_out *mout)
5088{
5089 mutex_lock(&codec->spdif_mutex);
5090 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5091 mutex_unlock(&codec->spdif_mutex);
5092 return 0;
5093}
5094EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
5095
Takashi Iwaid5191e52009-11-16 14:58:17 +01005096/**
5097 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005099int snd_hda_multi_out_dig_close(struct hda_codec *codec,
5100 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101{
Ingo Molnar62932df2006-01-16 16:34:20 +01005102 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01005104 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105 return 0;
5106}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005107EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108
Takashi Iwaid5191e52009-11-16 14:58:17 +01005109/**
5110 * snd_hda_multi_out_analog_open - open analog outputs
5111 *
5112 * Open analog outputs and set up the hw-constraints.
5113 * If the digital outputs can be opened as slave, open the digital
5114 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005116int snd_hda_multi_out_analog_open(struct hda_codec *codec,
5117 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01005118 struct snd_pcm_substream *substream,
5119 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120{
Takashi Iwai9a081602008-02-12 18:37:26 +01005121 struct snd_pcm_runtime *runtime = substream->runtime;
5122 runtime->hw.channels_max = mout->max_channels;
5123 if (mout->dig_out_nid) {
5124 if (!mout->analog_rates) {
5125 mout->analog_rates = hinfo->rates;
5126 mout->analog_formats = hinfo->formats;
5127 mout->analog_maxbps = hinfo->maxbps;
5128 } else {
5129 runtime->hw.rates = mout->analog_rates;
5130 runtime->hw.formats = mout->analog_formats;
5131 hinfo->maxbps = mout->analog_maxbps;
5132 }
5133 if (!mout->spdif_rates) {
5134 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
5135 &mout->spdif_rates,
5136 &mout->spdif_formats,
5137 &mout->spdif_maxbps);
5138 }
5139 mutex_lock(&codec->spdif_mutex);
5140 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02005141 if ((runtime->hw.rates & mout->spdif_rates) &&
5142 (runtime->hw.formats & mout->spdif_formats)) {
5143 runtime->hw.rates &= mout->spdif_rates;
5144 runtime->hw.formats &= mout->spdif_formats;
5145 if (mout->spdif_maxbps < hinfo->maxbps)
5146 hinfo->maxbps = mout->spdif_maxbps;
5147 } else {
5148 mout->share_spdif = 0;
5149 /* FIXME: need notify? */
5150 }
Takashi Iwai9a081602008-02-12 18:37:26 +01005151 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02005152 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01005153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154 return snd_pcm_hw_constraint_step(substream->runtime, 0,
5155 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
5156}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005157EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158
Takashi Iwaid5191e52009-11-16 14:58:17 +01005159/**
5160 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
5161 *
5162 * Set up the i/o for analog out.
5163 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005165int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5166 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167 unsigned int stream_tag,
5168 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01005169 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170{
Takashi Iwaidda14412011-05-02 11:29:30 +02005171 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005173 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174 int i;
5175
Ingo Molnar62932df2006-01-16 16:34:20 +01005176 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005177 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01005178 if (mout->dig_out_nid && mout->share_spdif &&
5179 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02005181 snd_hda_is_supported_format(codec, mout->dig_out_nid,
5182 format) &&
Stephen Warren7c935972011-06-01 11:14:17 -06005183 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005185 setup_dig_out_stream(codec, mout->dig_out_nid,
5186 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187 } else {
5188 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02005189 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190 }
5191 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005192 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193
5194 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005195 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5196 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005197 if (!mout->no_share_stream &&
5198 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005200 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5201 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005202 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005203 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5204 if (!mout->no_share_stream && mout->hp_out_nid[i])
5205 snd_hda_codec_setup_stream(codec,
5206 mout->hp_out_nid[i],
5207 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005208 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02005209 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01005210 snd_hda_codec_setup_stream(codec,
5211 mout->extra_out_nid[i],
5212 stream_tag, 0, format);
5213
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214 /* surrounds */
5215 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02005216 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005217 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5218 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005219 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005220 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5221 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222 }
5223 return 0;
5224}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005225EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226
Takashi Iwaid5191e52009-11-16 14:58:17 +01005227/**
5228 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005230int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5231 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232{
Takashi Iwaidda14412011-05-02 11:29:30 +02005233 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234 int i;
5235
5236 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01005237 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01005239 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005240 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5241 if (mout->hp_out_nid[i])
5242 snd_hda_codec_cleanup_stream(codec,
5243 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005244 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5245 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01005246 snd_hda_codec_cleanup_stream(codec,
5247 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01005248 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02005250 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251 mout->dig_out_used = 0;
5252 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005253 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254 return 0;
5255}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005256EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257
Takashi Iwai47408602012-04-20 13:06:53 +02005258/**
5259 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
5260 *
5261 * Guess the suitable VREF pin bits to be set as the pin-control value.
5262 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5263 */
5264unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5265{
5266 unsigned int pincap;
5267 unsigned int oldval;
5268 oldval = snd_hda_codec_read(codec, pin, 0,
5269 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5270 pincap = snd_hda_query_pin_caps(codec, pin);
5271 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5272 /* Exception: if the default pin setup is vref50, we give it priority */
5273 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5274 return AC_PINCTL_VREF_80;
5275 else if (pincap & AC_PINCAP_VREF_50)
5276 return AC_PINCTL_VREF_50;
5277 else if (pincap & AC_PINCAP_VREF_100)
5278 return AC_PINCTL_VREF_100;
5279 else if (pincap & AC_PINCAP_VREF_GRD)
5280 return AC_PINCTL_VREF_GRD;
5281 return AC_PINCTL_VREF_HIZ;
5282}
5283EXPORT_SYMBOL_HDA(snd_hda_get_default_vref);
5284
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005285/* correct the pin ctl value for matching with the pin cap */
5286unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
5287 hda_nid_t pin, unsigned int val)
5288{
5289 static unsigned int cap_lists[][2] = {
5290 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
5291 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
5292 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
5293 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
5294 };
5295 unsigned int cap;
5296
5297 if (!val)
5298 return 0;
5299 cap = snd_hda_query_pin_caps(codec, pin);
5300 if (!cap)
5301 return val; /* don't know what to do... */
5302
5303 if (val & AC_PINCTL_OUT_EN) {
5304 if (!(cap & AC_PINCAP_OUT))
5305 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5306 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
5307 val &= ~AC_PINCTL_HP_EN;
5308 }
5309
5310 if (val & AC_PINCTL_IN_EN) {
5311 if (!(cap & AC_PINCAP_IN))
5312 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5313 else {
5314 unsigned int vcap, vref;
5315 int i;
5316 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5317 vref = val & AC_PINCTL_VREFEN;
5318 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
5319 if (vref == cap_lists[i][0] &&
5320 !(vcap & cap_lists[i][1])) {
5321 if (i == ARRAY_SIZE(cap_lists) - 1)
5322 vref = AC_PINCTL_VREF_HIZ;
5323 else
5324 vref = cap_lists[i + 1][0];
5325 }
5326 }
5327 val &= ~AC_PINCTL_VREFEN;
5328 val |= vref;
5329 }
5330 }
5331
5332 return val;
5333}
5334EXPORT_SYMBOL_HDA(snd_hda_correct_pin_ctl);
5335
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005336int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5337 unsigned int val, bool cached)
5338{
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005339 val = snd_hda_correct_pin_ctl(codec, pin, val);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01005340 snd_hda_codec_set_pin_target(codec, pin, val);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005341 if (cached)
5342 return snd_hda_codec_update_cache(codec, pin, 0,
5343 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5344 else
5345 return snd_hda_codec_write(codec, pin, 0,
5346 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5347}
5348EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl);
5349
Takashi Iwai990061c2010-09-09 22:08:44 +02005350/**
5351 * snd_hda_add_imux_item - Add an item to input_mux
5352 *
5353 * When the same label is used already in the existing items, the number
5354 * suffix is appended to the label. This label index number is stored
5355 * to type_idx when non-NULL pointer is given.
5356 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005357int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5358 int index, int *type_idx)
5359{
5360 int i, label_idx = 0;
5361 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5362 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5363 return -EINVAL;
5364 }
5365 for (i = 0; i < imux->num_items; i++) {
5366 if (!strncmp(label, imux->items[i].label, strlen(label)))
5367 label_idx++;
5368 }
5369 if (type_idx)
5370 *type_idx = label_idx;
5371 if (label_idx > 0)
5372 snprintf(imux->items[imux->num_items].label,
5373 sizeof(imux->items[imux->num_items].label),
5374 "%s %d", label, label_idx);
5375 else
5376 strlcpy(imux->items[imux->num_items].label, label,
5377 sizeof(imux->items[imux->num_items].label));
5378 imux->items[imux->num_items].index = index;
5379 imux->num_items++;
5380 return 0;
5381}
5382EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005383
Takashi Iwai4a471b72005-12-07 13:56:29 +01005384
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385#ifdef CONFIG_PM
5386/*
5387 * power management
5388 */
5389
5390/**
5391 * snd_hda_suspend - suspend the codecs
5392 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393 *
5394 * Returns 0 if successful.
5395 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005396int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005398 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399
Takashi Iwai0ba21762007-04-16 11:29:14 +02005400 list_for_each_entry(codec, &bus->codec_list, list) {
David Henningsson26a6cb62012-10-09 15:04:21 +02005401 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005402 if (hda_codec_is_power_on(codec))
Dylan Reidd17344b2012-09-28 15:57:01 -07005403 hda_call_codec_suspend(codec, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404 }
5405 return 0;
5406}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005407EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408
5409/**
5410 * snd_hda_resume - resume the codecs
5411 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412 *
5413 * Returns 0 if successful.
5414 */
5415int snd_hda_resume(struct hda_bus *bus)
5416{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005417 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418
Takashi Iwai0ba21762007-04-16 11:29:14 +02005419 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai7f308302012-05-08 16:52:23 +02005420 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422 return 0;
5423}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005424EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005425#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005426
5427/*
5428 * generic arrays
5429 */
5430
Takashi Iwaid5191e52009-11-16 14:58:17 +01005431/**
5432 * snd_array_new - get a new element from the given array
5433 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005434 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005435 * Get a new element from the given array. If it exceeds the
5436 * pre-allocated array size, re-allocate the array.
5437 *
5438 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005439 */
5440void *snd_array_new(struct snd_array *array)
5441{
Takashi Iwai12f17712012-10-10 08:59:14 +02005442 if (snd_BUG_ON(!array->elem_size))
5443 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005444 if (array->used >= array->alloced) {
5445 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005446 int size = (num + 1) * array->elem_size;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005447 int oldsize = array->alloced * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005448 void *nlist;
5449 if (snd_BUG_ON(num >= 4096))
5450 return NULL;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005451 nlist = krealloc(array->list, size, GFP_KERNEL);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005452 if (!nlist)
5453 return NULL;
Takashi Iwai00ef9612011-07-14 15:57:27 +02005454 memset(nlist + oldsize, 0, size - oldsize);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005455 array->list = nlist;
5456 array->alloced = num;
5457 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005458 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005459}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005460EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005461
Takashi Iwaid5191e52009-11-16 14:58:17 +01005462/**
5463 * snd_array_free - free the given array elements
5464 * @array: the array object
5465 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005466void snd_array_free(struct snd_array *array)
5467{
5468 kfree(array->list);
5469 array->used = 0;
5470 array->alloced = 0;
5471 array->list = NULL;
5472}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005473EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005474
Takashi Iwaid5191e52009-11-16 14:58:17 +01005475/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005476 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5477 * @pcm: PCM caps bits
5478 * @buf: the string buffer to write
5479 * @buflen: the max buffer length
5480 *
5481 * used by hda_proc.c and hda_eld.c
5482 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005483void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5484{
5485 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5486 int i, j;
5487
5488 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5489 if (pcm & (AC_SUPPCM_BITS_8 << i))
5490 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5491
5492 buf[j] = '\0'; /* necessary when j == 0 */
5493}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005494EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005495
5496MODULE_DESCRIPTION("HDA codec core");
5497MODULE_LICENSE("GPL");