blob: 39a510699b86e3acd4db9b3ba28f157d97df13c6 [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 Iwai4eea3092013-02-07 18:18:19 +0100385 hda_nid_t list[32];
386 hda_nid_t *result = list;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200387 int len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200388
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200389 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
Takashi Iwai4eea3092013-02-07 18:18:19 +0100390 if (len == -ENOSPC) {
391 len = snd_hda_get_num_raw_conns(codec, nid);
392 result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL);
393 if (!result)
394 return -ENOMEM;
395 len = snd_hda_get_raw_connections(codec, nid, result, len);
396 }
397 if (len >= 0)
398 len = snd_hda_override_conn_list(codec, nid, len, result);
399 if (result != list)
400 kfree(result);
401 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200402}
Takashi Iwaidce20792011-06-24 14:10:28 +0200403
404/**
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100405 * snd_hda_get_conn_list - get connection list
406 * @codec: the HDA codec
407 * @nid: NID to parse
408 * @len: number of connection list entries
409 * @listp: the pointer to store NID list
410 *
411 * Parses the connection list of the given widget and stores the pointer
412 * to the list of NIDs.
413 *
414 * Returns the number of connections, or a negative error code.
415 *
416 * Note that the returned pointer isn't protected against the list
417 * modification. If snd_hda_override_conn_list() might be called
418 * concurrently, protect with a mutex appropriately.
419 */
420int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
421 const hda_nid_t **listp)
422{
423 bool added = false;
424
425 for (;;) {
426 int err;
427 const struct hda_conn_list *p;
428
429 /* if the connection-list is already cached, read it */
430 p = lookup_conn_list(codec, nid);
431 if (p) {
432 if (listp)
433 *listp = p->conns;
434 return p->len;
435 }
436 if (snd_BUG_ON(added))
437 return -EINVAL;
438
439 err = read_and_add_raw_conns(codec, nid);
440 if (err < 0)
441 return err;
442 added = true;
443 }
444}
445EXPORT_SYMBOL_HDA(snd_hda_get_conn_list);
446
447/**
Takashi Iwaidce20792011-06-24 14:10:28 +0200448 * snd_hda_get_connections - copy connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 * @codec: the HDA codec
450 * @nid: NID to parse
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200451 * @conn_list: connection list array; when NULL, checks only the size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 * @max_conns: max. number of connections to store
453 *
454 * Parses the connection list of the given widget and stores the list
455 * of NIDs.
456 *
457 * Returns the number of connections, or a negative error code.
458 */
459int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200460 hda_nid_t *conn_list, int max_conns)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200461{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100462 const hda_nid_t *list;
463 int len = snd_hda_get_conn_list(codec, nid, &list);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200464
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100465 if (len > 0 && conn_list) {
466 if (len > max_conns) {
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200467 snd_printk(KERN_ERR "hda_codec: "
468 "Too many connections %d for NID 0x%x\n",
469 len, nid);
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200470 return -EINVAL;
471 }
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100472 memcpy(conn_list, list, len * sizeof(hda_nid_t));
Takashi Iwaidce20792011-06-24 14:10:28 +0200473 }
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200474
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100475 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200476}
477EXPORT_SYMBOL_HDA(snd_hda_get_connections);
478
Takashi Iwai4eea3092013-02-07 18:18:19 +0100479/* return CONNLIST_LEN parameter of the given widget */
480static unsigned int get_num_conns(struct hda_codec *codec, hda_nid_t nid)
481{
482 unsigned int wcaps = get_wcaps(codec, nid);
483 unsigned int parm;
484
485 if (!(wcaps & AC_WCAP_CONN_LIST) &&
486 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
487 return 0;
488
489 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
490 if (parm == -1)
491 parm = 0;
492 return parm;
493}
494
495int snd_hda_get_num_raw_conns(struct hda_codec *codec, hda_nid_t nid)
496{
497 return get_num_conns(codec, nid) & AC_CLIST_LENGTH;
498}
499
Takashi Iwai9e7717c2011-07-11 15:42:52 +0200500/**
501 * snd_hda_get_raw_connections - copy connection list without cache
502 * @codec: the HDA codec
503 * @nid: NID to parse
504 * @conn_list: connection list array
505 * @max_conns: max. number of connections to store
506 *
507 * Like snd_hda_get_connections(), copy the connection list but without
508 * checking through the connection-list cache.
509 * Currently called only from hda_proc.c, so not exported.
510 */
511int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
512 hda_nid_t *conn_list, int max_conns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
514 unsigned int parm;
Takashi Iwai54d17402005-11-21 16:33:22 +0100515 int i, conn_len, conns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 unsigned int shift, num_elems, mask;
Takashi Iwai54d17402005-11-21 16:33:22 +0100517 hda_nid_t prev_nid;
Takashi Iwai5fdaecd2012-12-20 10:45:55 +0100518 int null_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Takashi Iwaida3cec32008-08-08 17:12:14 +0200520 if (snd_BUG_ON(!conn_list || max_conns <= 0))
521 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Takashi Iwai4eea3092013-02-07 18:18:19 +0100523 parm = get_num_conns(codec, nid);
524 if (!parm)
Takashi Iwai8d087c72011-06-28 12:45:47 +0200525 return 0;
Jaroslav Kysela16a433d2009-07-22 16:20:40 +0200526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 if (parm & AC_CLIST_LONG) {
528 /* long form */
529 shift = 16;
530 num_elems = 2;
531 } else {
532 /* short form */
533 shift = 8;
534 num_elems = 4;
535 }
536 conn_len = parm & AC_CLIST_LENGTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 mask = (1 << (shift-1)) - 1;
538
Takashi Iwai0ba21762007-04-16 11:29:14 +0200539 if (!conn_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 return 0; /* no connection */
541
542 if (conn_len == 1) {
543 /* single connection */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200544 parm = snd_hda_codec_read(codec, nid, 0,
545 AC_VERB_GET_CONNECT_LIST, 0);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200546 if (parm == -1 && codec->bus->rirb_error)
547 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 conn_list[0] = parm & mask;
549 return 1;
550 }
551
552 /* multi connection */
553 conns = 0;
Takashi Iwai54d17402005-11-21 16:33:22 +0100554 prev_nid = 0;
555 for (i = 0; i < conn_len; i++) {
556 int range_val;
557 hda_nid_t val, n;
558
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200559 if (i % num_elems == 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +0100560 parm = snd_hda_codec_read(codec, nid, 0,
561 AC_VERB_GET_CONNECT_LIST, i);
Takashi Iwai3c6aae42009-07-10 12:52:27 +0200562 if (parm == -1 && codec->bus->rirb_error)
563 return -EIO;
564 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200565 range_val = !!(parm & (1 << (shift-1))); /* ranges */
Takashi Iwai54d17402005-11-21 16:33:22 +0100566 val = parm & mask;
Takashi Iwai5fdaecd2012-12-20 10:45:55 +0100567 if (val == 0 && null_count++) { /* no second chance */
Jaroslav Kysela2e9bf242009-07-18 11:48:19 +0200568 snd_printk(KERN_WARNING "hda_codec: "
569 "invalid CONNECT_LIST verb %x[%i]:%x\n",
570 nid, i, parm);
571 return 0;
572 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100573 parm >>= shift;
574 if (range_val) {
575 /* ranges between the previous and this one */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200576 if (!prev_nid || prev_nid >= val) {
577 snd_printk(KERN_WARNING "hda_codec: "
578 "invalid dep_range_val %x:%x\n",
579 prev_nid, val);
Takashi Iwai54d17402005-11-21 16:33:22 +0100580 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100582 for (n = prev_nid + 1; n <= val; n++) {
Takashi Iwai4eea3092013-02-07 18:18:19 +0100583 if (conns >= max_conns)
584 return -ENOSPC;
Takashi Iwai54d17402005-11-21 16:33:22 +0100585 conn_list[conns++] = n;
586 }
587 } else {
Takashi Iwai4eea3092013-02-07 18:18:19 +0100588 if (conns >= max_conns)
589 return -ENOSPC;
Takashi Iwai54d17402005-11-21 16:33:22 +0100590 conn_list[conns++] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 }
Takashi Iwai54d17402005-11-21 16:33:22 +0100592 prev_nid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 }
594 return conns;
595}
596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200598 * snd_hda_override_conn_list - add/modify the connection-list to cache
599 * @codec: the HDA codec
600 * @nid: NID to parse
601 * @len: number of connection list entries
602 * @list: the list of connection entries
603 *
604 * Add or modify the given connection-list to the cache. If the corresponding
605 * cache already exists, invalidate it and append a new one.
606 *
607 * Returns zero or a negative error code.
608 */
609int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
610 const hda_nid_t *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100612 struct hda_conn_list *p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200613
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100614 p = lookup_conn_list(codec, nid);
615 if (p) {
616 list_del(&p->list);
617 kfree(p);
618 }
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200619
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100620 return add_conn_list(codec, nid, len, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621}
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200622EXPORT_SYMBOL_HDA(snd_hda_override_conn_list);
623
624/**
Takashi Iwai8d087c72011-06-28 12:45:47 +0200625 * snd_hda_get_conn_index - get the connection index of the given NID
626 * @codec: the HDA codec
627 * @mux: NID containing the list
628 * @nid: NID to select
629 * @recursive: 1 when searching NID recursively, otherwise 0
630 *
631 * Parses the connection list of the widget @mux and checks whether the
632 * widget @nid is present. If it is, return the connection index.
633 * Otherwise it returns -1.
634 */
635int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
636 hda_nid_t nid, int recursive)
637{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100638 const hda_nid_t *conn;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200639 int i, nums;
640
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100641 nums = snd_hda_get_conn_list(codec, mux, &conn);
Takashi Iwai8d087c72011-06-28 12:45:47 +0200642 for (i = 0; i < nums; i++)
643 if (conn[i] == nid)
644 return i;
645 if (!recursive)
646 return -1;
Takashi Iwaid94ddd82012-12-20 14:42:42 +0100647 if (recursive > 10) {
Takashi Iwai8d087c72011-06-28 12:45:47 +0200648 snd_printd("hda_codec: too deep connection for 0x%x\n", nid);
649 return -1;
650 }
651 recursive++;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200652 for (i = 0; i < nums; i++) {
653 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
654 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
655 continue;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200656 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
657 return i;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200658 }
Takashi Iwai8d087c72011-06-28 12:45:47 +0200659 return -1;
660}
661EXPORT_SYMBOL_HDA(snd_hda_get_conn_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663/**
664 * snd_hda_queue_unsol_event - add an unsolicited event to queue
665 * @bus: the BUS
666 * @res: unsolicited event (lower 32bit of RIRB entry)
667 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
668 *
669 * Adds the given event to the queue. The events are processed in
670 * the workqueue asynchronously. Call this function in the interrupt
671 * hanlder when RIRB receives an unsolicited event.
672 *
673 * Returns 0 if successful, or a negative error code.
674 */
675int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
676{
677 struct hda_bus_unsolicited *unsol;
678 unsigned int wp;
679
Takashi Iwaiecf726f2011-08-09 14:22:44 +0200680 trace_hda_unsol_event(bus, res, res_ex);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200681 unsol = bus->unsol;
682 if (!unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 return 0;
684
685 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
686 unsol->wp = wp;
687
688 wp <<= 1;
689 unsol->queue[wp] = res;
690 unsol->queue[wp + 1] = res_ex;
691
Takashi Iwai6acaed32009-01-12 10:09:24 +0100692 queue_work(bus->workq, &unsol->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694 return 0;
695}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100696EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698/*
Wu Fengguang5c1d1a92008-10-07 14:17:53 +0800699 * process queued unsolicited events
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 */
David Howellsc4028952006-11-22 14:57:56 +0000701static void process_unsol_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
David Howellsc4028952006-11-22 14:57:56 +0000703 struct hda_bus_unsolicited *unsol =
704 container_of(work, struct hda_bus_unsolicited, work);
705 struct hda_bus *bus = unsol->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 struct hda_codec *codec;
707 unsigned int rp, caddr, res;
708
709 while (unsol->rp != unsol->wp) {
710 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
711 unsol->rp = rp;
712 rp <<= 1;
713 res = unsol->queue[rp];
714 caddr = unsol->queue[rp + 1];
Takashi Iwai0ba21762007-04-16 11:29:14 +0200715 if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 continue;
717 codec = bus->caddr_tbl[caddr & 0x0f];
718 if (codec && codec->patch_ops.unsol_event)
719 codec->patch_ops.unsol_event(codec, res);
720 }
721}
722
723/*
724 * initialize unsolicited queue
725 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200726static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
728 struct hda_bus_unsolicited *unsol;
729
Takashi Iwai9f146bb2005-11-17 11:07:49 +0100730 if (bus->unsol) /* already initialized */
731 return 0;
732
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200733 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200734 if (!unsol) {
735 snd_printk(KERN_ERR "hda_codec: "
736 "can't allocate unsolicited queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 return -ENOMEM;
738 }
David Howellsc4028952006-11-22 14:57:56 +0000739 INIT_WORK(&unsol->work, process_unsol_events);
740 unsol->bus = bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 bus->unsol = unsol;
742 return 0;
743}
744
745/*
746 * destructor
747 */
748static void snd_hda_codec_free(struct hda_codec *codec);
749
750static int snd_hda_bus_free(struct hda_bus *bus)
751{
Takashi Iwai0ba21762007-04-16 11:29:14 +0200752 struct hda_codec *codec, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Takashi Iwai0ba21762007-04-16 11:29:14 +0200754 if (!bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 return 0;
Takashi Iwai6acaed32009-01-12 10:09:24 +0100756 if (bus->workq)
757 flush_workqueue(bus->workq);
758 if (bus->unsol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 kfree(bus->unsol);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200760 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 snd_hda_codec_free(codec);
762 }
763 if (bus->ops.private_free)
764 bus->ops.private_free(bus);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100765 if (bus->workq)
766 destroy_workqueue(bus->workq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 kfree(bus);
768 return 0;
769}
770
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100771static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
773 struct hda_bus *bus = device->device_data;
Takashi Iwaib94d35392008-11-21 09:08:06 +0100774 bus->shutdown = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 return snd_hda_bus_free(bus);
776}
777
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200778#ifdef CONFIG_SND_HDA_HWDEP
779static int snd_hda_bus_dev_register(struct snd_device *device)
780{
781 struct hda_bus *bus = device->device_data;
782 struct hda_codec *codec;
783 list_for_each_entry(codec, &bus->codec_list, list) {
784 snd_hda_hwdep_add_sysfs(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +0100785 snd_hda_hwdep_add_power_sysfs(codec);
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200786 }
787 return 0;
788}
789#else
790#define snd_hda_bus_dev_register NULL
791#endif
792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793/**
794 * snd_hda_bus_new - create a HDA bus
795 * @card: the card entry
796 * @temp: the template for hda_bus information
797 * @busp: the pointer to store the created bus instance
798 *
799 * Returns 0 if successful, or a negative error code.
800 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +0100801int snd_hda_bus_new(struct snd_card *card,
Takashi Iwai756e2b02007-04-16 11:27:07 +0200802 const struct hda_bus_template *temp,
803 struct hda_bus **busp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
805 struct hda_bus *bus;
806 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100807 static struct snd_device_ops dev_ops = {
Takashi Iwaid7ffba12008-07-30 15:01:46 +0200808 .dev_register = snd_hda_bus_dev_register,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 .dev_free = snd_hda_bus_dev_free,
810 };
811
Takashi Iwaida3cec32008-08-08 17:12:14 +0200812 if (snd_BUG_ON(!temp))
813 return -EINVAL;
814 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
815 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 if (busp)
818 *busp = NULL;
819
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200820 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 if (bus == NULL) {
822 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
823 return -ENOMEM;
824 }
825
826 bus->card = card;
827 bus->private_data = temp->private_data;
828 bus->pci = temp->pci;
829 bus->modelname = temp->modelname;
Takashi Iwaifee2fba2008-11-27 12:43:28 +0100830 bus->power_save = temp->power_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 bus->ops = temp->ops;
832
Ingo Molnar62932df2006-01-16 16:34:20 +0100833 mutex_init(&bus->cmd_mutex);
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200834 mutex_init(&bus->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 INIT_LIST_HEAD(&bus->codec_list);
836
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100837 snprintf(bus->workq_name, sizeof(bus->workq_name),
838 "hd-audio%d", card->number);
839 bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100840 if (!bus->workq) {
Takashi Iwaie8c0ee52009-02-05 07:34:28 +0100841 snd_printk(KERN_ERR "cannot create workqueue %s\n",
842 bus->workq_name);
Takashi Iwai6acaed32009-01-12 10:09:24 +0100843 kfree(bus);
844 return -ENOMEM;
845 }
846
Takashi Iwai0ba21762007-04-16 11:29:14 +0200847 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
848 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 snd_hda_bus_free(bus);
850 return err;
851 }
852 if (busp)
853 *busp = bus;
854 return 0;
855}
Takashi Iwaiff7a3262008-11-28 15:17:06 +0100856EXPORT_SYMBOL_HDA(snd_hda_bus_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Takashi Iwai82467612007-07-27 19:15:54 +0200858#ifdef CONFIG_SND_HDA_GENERIC
859#define is_generic_config(codec) \
Takashi Iwaif44ac832008-07-30 15:01:45 +0200860 (codec->modelname && !strcmp(codec->modelname, "generic"))
Takashi Iwai82467612007-07-27 19:15:54 +0200861#else
862#define is_generic_config(codec) 0
863#endif
864
Takashi Iwai645f10c2008-11-28 15:07:37 +0100865#ifdef MODULE
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100866#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
867#else
Takashi Iwai645f10c2008-11-28 15:07:37 +0100868#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100869#endif
870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871/*
872 * find a matching codec preset
873 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200874static const struct hda_codec_preset *
Takashi Iwai756e2b02007-04-16 11:27:07 +0200875find_codec_preset(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876{
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100877 struct hda_codec_preset_list *tbl;
878 const struct hda_codec_preset *preset;
Takashi Iwai5d908ab2012-08-24 18:40:10 +0200879 unsigned int mod_requested = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Takashi Iwai82467612007-07-27 19:15:54 +0200881 if (is_generic_config(codec))
Takashi Iwaid5ad6302007-03-07 15:55:59 +0100882 return NULL; /* use the generic parser */
883
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100884 again:
885 mutex_lock(&preset_mutex);
886 list_for_each_entry(tbl, &hda_preset_tables, list) {
887 if (!try_module_get(tbl->owner)) {
888 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
889 continue;
890 }
891 for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 u32 mask = preset->mask;
Marc Boucherca7cfae2008-01-22 15:32:25 +0100893 if (preset->afg && preset->afg != codec->afg)
894 continue;
895 if (preset->mfg && preset->mfg != codec->mfg)
896 continue;
Takashi Iwai0ba21762007-04-16 11:29:14 +0200897 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 mask = ~0;
Takashi Iwai9c7f8522006-06-28 15:08:22 +0200899 if (preset->id == (codec->vendor_id & mask) &&
Takashi Iwai0ba21762007-04-16 11:29:14 +0200900 (!preset->rev ||
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100901 preset->rev == codec->revision_id)) {
902 mutex_unlock(&preset_mutex);
903 codec->owner = tbl->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 return preset;
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100905 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 }
Takashi Iwai1289e9e2008-11-27 15:47:11 +0100907 module_put(tbl->owner);
908 }
909 mutex_unlock(&preset_mutex);
910
911 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
912 char name[32];
913 if (!mod_requested)
914 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
915 codec->vendor_id);
916 else
917 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
918 (codec->vendor_id >> 16) & 0xffff);
919 request_module(name);
920 mod_requested++;
921 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 }
923 return NULL;
924}
925
926/*
Takashi Iwaif44ac832008-07-30 15:01:45 +0200927 * get_codec_name - store the codec name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 */
Takashi Iwaif44ac832008-07-30 15:01:45 +0200929static int get_codec_name(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
931 const struct hda_vendor_id *c;
932 const char *vendor = NULL;
933 u16 vendor_id = codec->vendor_id >> 16;
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200934 char tmp[16];
935
936 if (codec->vendor_name)
937 goto get_chip_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939 for (c = hda_vendor_ids; c->id; c++) {
940 if (c->id == vendor_id) {
941 vendor = c->name;
942 break;
943 }
944 }
Takashi Iwai0ba21762007-04-16 11:29:14 +0200945 if (!vendor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 sprintf(tmp, "Generic %04x", vendor_id);
947 vendor = tmp;
948 }
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200949 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
950 if (!codec->vendor_name)
951 return -ENOMEM;
952
953 get_chip_name:
954 if (codec->chip_name)
955 return 0;
956
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 if (codec->preset && codec->preset->name)
Takashi Iwai812a2cc2009-05-16 10:00:49 +0200958 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
959 else {
960 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
961 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
962 }
963 if (!codec->chip_name)
Takashi Iwaif44ac832008-07-30 15:01:45 +0200964 return -ENOMEM;
965 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966}
967
968/*
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200969 * look for an AFG and MFG nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +0100971static void setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200973 int i, total_nodes, function_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 hda_nid_t nid;
975
976 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
977 for (i = 0; i < total_nodes; i++, nid++) {
Takashi Iwai93e82ae2009-04-17 18:04:41 +0200978 function_id = snd_hda_param_read(codec, nid,
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200979 AC_PAR_FUNCTION_TYPE);
Jaroslav Kyselacd7643b2010-07-20 12:11:25 +0200980 switch (function_id & 0xff) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200981 case AC_GRP_AUDIO_FUNCTION:
982 codec->afg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200983 codec->afg_function_id = function_id & 0xff;
984 codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200985 break;
986 case AC_GRP_MODEM_FUNCTION:
987 codec->mfg = nid;
Jaroslav Kysela79c944a2010-07-19 15:52:39 +0200988 codec->mfg_function_id = function_id & 0xff;
989 codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +0200990 break;
991 default:
992 break;
993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995}
996
997/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100998 * read widget caps for each widget and store in cache
999 */
1000static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
1001{
1002 int i;
1003 hda_nid_t nid;
1004
1005 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
1006 &codec->start_nid);
1007 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001008 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +01001009 return -ENOMEM;
1010 nid = codec->start_nid;
1011 for (i = 0; i < codec->num_nodes; i++, nid++)
1012 codec->wcaps[i] = snd_hda_param_read(codec, nid,
1013 AC_PAR_AUDIO_WIDGET_CAP);
1014 return 0;
1015}
1016
Takashi Iwai3be14142009-02-20 14:11:16 +01001017/* read all pin default configurations and save codec->init_pins */
1018static int read_pin_defaults(struct hda_codec *codec)
1019{
1020 int i;
1021 hda_nid_t nid = codec->start_nid;
1022
1023 for (i = 0; i < codec->num_nodes; i++, nid++) {
1024 struct hda_pincfg *pin;
1025 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +02001026 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +01001027 if (wid_type != AC_WID_PIN)
1028 continue;
1029 pin = snd_array_new(&codec->init_pins);
1030 if (!pin)
1031 return -ENOMEM;
1032 pin->nid = nid;
1033 pin->cfg = snd_hda_codec_read(codec, nid, 0,
1034 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001035 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
1036 AC_VERB_GET_PIN_WIDGET_CONTROL,
1037 0);
Takashi Iwai3be14142009-02-20 14:11:16 +01001038 }
1039 return 0;
1040}
1041
1042/* look up the given pin config list and return the item matching with NID */
1043static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
1044 struct snd_array *array,
1045 hda_nid_t nid)
1046{
1047 int i;
1048 for (i = 0; i < array->used; i++) {
1049 struct hda_pincfg *pin = snd_array_elem(array, i);
1050 if (pin->nid == nid)
1051 return pin;
1052 }
1053 return NULL;
1054}
1055
Takashi Iwai3be14142009-02-20 14:11:16 +01001056/* set the current pin config value for the given NID.
1057 * the value is cached, and read via snd_hda_codec_get_pincfg()
1058 */
1059int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
1060 hda_nid_t nid, unsigned int cfg)
1061{
1062 struct hda_pincfg *pin;
1063
Takashi Iwaib82855a2009-12-27 11:24:56 +01001064 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1065 return -EINVAL;
1066
Takashi Iwai3be14142009-02-20 14:11:16 +01001067 pin = look_up_pincfg(codec, list, nid);
1068 if (!pin) {
1069 pin = snd_array_new(list);
1070 if (!pin)
1071 return -ENOMEM;
1072 pin->nid = nid;
1073 }
1074 pin->cfg = cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001075 return 0;
1076}
1077
Takashi Iwaid5191e52009-11-16 14:58:17 +01001078/**
1079 * snd_hda_codec_set_pincfg - Override a pin default configuration
1080 * @codec: the HDA codec
1081 * @nid: NID to set the pin config
1082 * @cfg: the pin default config value
1083 *
1084 * Override a pin default configuration value in the cache.
1085 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1086 * priority than the real hardware value.
1087 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001088int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1089 hda_nid_t nid, unsigned int cfg)
1090{
Takashi Iwai346ff702009-02-23 09:42:57 +01001091 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001092}
1093EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
1094
Takashi Iwaid5191e52009-11-16 14:58:17 +01001095/**
1096 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1097 * @codec: the HDA codec
1098 * @nid: NID to get the pin config
1099 *
1100 * Get the current pin config value of the given pin NID.
1101 * If the pincfg value is cached or overridden via sysfs or driver,
1102 * returns the cached value.
1103 */
Takashi Iwai3be14142009-02-20 14:11:16 +01001104unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1105{
1106 struct hda_pincfg *pin;
1107
Takashi Iwai3be14142009-02-20 14:11:16 +01001108#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai09b70e82013-01-10 18:21:56 +01001109 {
1110 unsigned int cfg = 0;
1111 mutex_lock(&codec->user_mutex);
1112 pin = look_up_pincfg(codec, &codec->user_pins, nid);
1113 if (pin)
1114 cfg = pin->cfg;
1115 mutex_unlock(&codec->user_mutex);
1116 if (cfg)
1117 return cfg;
1118 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001119#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +01001120 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1121 if (pin)
1122 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +01001123 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1124 if (pin)
1125 return pin->cfg;
1126 return 0;
1127}
1128EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
1129
Takashi Iwaid7fdc002013-01-10 08:38:04 +01001130/* remember the current pinctl target value */
1131int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
1132 unsigned int val)
1133{
1134 struct hda_pincfg *pin;
1135
1136 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1137 if (!pin)
1138 return -EINVAL;
1139 pin->target = val;
1140 return 0;
1141}
1142EXPORT_SYMBOL_HDA(snd_hda_codec_set_pin_target);
1143
1144/* return the current pinctl target value */
1145int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
1146{
1147 struct hda_pincfg *pin;
1148
1149 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1150 if (!pin)
1151 return 0;
1152 return pin->target;
1153}
1154EXPORT_SYMBOL_HDA(snd_hda_codec_get_pin_target);
1155
Takashi Iwai92ee6162009-12-27 11:18:59 +01001156/**
1157 * snd_hda_shutup_pins - Shut up all pins
1158 * @codec: the HDA codec
1159 *
1160 * Clear all pin controls to shup up before suspend for avoiding click noise.
1161 * The controls aren't cached so that they can be resumed properly.
1162 */
1163void snd_hda_shutup_pins(struct hda_codec *codec)
1164{
1165 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001166 /* don't shut up pins when unloading the driver; otherwise it breaks
1167 * the default pin setup at the next load of the driver
1168 */
1169 if (codec->bus->shutdown)
1170 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001171 for (i = 0; i < codec->init_pins.used; i++) {
1172 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1173 /* use read here for syncing after issuing each verb */
1174 snd_hda_codec_read(codec, pin->nid, 0,
1175 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1176 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001177 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +01001178}
1179EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
1180
Takashi Iwai2a439522011-07-26 09:52:50 +02001181#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001182/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1183static void restore_shutup_pins(struct hda_codec *codec)
1184{
1185 int i;
1186 if (!codec->pins_shutup)
1187 return;
1188 if (codec->bus->shutdown)
1189 return;
1190 for (i = 0; i < codec->init_pins.used; i++) {
1191 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1192 snd_hda_codec_write(codec, pin->nid, 0,
1193 AC_VERB_SET_PIN_WIDGET_CONTROL,
1194 pin->ctrl);
1195 }
1196 codec->pins_shutup = 0;
1197}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001198#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +02001199
David Henningsson26a6cb62012-10-09 15:04:21 +02001200static void hda_jackpoll_work(struct work_struct *work)
1201{
1202 struct hda_codec *codec =
1203 container_of(work, struct hda_codec, jackpoll_work.work);
1204 if (!codec->jackpoll_interval)
1205 return;
1206
1207 snd_hda_jack_set_dirty_all(codec);
1208 snd_hda_jack_poll_all(codec);
1209 queue_delayed_work(codec->bus->workq, &codec->jackpoll_work,
1210 codec->jackpoll_interval);
1211}
1212
Takashi Iwai01751f52007-08-10 16:59:39 +02001213static void init_hda_cache(struct hda_cache_rec *cache,
1214 unsigned int record_size);
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001215static void free_hda_cache(struct hda_cache_rec *cache);
Takashi Iwai01751f52007-08-10 16:59:39 +02001216
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001217/* release all pincfg lists */
1218static void free_init_pincfgs(struct hda_codec *codec)
Takashi Iwai3be14142009-02-20 14:11:16 +01001219{
Takashi Iwai346ff702009-02-23 09:42:57 +01001220 snd_array_free(&codec->driver_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001221#ifdef CONFIG_SND_HDA_HWDEP
Takashi Iwai346ff702009-02-23 09:42:57 +01001222 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +01001223#endif
Takashi Iwai3be14142009-02-20 14:11:16 +01001224 snd_array_free(&codec->init_pins);
1225}
1226
Takashi Iwai54d17402005-11-21 16:33:22 +01001227/*
Takashi Iwaieb541332010-08-06 13:48:11 +02001228 * audio-converter setup caches
1229 */
1230struct hda_cvt_setup {
1231 hda_nid_t nid;
1232 u8 stream_tag;
1233 u8 channel_id;
1234 u16 format_id;
1235 unsigned char active; /* cvt is currently used */
1236 unsigned char dirty; /* setups should be cleared */
1237};
1238
1239/* get or create a cache entry for the given audio converter NID */
1240static struct hda_cvt_setup *
1241get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1242{
1243 struct hda_cvt_setup *p;
1244 int i;
1245
1246 for (i = 0; i < codec->cvt_setups.used; i++) {
1247 p = snd_array_elem(&codec->cvt_setups, i);
1248 if (p->nid == nid)
1249 return p;
1250 }
1251 p = snd_array_new(&codec->cvt_setups);
1252 if (p)
1253 p->nid = nid;
1254 return p;
1255}
1256
1257/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 * codec destructor
1259 */
1260static void snd_hda_codec_free(struct hda_codec *codec)
1261{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001262 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 return;
David Henningsson26a6cb62012-10-09 15:04:21 +02001264 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai59cad162012-06-26 15:00:20 +02001265 snd_hda_jack_tbl_clear(codec);
Takashi Iwai3fdf1462012-11-22 08:16:31 +01001266 free_init_pincfgs(codec);
Takashi Iwai83012a72012-08-24 18:38:08 +02001267#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02001268 cancel_delayed_work(&codec->power_work);
Takashi Iwai6acaed32009-01-12 10:09:24 +01001269 flush_workqueue(codec->bus->workq);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001270#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 list_del(&codec->list);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001272 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001273 snd_array_free(&codec->nids);
Takashi Iwai59cad162012-06-26 15:00:20 +02001274 snd_array_free(&codec->cvt_setups);
Stephen Warren7c935972011-06-01 11:14:17 -06001275 snd_array_free(&codec->spdif_out);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001276 remove_conn_list(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 codec->bus->caddr_tbl[codec->addr] = NULL;
1278 if (codec->patch_ops.free)
1279 codec->patch_ops.free(codec);
Takashi Iwai83012a72012-08-24 18:38:08 +02001280#ifdef CONFIG_PM
Takashi Iwai08fa20a2012-08-31 07:46:56 -07001281 if (!codec->pm_down_notified) /* cancel leftover refcounts */
Takashi Iwai68467f52012-08-28 09:14:29 -07001282 hda_call_pm_notify(codec->bus, false);
1283#endif
Takashi Iwai1289e9e2008-11-27 15:47:11 +01001284 module_put(codec->owner);
Takashi Iwai01751f52007-08-10 16:59:39 +02001285 free_hda_cache(&codec->amp_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001286 free_hda_cache(&codec->cmd_cache);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001287 kfree(codec->vendor_name);
1288 kfree(codec->chip_name);
Takashi Iwaif44ac832008-07-30 15:01:45 +02001289 kfree(codec->modelname);
Takashi Iwai54d17402005-11-21 16:33:22 +01001290 kfree(codec->wcaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 kfree(codec);
1292}
1293
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001294static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec,
1295 hda_nid_t fg, unsigned int power_state);
1296
Takashi Iwaid8193872012-08-31 07:54:38 -07001297static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001298 unsigned int power_state);
1299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300/**
1301 * snd_hda_codec_new - create a HDA codec
1302 * @bus: the bus to assign
1303 * @codec_addr: the codec address
1304 * @codecp: the pointer to store the generated codec
1305 *
1306 * Returns 0 if successful, or a negative error code.
1307 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001308int snd_hda_codec_new(struct hda_bus *bus,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001309 unsigned int codec_addr,
1310 struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311{
1312 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +02001313 char component[31];
Takashi Iwaid8193872012-08-31 07:54:38 -07001314 hda_nid_t fg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 int err;
1316
Takashi Iwaida3cec32008-08-08 17:12:14 +02001317 if (snd_BUG_ON(!bus))
1318 return -EINVAL;
1319 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1320 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
1322 if (bus->caddr_tbl[codec_addr]) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001323 snd_printk(KERN_ERR "hda_codec: "
1324 "address 0x%x is already occupied\n", codec_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 return -EBUSY;
1326 }
1327
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001328 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 if (codec == NULL) {
1330 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1331 return -ENOMEM;
1332 }
1333
1334 codec->bus = bus;
1335 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +01001336 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08001337 mutex_init(&codec->control_mutex);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001338 mutex_init(&codec->hash_mutex);
Takashi Iwai01751f52007-08-10 16:59:39 +02001339 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
Takashi Iwaib3ac5632007-08-10 17:03:40 +02001340 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001341 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1342 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +01001343 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +01001344 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +02001345 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Stephen Warren7c935972011-06-01 11:14:17 -06001346 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai361dab32012-05-09 14:35:27 +02001347 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01001348 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
Takashi Iwaiee8e7652013-01-03 15:25:11 +01001349 INIT_LIST_HEAD(&codec->conn_list);
1350
David Henningsson26a6cb62012-10-09 15:04:21 +02001351 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Takashi Iwai83012a72012-08-24 18:38:08 +02001353#ifdef CONFIG_PM
Takashi Iwai5536c6d2012-05-08 17:08:10 +02001354 spin_lock_init(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001355 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1356 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1357 * the caller has to power down appropriatley after initialization
1358 * phase.
1359 */
1360 hda_keep_power_on(codec);
Takashi Iwai68467f52012-08-28 09:14:29 -07001361 hda_call_pm_notify(bus, true);
Takashi Iwaicb53c622007-08-10 17:21:45 +02001362#endif
1363
Takashi Iwaic382a9f2012-05-07 15:01:02 +02001364 if (codec->bus->modelname) {
1365 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1366 if (!codec->modelname) {
1367 snd_hda_codec_free(codec);
1368 return -ENODEV;
1369 }
1370 }
1371
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 list_add_tail(&codec->list, &bus->codec_list);
1373 bus->caddr_tbl[codec_addr] = codec;
1374
Takashi Iwai0ba21762007-04-16 11:29:14 +02001375 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1376 AC_PAR_VENDOR_ID);
Takashi Iwai111d3af2006-02-16 18:17:58 +01001377 if (codec->vendor_id == -1)
1378 /* read again, hopefully the access method was corrected
1379 * in the last read...
1380 */
1381 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1382 AC_PAR_VENDOR_ID);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001383 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1384 AC_PAR_SUBSYSTEM_ID);
1385 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1386 AC_PAR_REV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001388 setup_fg_nodes(codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001389 if (!codec->afg && !codec->mfg) {
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001390 snd_printdd("hda_codec: no AFG or MFG node found\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001391 err = -ENODEV;
1392 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 }
1394
Takashi Iwaid8193872012-08-31 07:54:38 -07001395 fg = codec->afg ? codec->afg : codec->mfg;
1396 err = read_widget_caps(codec, fg);
Takashi Iwai3be14142009-02-20 14:11:16 +01001397 if (err < 0) {
Takashi Iwai54d17402005-11-21 16:33:22 +01001398 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
Takashi Iwai3be14142009-02-20 14:11:16 +01001399 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001400 }
Takashi Iwai3be14142009-02-20 14:11:16 +01001401 err = read_pin_defaults(codec);
1402 if (err < 0)
1403 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +01001404
Takashi Iwai0ba21762007-04-16 11:29:14 +02001405 if (!codec->subsystem_id) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02001406 codec->subsystem_id =
Takashi Iwaid8193872012-08-31 07:54:38 -07001407 snd_hda_codec_read(codec, fg, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001408 AC_VERB_GET_SUBSYSTEM_ID, 0);
Takashi Iwai86284e42005-10-11 15:05:54 +02001409 }
1410
Takashi Iwai83012a72012-08-24 18:38:08 +02001411#ifdef CONFIG_PM
Takashi Iwaid8193872012-08-31 07:54:38 -07001412 codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg,
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001413 AC_PWRST_CLKSTOP);
1414 if (!codec->d3_stop_clk)
1415 bus->power_keep_link_on = 1;
Mengdong Lin5d6147f2012-08-24 12:06:30 +08001416#endif
Takashi Iwaid8193872012-08-31 07:54:38 -07001417 codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
Takashi Iwai983f6b92012-08-28 09:18:01 -07001418 AC_PWRST_EPSS);
Mengdong Linb8dfc4622012-08-23 17:32:30 +08001419
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001420 /* power-up all before initialization */
Takashi Iwaid8193872012-08-31 07:54:38 -07001421 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaibb6ac722009-03-13 09:02:42 +01001422
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001423 snd_hda_codec_proc_new(codec);
1424
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001425 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001426
1427 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1428 codec->subsystem_id, codec->revision_id);
1429 snd_component_add(codec->bus->card, component);
1430
1431 if (codecp)
1432 *codecp = codec;
1433 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +01001434
1435 error:
1436 snd_hda_codec_free(codec);
1437 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001438}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001439EXPORT_SYMBOL_HDA(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001440
Mengdong Lina15d05d2013-02-08 17:09:31 -05001441int snd_hda_codec_update_widgets(struct hda_codec *codec)
1442{
1443 hda_nid_t fg;
1444 int err;
1445
1446 /* Assume the function group node does not change,
1447 * only the widget nodes may change.
1448 */
1449 kfree(codec->wcaps);
1450 fg = codec->afg ? codec->afg : codec->mfg;
1451 err = read_widget_caps(codec, fg);
1452 if (err < 0) {
1453 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
1454 return err;
1455 }
1456
1457 snd_array_free(&codec->init_pins);
1458 err = read_pin_defaults(codec);
1459
1460 return err;
1461}
1462EXPORT_SYMBOL_HDA(snd_hda_codec_update_widgets);
1463
1464
Takashi Iwaid5191e52009-11-16 14:58:17 +01001465/**
1466 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1467 * @codec: the HDA codec
1468 *
1469 * Start parsing of the given codec tree and (re-)initialize the whole
1470 * patch instance.
1471 *
1472 * Returns 0 if successful or a negative error code.
1473 */
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001474int snd_hda_codec_configure(struct hda_codec *codec)
1475{
1476 int err;
1477
Takashi Iwaid5ad6302007-03-07 15:55:59 +01001478 codec->preset = find_codec_preset(codec);
Takashi Iwai812a2cc2009-05-16 10:00:49 +02001479 if (!codec->vendor_name || !codec->chip_name) {
Takashi Iwaif44ac832008-07-30 15:01:45 +02001480 err = get_codec_name(codec);
1481 if (err < 0)
1482 return err;
1483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
Takashi Iwai82467612007-07-27 19:15:54 +02001485 if (is_generic_config(codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai82467612007-07-27 19:15:54 +02001487 goto patched;
1488 }
Takashi Iwai82467612007-07-27 19:15:54 +02001489 if (codec->preset && codec->preset->patch) {
1490 err = codec->preset->patch(codec);
1491 goto patched;
1492 }
1493
1494 /* call the default parser */
Takashi Iwai82467612007-07-27 19:15:54 +02001495 err = snd_hda_parse_generic_codec(codec);
Takashi Iwai35a1e0c2007-10-19 08:13:40 +02001496 if (err < 0)
1497 printk(KERN_ERR "hda-codec: No codec parser is available\n");
Takashi Iwai82467612007-07-27 19:15:54 +02001498
1499 patched:
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001500 if (!err && codec->patch_ops.unsol_event)
1501 err = init_unsol_queue(codec->bus);
Takashi Iwaif62faed2009-12-23 09:27:51 +01001502 /* audio codec should override the mixer name */
1503 if (!err && (codec->afg || !*codec->bus->card->mixername))
1504 snprintf(codec->bus->card->mixername,
1505 sizeof(codec->bus->card->mixername),
1506 "%s %s", codec->vendor_name, codec->chip_name);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001507 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508}
Takashi Iwaia1e21c92009-06-17 09:33:52 +02001509EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Takashi Iwaied360812012-08-08 17:12:52 +02001511/* update the stream-id if changed */
1512static void update_pcm_stream_id(struct hda_codec *codec,
1513 struct hda_cvt_setup *p, hda_nid_t nid,
1514 u32 stream_tag, int channel_id)
1515{
1516 unsigned int oldval, newval;
1517
1518 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1519 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1520 newval = (stream_tag << 4) | channel_id;
1521 if (oldval != newval)
1522 snd_hda_codec_write(codec, nid, 0,
1523 AC_VERB_SET_CHANNEL_STREAMID,
1524 newval);
1525 p->stream_tag = stream_tag;
1526 p->channel_id = channel_id;
1527 }
1528}
1529
1530/* update the format-id if changed */
1531static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1532 hda_nid_t nid, int format)
1533{
1534 unsigned int oldval;
1535
1536 if (p->format_id != format) {
1537 oldval = snd_hda_codec_read(codec, nid, 0,
1538 AC_VERB_GET_STREAM_FORMAT, 0);
1539 if (oldval != format) {
1540 msleep(1);
1541 snd_hda_codec_write(codec, nid, 0,
1542 AC_VERB_SET_STREAM_FORMAT,
1543 format);
1544 }
1545 p->format_id = format;
1546 }
1547}
1548
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549/**
1550 * snd_hda_codec_setup_stream - set up the codec for streaming
1551 * @codec: the CODEC to set up
1552 * @nid: the NID to set up
1553 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1554 * @channel_id: channel id to pass, zero based.
1555 * @format: stream format.
1556 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001557void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1558 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 int channel_id, int format)
1560{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001561 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001562 struct hda_cvt_setup *p;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001563 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001564 int i;
1565
Takashi Iwai0ba21762007-04-16 11:29:14 +02001566 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001567 return;
1568
Takashi Iwai0ba21762007-04-16 11:29:14 +02001569 snd_printdd("hda_codec_setup_stream: "
1570 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001572 p = get_hda_cvt_setup(codec, nid);
Takashi Iwaiaffdb622013-01-07 15:36:45 +01001573 if (!p || p->active)
Takashi Iwaieb541332010-08-06 13:48:11 +02001574 return;
Takashi Iwaied360812012-08-08 17:12:52 +02001575
1576 if (codec->pcm_format_first)
1577 update_pcm_format(codec, p, nid, format);
1578 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1579 if (!codec->pcm_format_first)
1580 update_pcm_format(codec, p, nid, format);
1581
Takashi Iwaieb541332010-08-06 13:48:11 +02001582 p->active = 1;
1583 p->dirty = 0;
1584
1585 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001586 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001587 list_for_each_entry(c, &codec->bus->codec_list, list) {
1588 for (i = 0; i < c->cvt_setups.used; i++) {
1589 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001590 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001591 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001592 p->dirty = 1;
1593 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001596EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
Takashi Iwaif0cea792010-08-13 11:56:53 +02001598static void really_cleanup_stream(struct hda_codec *codec,
1599 struct hda_cvt_setup *q);
1600
Takashi Iwaid5191e52009-11-16 14:58:17 +01001601/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001602 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001603 * @codec: the CODEC to clean up
1604 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001605 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001606 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001607void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1608 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001609{
Takashi Iwaieb541332010-08-06 13:48:11 +02001610 struct hda_cvt_setup *p;
1611
Takashi Iwai888afa12008-03-18 09:57:50 +01001612 if (!nid)
1613 return;
1614
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001615 if (codec->no_sticky_stream)
1616 do_now = 1;
1617
Takashi Iwai888afa12008-03-18 09:57:50 +01001618 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001619 p = get_hda_cvt_setup(codec, nid);
Takashi Iwaiaffdb622013-01-07 15:36:45 +01001620 if (p && p->active) {
Takashi Iwaif0cea792010-08-13 11:56:53 +02001621 /* here we just clear the active flag when do_now isn't set;
1622 * actual clean-ups will be done later in
1623 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1624 */
1625 if (do_now)
1626 really_cleanup_stream(codec, p);
1627 else
1628 p->active = 0;
1629 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001630}
Takashi Iwaif0cea792010-08-13 11:56:53 +02001631EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001632
Takashi Iwaieb541332010-08-06 13:48:11 +02001633static void really_cleanup_stream(struct hda_codec *codec,
1634 struct hda_cvt_setup *q)
1635{
1636 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001637 if (q->stream_tag || q->channel_id)
1638 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1639 if (q->format_id)
1640 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1641);
Takashi Iwaieb541332010-08-06 13:48:11 +02001642 memset(q, 0, sizeof(*q));
1643 q->nid = nid;
1644}
1645
1646/* clean up the all conflicting obsolete streams */
1647static void purify_inactive_streams(struct hda_codec *codec)
1648{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001649 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001650 int i;
1651
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001652 list_for_each_entry(c, &codec->bus->codec_list, list) {
1653 for (i = 0; i < c->cvt_setups.used; i++) {
1654 struct hda_cvt_setup *p;
1655 p = snd_array_elem(&c->cvt_setups, i);
1656 if (p->dirty)
1657 really_cleanup_stream(c, p);
1658 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001659 }
1660}
1661
Takashi Iwai2a439522011-07-26 09:52:50 +02001662#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001663/* clean up all streams; called from suspend */
1664static void hda_cleanup_all_streams(struct hda_codec *codec)
1665{
1666 int i;
1667
1668 for (i = 0; i < codec->cvt_setups.used; i++) {
1669 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1670 if (p->stream_tag)
1671 really_cleanup_stream(codec, p);
1672 }
1673}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001674#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001675
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676/*
1677 * amp access functions
1678 */
1679
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001680/* FIXME: more better hash key? */
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001681#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
Takashi Iwai1327a322009-03-23 13:07:47 +01001682#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001683#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1684#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685#define INFO_AMP_CAPS (1<<0)
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001686#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
1688/* initialize the hash table */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01001689static void init_hda_cache(struct hda_cache_rec *cache,
Takashi Iwai01751f52007-08-10 16:59:39 +02001690 unsigned int record_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691{
Takashi Iwai01751f52007-08-10 16:59:39 +02001692 memset(cache, 0, sizeof(*cache));
1693 memset(cache->hash, 0xff, sizeof(cache->hash));
Takashi Iwai603c4012008-07-30 15:01:44 +02001694 snd_array_init(&cache->buf, record_size, 64);
Takashi Iwai01751f52007-08-10 16:59:39 +02001695}
1696
Takashi Iwai1fcaee62007-08-23 00:01:09 +02001697static void free_hda_cache(struct hda_cache_rec *cache)
Takashi Iwai01751f52007-08-10 16:59:39 +02001698{
Takashi Iwai603c4012008-07-30 15:01:44 +02001699 snd_array_free(&cache->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700}
1701
1702/* query the hash. allocate an entry if not found. */
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001703static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704{
Takashi Iwai01751f52007-08-10 16:59:39 +02001705 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1706 u16 cur = cache->hash[idx];
1707 struct hda_cache_head *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
1709 while (cur != 0xffff) {
Takashi Iwaif43aa022008-11-10 16:24:26 +01001710 info = snd_array_elem(&cache->buf, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 if (info->key == key)
1712 return info;
1713 cur = info->next;
1714 }
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001715 return NULL;
1716}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001718/* query the hash. allocate an entry if not found. */
1719static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1720 u32 key)
1721{
1722 struct hda_cache_head *info = get_hash(cache, key);
1723 if (!info) {
1724 u16 idx, cur;
1725 /* add a new hash entry */
1726 info = snd_array_new(&cache->buf);
1727 if (!info)
1728 return NULL;
1729 cur = snd_array_index(&cache->buf, info);
1730 info->key = key;
1731 info->val = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01001732 info->dirty = 0;
Takashi Iwaia68d5a52010-03-30 18:03:44 +02001733 idx = key % (u16)ARRAY_SIZE(cache->hash);
1734 info->next = cache->hash[idx];
1735 cache->hash[idx] = cur;
1736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 return info;
1738}
1739
Takashi Iwai01751f52007-08-10 16:59:39 +02001740/* query and allocate an amp hash entry */
1741static inline struct hda_amp_info *
1742get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1743{
1744 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1745}
1746
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001747/* overwrite the value with the key in the caps hash */
1748static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1749{
1750 struct hda_amp_info *info;
1751
1752 mutex_lock(&codec->hash_mutex);
1753 info = get_alloc_amp_hash(codec, key);
1754 if (!info) {
1755 mutex_unlock(&codec->hash_mutex);
1756 return -EINVAL;
1757 }
1758 info->amp_caps = val;
1759 info->head.val |= INFO_AMP_CAPS;
1760 mutex_unlock(&codec->hash_mutex);
1761 return 0;
1762}
1763
1764/* query the value from the caps hash; if not found, fetch the current
1765 * value from the given function and store in the hash
1766 */
1767static unsigned int
1768query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1769 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1770{
1771 struct hda_amp_info *info;
1772 unsigned int val;
1773
1774 mutex_lock(&codec->hash_mutex);
1775 info = get_alloc_amp_hash(codec, key);
1776 if (!info) {
1777 mutex_unlock(&codec->hash_mutex);
1778 return 0;
1779 }
1780 if (!(info->head.val & INFO_AMP_CAPS)) {
1781 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1782 val = func(codec, nid, dir);
1783 write_caps_hash(codec, key, val);
1784 } else {
1785 val = info->amp_caps;
1786 mutex_unlock(&codec->hash_mutex);
1787 }
1788 return val;
1789}
1790
1791static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1792 int direction)
1793{
1794 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1795 nid = codec->afg;
1796 return snd_hda_param_read(codec, nid,
1797 direction == HDA_OUTPUT ?
1798 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1799}
1800
Takashi Iwaid5191e52009-11-16 14:58:17 +01001801/**
1802 * query_amp_caps - query AMP capabilities
1803 * @codec: the HD-auio codec
1804 * @nid: the NID to query
1805 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1806 *
1807 * Query AMP capabilities for the given widget and direction.
1808 * Returns the obtained capability bits.
1809 *
1810 * When cap bits have been already read, this doesn't read again but
1811 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001813u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001815 return query_caps_hash(codec, nid, direction,
1816 HDA_HASH_KEY(nid, direction, 0),
1817 read_amp_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001819EXPORT_SYMBOL_HDA(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
Takashi Iwaid5191e52009-11-16 14:58:17 +01001821/**
1822 * snd_hda_override_amp_caps - Override the AMP capabilities
1823 * @codec: the CODEC to clean up
1824 * @nid: the NID to clean up
1825 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1826 * @caps: the capability bits to set
1827 *
1828 * Override the cached AMP caps bits value by the given one.
1829 * This function is useful if the driver needs to adjust the AMP ranges,
1830 * e.g. limit to 0dB, etc.
1831 *
1832 * Returns zero if successful or a negative error code.
1833 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001834int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1835 unsigned int caps)
1836{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001837 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001838}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001839EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001840
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001841static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
1842 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001843{
1844 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1845}
1846
Takashi Iwaid5191e52009-11-16 14:58:17 +01001847/**
1848 * snd_hda_query_pin_caps - Query PIN capabilities
1849 * @codec: the HD-auio codec
1850 * @nid: the NID to query
1851 *
1852 * Query PIN capabilities for the given widget.
1853 * Returns the obtained capability bits.
1854 *
1855 * When cap bits have been already read, this doesn't read again but
1856 * returns the cached value.
1857 */
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001858u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1859{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001860 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01001861 read_pin_cap);
1862}
Takashi Iwai1327a322009-03-23 13:07:47 +01001863EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1864
Wu Fengguang864f92b2009-11-18 12:38:02 +08001865/**
Takashi Iwaif57c2562011-08-15 12:49:07 +02001866 * snd_hda_override_pin_caps - Override the pin capabilities
1867 * @codec: the CODEC
1868 * @nid: the NID to override
1869 * @caps: the capability bits to set
1870 *
1871 * Override the cached PIN capabilitiy bits value by the given one.
1872 *
1873 * Returns zero if successful or a negative error code.
1874 */
1875int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1876 unsigned int caps)
1877{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001878 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
Takashi Iwaif57c2562011-08-15 12:49:07 +02001879}
1880EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps);
1881
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001882/* read or sync the hash value with the current value;
1883 * call within hash_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 */
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001885static struct hda_amp_info *
1886update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
Takashi Iwai280e57d2012-12-14 10:32:21 +01001887 int direction, int index, bool init_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001889 struct hda_amp_info *info;
1890 unsigned int parm, val = 0;
1891 bool val_read = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001893 retry:
1894 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1895 if (!info)
1896 return NULL;
1897 if (!(info->head.val & INFO_AMP_VOL(ch))) {
1898 if (!val_read) {
1899 mutex_unlock(&codec->hash_mutex);
1900 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1901 parm |= direction == HDA_OUTPUT ?
1902 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1903 parm |= index;
1904 val = snd_hda_codec_read(codec, nid, 0,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001905 AC_VERB_GET_AMP_GAIN_MUTE, parm);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001906 val &= 0xff;
1907 val_read = true;
1908 mutex_lock(&codec->hash_mutex);
1909 goto retry;
1910 }
1911 info->vol[ch] = val;
1912 info->head.val |= INFO_AMP_VOL(ch);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001913 } else if (init_only)
1914 return NULL;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001915 return info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916}
1917
1918/*
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001919 * write the current volume in info to the h/w
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 */
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001921static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps,
Takashi Iwai0ba21762007-04-16 11:29:14 +02001922 hda_nid_t nid, int ch, int direction, int index,
1923 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924{
1925 u32 parm;
1926
1927 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1928 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1929 parm |= index << AC_AMP_SET_INDEX_SHIFT;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001930 if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) &&
1931 (amp_caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwai38681372012-02-27 15:00:58 +01001932 ; /* set the zero value as a fake mute */
1933 else
1934 parm |= val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1936}
1937
Takashi Iwaid5191e52009-11-16 14:58:17 +01001938/**
1939 * snd_hda_codec_amp_read - Read AMP value
1940 * @codec: HD-audio codec
1941 * @nid: NID to read the AMP value
1942 * @ch: channel (left=0 or right=1)
1943 * @direction: #HDA_INPUT or #HDA_OUTPUT
1944 * @index: the index value (only for input direction)
1945 *
1946 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 */
Takashi Iwai834be882006-03-01 14:16:17 +01001948int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1949 int direction, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950{
Takashi Iwai0ba21762007-04-16 11:29:14 +02001951 struct hda_amp_info *info;
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001952 unsigned int val = 0;
1953
1954 mutex_lock(&codec->hash_mutex);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001955 info = update_amp_hash(codec, nid, ch, direction, index, false);
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02001956 if (info)
1957 val = info->vol[ch];
1958 mutex_unlock(&codec->hash_mutex);
1959 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01001961EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
Takashi Iwai280e57d2012-12-14 10:32:21 +01001963static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1964 int direction, int idx, int mask, int val,
1965 bool init_only)
1966{
1967 struct hda_amp_info *info;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001968 unsigned int caps;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01001969 unsigned int cache_only;
Takashi Iwai280e57d2012-12-14 10:32:21 +01001970
1971 if (snd_BUG_ON(mask & ~0xff))
1972 mask &= 0xff;
1973 val &= mask;
1974
1975 mutex_lock(&codec->hash_mutex);
1976 info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
1977 if (!info) {
1978 mutex_unlock(&codec->hash_mutex);
1979 return 0;
1980 }
1981 val |= info->vol[ch] & ~mask;
1982 if (info->vol[ch] == val) {
1983 mutex_unlock(&codec->hash_mutex);
1984 return 0;
1985 }
1986 info->vol[ch] = val;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01001987 cache_only = info->head.dirty = codec->cached_write;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001988 caps = info->amp_caps;
Takashi Iwai280e57d2012-12-14 10:32:21 +01001989 mutex_unlock(&codec->hash_mutex);
Takashi Iwaide1e37b2012-12-20 11:00:21 +01001990 if (!cache_only)
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01001991 put_vol_mute(codec, caps, nid, ch, direction, idx, val);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001992 return 1;
1993}
1994
Takashi Iwaid5191e52009-11-16 14:58:17 +01001995/**
1996 * snd_hda_codec_amp_update - update the AMP value
1997 * @codec: HD-audio codec
1998 * @nid: NID to read the AMP value
1999 * @ch: channel (left=0 or right=1)
2000 * @direction: #HDA_INPUT or #HDA_OUTPUT
2001 * @idx: the index value (only for input direction)
2002 * @mask: bit mask to set
2003 * @val: the bits value to set
2004 *
2005 * Update the AMP value with a bit mask.
2006 * Returns 0 if the value is unchanged, 1 if changed.
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002007 */
Takashi Iwai834be882006-03-01 14:16:17 +01002008int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
2009 int direction, int idx, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010{
Takashi Iwai280e57d2012-12-14 10:32:21 +01002011 return codec_amp_update(codec, nid, ch, direction, idx, mask, val, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002013EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
Takashi Iwaid5191e52009-11-16 14:58:17 +01002015/**
2016 * snd_hda_codec_amp_stereo - update the AMP stereo values
2017 * @codec: HD-audio codec
2018 * @nid: NID to read the AMP value
2019 * @direction: #HDA_INPUT or #HDA_OUTPUT
2020 * @idx: the index value (only for input direction)
2021 * @mask: bit mask to set
2022 * @val: the bits value to set
2023 *
2024 * Update the AMP values like snd_hda_codec_amp_update(), but for a
2025 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02002026 */
2027int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
2028 int direction, int idx, int mask, int val)
2029{
2030 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02002031
2032 if (snd_BUG_ON(mask & ~0xff))
2033 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02002034 for (ch = 0; ch < 2; ch++)
2035 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
2036 idx, mask, val);
2037 return ret;
2038}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002039EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02002040
Takashi Iwai280e57d2012-12-14 10:32:21 +01002041/* Works like snd_hda_codec_amp_update() but it writes the value only at
2042 * the first access. If the amp was already initialized / updated beforehand,
2043 * this does nothing.
2044 */
2045int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
2046 int dir, int idx, int mask, int val)
2047{
2048 return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true);
2049}
2050EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init);
2051
2052int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
2053 int dir, int idx, int mask, int val)
2054{
2055 int ch, ret = 0;
2056
2057 if (snd_BUG_ON(mask & ~0xff))
2058 mask &= 0xff;
2059 for (ch = 0; ch < 2; ch++)
2060 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
2061 idx, mask, val);
2062 return ret;
2063}
2064EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init_stereo);
2065
Takashi Iwaid5191e52009-11-16 14:58:17 +01002066/**
2067 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
2068 * @codec: HD-audio codec
2069 *
2070 * Resume the all amp commands from the cache.
2071 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002072void snd_hda_codec_resume_amp(struct hda_codec *codec)
2073{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002074 int i;
2075
Takashi Iwaic370dd62012-12-13 18:30:04 +01002076 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01002077 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002078 for (i = 0; i < codec->amp_cache.buf.used; i++) {
2079 struct hda_amp_info *buffer;
2080 u32 key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002081 hda_nid_t nid;
2082 unsigned int idx, dir, ch;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002083 struct hda_amp_info info;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002084
2085 buffer = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwai8565f052012-12-20 12:54:18 +01002086 if (!buffer->head.dirty)
2087 continue;
2088 buffer->head.dirty = 0;
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002089 info = *buffer;
2090 key = info.head.key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002091 if (!key)
2092 continue;
2093 nid = key & 0xff;
2094 idx = (key >> 16) & 0xff;
2095 dir = (key >> 24) & 0xff;
2096 for (ch = 0; ch < 2; ch++) {
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002097 if (!(info.head.val & INFO_AMP_VOL(ch)))
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002098 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01002099 mutex_unlock(&codec->hash_mutex);
Takashi Iwai2ce4886a2012-12-20 12:58:12 +01002100 put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx,
2101 info.vol[ch]);
Takashi Iwaic370dd62012-12-13 18:30:04 +01002102 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002103 }
2104 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01002105 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02002106}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002107EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002109static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
2110 unsigned int ofs)
2111{
2112 u32 caps = query_amp_caps(codec, nid, dir);
2113 /* get num steps */
2114 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2115 if (ofs < caps)
2116 caps -= ofs;
2117 return caps;
2118}
2119
Takashi Iwaid5191e52009-11-16 14:58:17 +01002120/**
2121 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
2122 *
2123 * The control element is supposed to have the private_value field
2124 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2125 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002126int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
2127 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128{
2129 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2130 u16 nid = get_amp_nid(kcontrol);
2131 u8 chs = get_amp_channels(kcontrol);
2132 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002133 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002135 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2136 uinfo->count = chs == 3 ? 2 : 1;
2137 uinfo->value.integer.min = 0;
2138 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
2139 if (!uinfo->value.integer.max) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002140 printk(KERN_WARNING "hda_codec: "
Takashi Iwai9c8f2ab2008-01-11 16:12:23 +01002141 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
2142 kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 return -EINVAL;
2144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 return 0;
2146}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002147EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002149
2150static inline unsigned int
2151read_amp_value(struct hda_codec *codec, hda_nid_t nid,
2152 int ch, int dir, int idx, unsigned int ofs)
2153{
2154 unsigned int val;
2155 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
2156 val &= HDA_AMP_VOLMASK;
2157 if (val >= ofs)
2158 val -= ofs;
2159 else
2160 val = 0;
2161 return val;
2162}
2163
2164static inline int
2165update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2166 int ch, int dir, int idx, unsigned int ofs,
2167 unsigned int val)
2168{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002169 unsigned int maxval;
2170
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002171 if (val > 0)
2172 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02002173 /* ofs = 0: raw max value */
2174 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02002175 if (val > maxval)
2176 val = maxval;
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002177 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
2178 HDA_AMP_VOLMASK, val);
2179}
2180
Takashi Iwaid5191e52009-11-16 14:58:17 +01002181/**
2182 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
2183 *
2184 * The control element is supposed to have the private_value field
2185 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2186 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002187int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2188 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189{
2190 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2191 hda_nid_t nid = get_amp_nid(kcontrol);
2192 int chs = get_amp_channels(kcontrol);
2193 int dir = get_amp_direction(kcontrol);
2194 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002195 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 long *valp = ucontrol->value.integer.value;
2197
2198 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002199 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002201 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 return 0;
2203}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002204EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
Takashi Iwaid5191e52009-11-16 14:58:17 +01002206/**
2207 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2208 *
2209 * The control element is supposed to have the private_value field
2210 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2211 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002212int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2213 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214{
2215 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2216 hda_nid_t nid = get_amp_nid(kcontrol);
2217 int chs = get_amp_channels(kcontrol);
2218 int dir = get_amp_direction(kcontrol);
2219 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002220 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 long *valp = ucontrol->value.integer.value;
2222 int change = 0;
2223
Takashi Iwaicb53c622007-08-10 17:21:45 +02002224 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002225 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002226 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002227 valp++;
2228 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002229 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002230 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002231 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 return change;
2233}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002234EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
Takashi Iwaid5191e52009-11-16 14:58:17 +01002236/**
2237 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2238 *
2239 * The control element is supposed to have the private_value field
2240 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2241 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002242int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2243 unsigned int size, unsigned int __user *_tlv)
2244{
2245 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2246 hda_nid_t nid = get_amp_nid(kcontrol);
2247 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002248 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02002249 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002250 u32 caps, val1, val2;
2251
2252 if (size < 4 * sizeof(unsigned int))
2253 return -ENOMEM;
2254 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002255 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2256 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002257 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01002258 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002259 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01002260 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02002261 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002262 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2263 return -EFAULT;
2264 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2265 return -EFAULT;
2266 if (put_user(val1, _tlv + 2))
2267 return -EFAULT;
2268 if (put_user(val2, _tlv + 3))
2269 return -EFAULT;
2270 return 0;
2271}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002272EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02002273
Takashi Iwaid5191e52009-11-16 14:58:17 +01002274/**
2275 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2276 * @codec: HD-audio codec
2277 * @nid: NID of a reference widget
2278 * @dir: #HDA_INPUT or #HDA_OUTPUT
2279 * @tlv: TLV data to be stored, at least 4 elements
2280 *
2281 * Set (static) TLV data for a virtual master volume using the AMP caps
2282 * obtained from the reference NID.
2283 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01002284 */
2285void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2286 unsigned int *tlv)
2287{
2288 u32 caps;
2289 int nums, step;
2290
2291 caps = query_amp_caps(codec, nid, dir);
2292 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2293 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2294 step = (step + 1) * 25;
2295 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2296 tlv[1] = 2 * sizeof(unsigned int);
2297 tlv[2] = -nums * step;
2298 tlv[3] = step;
2299}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002300EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002301
2302/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01002303static struct snd_kcontrol *
Takashi Iwaidcda5802012-10-12 17:24:51 +02002304find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002305{
2306 struct snd_ctl_elem_id id;
2307 memset(&id, 0, sizeof(id));
2308 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002309 id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01002310 id.index = idx;
Takashi Iwai18cb71092009-04-16 10:22:24 +02002311 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2312 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002313 strcpy(id.name, name);
2314 return snd_ctl_find_id(codec->bus->card, &id);
2315}
2316
Takashi Iwaid5191e52009-11-16 14:58:17 +01002317/**
2318 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2319 * @codec: HD-audio codec
2320 * @name: ctl id name string
2321 *
2322 * Get the control element with the given id string and IFACE_MIXER.
2323 */
Takashi Iwai09f99702008-02-04 12:31:13 +01002324struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2325 const char *name)
2326{
Takashi Iwaidcda5802012-10-12 17:24:51 +02002327 return find_mixer_ctl(codec, name, 0, 0);
Takashi Iwai09f99702008-02-04 12:31:13 +01002328}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002329EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01002330
Takashi Iwaidcda5802012-10-12 17:24:51 +02002331static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002332 int start_idx)
Takashi Iwai1afe2062010-12-23 10:17:52 +01002333{
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002334 int i, idx;
2335 /* 16 ctlrs should be large enough */
2336 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
2337 if (!find_mixer_ctl(codec, name, 0, idx))
Takashi Iwai1afe2062010-12-23 10:17:52 +01002338 return idx;
2339 }
2340 return -EBUSY;
2341}
2342
Takashi Iwaid5191e52009-11-16 14:58:17 +01002343/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002344 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01002345 * @codec: HD-audio codec
2346 * @nid: corresponding NID (optional)
2347 * @kctl: the control element to assign
2348 *
2349 * Add the given control element to an array inside the codec instance.
2350 * All control elements belonging to a codec are supposed to be added
2351 * by this function so that a proper clean-up works at the free or
2352 * reconfiguration time.
2353 *
2354 * If non-zero @nid is passed, the NID is assigned to the control element.
2355 * The assignment is shown in the codec proc file.
2356 *
2357 * snd_hda_ctl_add() checks the control subdev id field whether
2358 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002359 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2360 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01002361 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002362int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2363 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002364{
2365 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002366 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002367 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002368
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002369 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002370 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002371 if (nid == 0)
2372 nid = get_amp_nid_(kctl->private_value);
2373 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002374 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2375 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01002376 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01002377 kctl->id.subdevice = 0;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002378 err = snd_ctl_add(codec->bus->card, kctl);
2379 if (err < 0)
2380 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002381 item = snd_array_new(&codec->mixers);
2382 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02002383 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002384 item->kctl = kctl;
2385 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01002386 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002387 return 0;
2388}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002389EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002390
Takashi Iwaid5191e52009-11-16 14:58:17 +01002391/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002392 * snd_hda_add_nid - Assign a NID to a control element
2393 * @codec: HD-audio codec
2394 * @nid: corresponding NID (optional)
2395 * @kctl: the control element to assign
2396 * @index: index to kctl
2397 *
2398 * Add the given control element to an array inside the codec instance.
2399 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2400 * NID:KCTL mapping - for example "Capture Source" selector.
2401 */
2402int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2403 unsigned int index, hda_nid_t nid)
2404{
2405 struct hda_nid_item *item;
2406
2407 if (nid > 0) {
2408 item = snd_array_new(&codec->nids);
2409 if (!item)
2410 return -ENOMEM;
2411 item->kctl = kctl;
2412 item->index = index;
2413 item->nid = nid;
2414 return 0;
2415 }
Takashi Iwai28d1a852010-03-15 09:05:46 +01002416 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2417 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002418 return -EINVAL;
2419}
2420EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2421
2422/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01002423 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2424 * @codec: HD-audio codec
2425 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02002426void snd_hda_ctls_clear(struct hda_codec *codec)
2427{
2428 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002429 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02002430 for (i = 0; i < codec->mixers.used; i++)
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002431 snd_ctl_remove(codec->bus->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002432 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01002433 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002434}
2435
Takashi Iwaia65d6292009-02-23 16:57:04 +01002436/* pseudo device locking
2437 * toggle card->shutdown to allow/disallow the device access (as a hack)
2438 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002439int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002440{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002441 struct snd_card *card = bus->card;
2442 struct hda_codec *codec;
2443
Takashi Iwaia65d6292009-02-23 16:57:04 +01002444 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002445 if (card->shutdown)
2446 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002447 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002448 if (!list_empty(&card->ctl_files))
2449 goto err_clear;
2450
2451 list_for_each_entry(codec, &bus->codec_list, list) {
2452 int pcm;
2453 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2454 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2455 if (!cpcm->pcm)
2456 continue;
2457 if (cpcm->pcm->streams[0].substream_opened ||
2458 cpcm->pcm->streams[1].substream_opened)
2459 goto err_clear;
2460 }
2461 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01002462 spin_unlock(&card->files_lock);
2463 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002464
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002465 err_clear:
2466 card->shutdown = 0;
2467 err_unlock:
2468 spin_unlock(&card->files_lock);
2469 return -EINVAL;
2470}
2471EXPORT_SYMBOL_HDA(snd_hda_lock_devices);
2472
2473void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002474{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002475 struct snd_card *card = bus->card;
2476
2477 card = bus->card;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002478 spin_lock(&card->files_lock);
2479 card->shutdown = 0;
2480 spin_unlock(&card->files_lock);
2481}
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002482EXPORT_SYMBOL_HDA(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002483
Takashi Iwaid5191e52009-11-16 14:58:17 +01002484/**
2485 * snd_hda_codec_reset - Clear all objects assigned to the codec
2486 * @codec: HD-audio codec
2487 *
2488 * This frees the all PCM and control elements assigned to the codec, and
2489 * clears the caches and restores the pin default configurations.
2490 *
2491 * When a device is being used, it returns -EBSY. If successfully freed,
2492 * returns zero.
2493 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01002494int snd_hda_codec_reset(struct hda_codec *codec)
2495{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002496 struct hda_bus *bus = codec->bus;
2497 struct snd_card *card = bus->card;
2498 int i;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002499
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002500 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01002501 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002502
2503 /* OK, let it free */
David Henningsson26a6cb62012-10-09 15:04:21 +02002504 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai83012a72012-08-24 18:38:08 +02002505#ifdef CONFIG_PM
Takashi Iwaia2d96e72012-05-09 12:36:22 +02002506 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwai339876d2012-05-08 16:57:12 +02002507 codec->power_on = 0;
2508 codec->power_transition = 0;
2509 codec->power_jiffies = jiffies;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002510 flush_workqueue(bus->workq);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002511#endif
2512 snd_hda_ctls_clear(codec);
2513 /* relase PCMs */
2514 for (i = 0; i < codec->num_pcms; i++) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002515 if (codec->pcm_info[i].pcm) {
Takashi Iwaia65d6292009-02-23 16:57:04 +01002516 snd_device_free(card, codec->pcm_info[i].pcm);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002517 clear_bit(codec->pcm_info[i].device,
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002518 bus->pcm_dev_bits);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01002519 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002520 }
2521 if (codec->patch_ops.free)
2522 codec->patch_ops.free(codec);
Takashi Iwai07dc59f2012-09-10 09:39:31 +02002523 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
Takashi Iwai1835a0f2011-10-27 22:12:46 +02002524 snd_hda_jack_tbl_clear(codec);
Takashi Iwai56d17712008-11-28 14:36:23 +01002525 codec->proc_widget_hook = NULL;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002526 codec->spec = NULL;
2527 free_hda_cache(&codec->amp_cache);
2528 free_hda_cache(&codec->cmd_cache);
Takashi Iwai827057f2008-12-19 10:12:02 +01002529 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2530 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
Takashi Iwai346ff702009-02-23 09:42:57 +01002531 /* free only driver_pins so that init_pins + user_pins are restored */
2532 snd_array_free(&codec->driver_pins);
Takashi Iwai09a60712012-06-26 15:01:33 +02002533 snd_array_free(&codec->cvt_setups);
2534 snd_array_free(&codec->spdif_out);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01002535 snd_array_free(&codec->verbs);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002536 codec->num_pcms = 0;
2537 codec->pcm_info = NULL;
2538 codec->preset = NULL;
Takashi Iwaid1f1af22009-03-02 10:35:29 +01002539 codec->slave_dig_outs = NULL;
2540 codec->spdif_status_reset = 0;
Takashi Iwai1289e9e2008-11-27 15:47:11 +01002541 module_put(codec->owner);
2542 codec->owner = NULL;
Takashi Iwaia65d6292009-02-23 16:57:04 +01002543
2544 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02002545 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01002546 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002547}
2548
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002549typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2550
2551/* apply the function to all matching slave ctls in the mixer list */
2552static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002553 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002554{
2555 struct hda_nid_item *items;
2556 const char * const *s;
2557 int i, err;
2558
2559 items = codec->mixers.list;
2560 for (i = 0; i < codec->mixers.used; i++) {
2561 struct snd_kcontrol *sctl = items[i].kctl;
2562 if (!sctl || !sctl->id.name ||
2563 sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2564 continue;
2565 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01002566 char tmpname[sizeof(sctl->id.name)];
2567 const char *name = *s;
2568 if (suffix) {
2569 snprintf(tmpname, sizeof(tmpname), "%s %s",
2570 name, suffix);
2571 name = tmpname;
2572 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01002573 if (!strcmp(sctl->id.name, name)) {
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002574 err = func(data, sctl);
2575 if (err)
2576 return err;
2577 break;
2578 }
2579 }
2580 }
2581 return 0;
2582}
2583
2584static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2585{
2586 return 1;
2587}
2588
Takashi Iwai18478e82012-03-09 17:51:10 +01002589/* guess the value corresponding to 0dB */
2590static int get_kctl_0dB_offset(struct snd_kcontrol *kctl)
2591{
2592 int _tlv[4];
2593 const int *tlv = NULL;
2594 int val = -1;
2595
2596 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2597 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2598 mm_segment_t fs = get_fs();
2599 set_fs(get_ds());
2600 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2601 tlv = _tlv;
2602 set_fs(fs);
2603 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2604 tlv = kctl->tlv.p;
2605 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE)
2606 val = -tlv[2] / tlv[3];
2607 return val;
2608}
2609
2610/* call kctl->put with the given value(s) */
2611static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2612{
2613 struct snd_ctl_elem_value *ucontrol;
2614 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2615 if (!ucontrol)
2616 return -ENOMEM;
2617 ucontrol->value.integer.value[0] = val;
2618 ucontrol->value.integer.value[1] = val;
2619 kctl->put(kctl, ucontrol);
2620 kfree(ucontrol);
2621 return 0;
2622}
2623
2624/* initialize the slave volume with 0dB */
2625static int init_slave_0dB(void *data, struct snd_kcontrol *slave)
2626{
2627 int offset = get_kctl_0dB_offset(slave);
2628 if (offset > 0)
2629 put_kctl_with_value(slave, offset);
2630 return 0;
2631}
2632
2633/* unmute the slave */
2634static int init_slave_unmute(void *data, struct snd_kcontrol *slave)
2635{
2636 return put_kctl_with_value(slave, 1);
2637}
2638
Takashi Iwaid5191e52009-11-16 14:58:17 +01002639/**
2640 * snd_hda_add_vmaster - create a virtual master control and add slaves
2641 * @codec: HD-audio codec
2642 * @name: vmaster control name
2643 * @tlv: TLV data (optional)
2644 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01002645 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01002646 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01002647 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01002648 *
2649 * Create a virtual master control with the given name. The TLV data
2650 * must be either NULL or a valid data.
2651 *
2652 * @slaves is a NULL-terminated array of strings, each of which is a
2653 * slave control name. All controls with these names are assigned to
2654 * the new virtual master control.
2655 *
2656 * This function returns zero if successful or a negative error code.
2657 */
Takashi Iwai18478e82012-03-09 17:51:10 +01002658int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01002659 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01002660 const char *suffix, bool init_slave_vol,
2661 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01002662{
2663 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002664 int err;
2665
Takashi Iwai29e58532012-03-12 12:25:03 +01002666 if (ctl_ret)
2667 *ctl_ret = NULL;
2668
Takashi Iwai9322ca52012-02-03 14:28:01 +01002669 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002670 if (err != 1) {
Takashi Iwai2f085542008-02-22 18:43:50 +01002671 snd_printdd("No slave found for %s\n", name);
2672 return 0;
2673 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01002674 kctl = snd_ctl_make_virtual_master(name, tlv);
2675 if (!kctl)
2676 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002677 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002678 if (err < 0)
2679 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002680
Takashi Iwai9322ca52012-02-03 14:28:01 +01002681 err = map_slaves(codec, slaves, suffix,
2682 (map_slave_func_t)snd_ctl_add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01002683 if (err < 0)
2684 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01002685
2686 /* init with master mute & zero volume */
2687 put_kctl_with_value(kctl, 0);
2688 if (init_slave_vol)
2689 map_slaves(codec, slaves, suffix,
2690 tlv ? init_slave_0dB : init_slave_unmute, kctl);
2691
Takashi Iwai29e58532012-03-12 12:25:03 +01002692 if (ctl_ret)
2693 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01002694 return 0;
2695}
Takashi Iwai18478e82012-03-09 17:51:10 +01002696EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01002697
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002698/*
2699 * mute-LED control using vmaster
2700 */
2701static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2702 struct snd_ctl_elem_info *uinfo)
2703{
2704 static const char * const texts[] = {
David Henningssonc86c2d42013-01-03 14:12:29 +01002705 "On", "Off", "Follow Master"
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002706 };
2707 unsigned int index;
2708
2709 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2710 uinfo->count = 1;
2711 uinfo->value.enumerated.items = 3;
2712 index = uinfo->value.enumerated.item;
2713 if (index >= 3)
2714 index = 2;
2715 strcpy(uinfo->value.enumerated.name, texts[index]);
2716 return 0;
2717}
2718
2719static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2720 struct snd_ctl_elem_value *ucontrol)
2721{
2722 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2723 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2724 return 0;
2725}
2726
2727static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2728 struct snd_ctl_elem_value *ucontrol)
2729{
2730 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2731 unsigned int old_mode = hook->mute_mode;
2732
2733 hook->mute_mode = ucontrol->value.enumerated.item[0];
2734 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2735 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2736 if (old_mode == hook->mute_mode)
2737 return 0;
2738 snd_hda_sync_vmaster_hook(hook);
2739 return 1;
2740}
2741
2742static struct snd_kcontrol_new vmaster_mute_mode = {
2743 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2744 .name = "Mute-LED Mode",
2745 .info = vmaster_mute_mode_info,
2746 .get = vmaster_mute_mode_get,
2747 .put = vmaster_mute_mode_put,
2748};
2749
2750/*
2751 * Add a mute-LED hook with the given vmaster switch kctl
2752 * "Mute-LED Mode" control is automatically created and associated with
2753 * the given hook.
2754 */
2755int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735c2012-03-13 07:55:10 +01002756 struct hda_vmaster_mute_hook *hook,
2757 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002758{
2759 struct snd_kcontrol *kctl;
2760
2761 if (!hook->hook || !hook->sw_kctl)
2762 return 0;
2763 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2764 hook->codec = codec;
2765 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaif29735c2012-03-13 07:55:10 +01002766 if (!expose_enum_ctl)
2767 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002768 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2769 if (!kctl)
2770 return -ENOMEM;
2771 return snd_hda_ctl_add(codec, 0, kctl);
2772}
2773EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook);
2774
2775/*
2776 * Call the hook with the current value for synchronization
2777 * Should be called in init callback
2778 */
2779void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2780{
2781 if (!hook->hook || !hook->codec)
2782 return;
2783 switch (hook->mute_mode) {
2784 case HDA_VMUTE_FOLLOW_MASTER:
2785 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2786 break;
2787 default:
2788 hook->hook(hook->codec, hook->mute_mode);
2789 break;
2790 }
2791}
2792EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook);
2793
2794
Takashi Iwaid5191e52009-11-16 14:58:17 +01002795/**
2796 * snd_hda_mixer_amp_switch_info - Info 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_info(struct snd_kcontrol *kcontrol,
2802 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803{
2804 int chs = get_amp_channels(kcontrol);
2805
2806 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2807 uinfo->count = chs == 3 ? 2 : 1;
2808 uinfo->value.integer.min = 0;
2809 uinfo->value.integer.max = 1;
2810 return 0;
2811}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002812EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813
Takashi Iwaid5191e52009-11-16 14:58:17 +01002814/**
2815 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2816 *
2817 * The control element is supposed to have the private_value field
2818 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2819 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002820int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2821 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822{
2823 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2824 hda_nid_t nid = get_amp_nid(kcontrol);
2825 int chs = get_amp_channels(kcontrol);
2826 int dir = get_amp_direction(kcontrol);
2827 int idx = get_amp_index(kcontrol);
2828 long *valp = ucontrol->value.integer.value;
2829
2830 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002831 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002832 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002834 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002835 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 return 0;
2837}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002838EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839
Takashi Iwaid5191e52009-11-16 14:58:17 +01002840/**
2841 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2842 *
2843 * The control element is supposed to have the private_value field
2844 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2845 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002846int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2847 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848{
2849 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2850 hda_nid_t nid = get_amp_nid(kcontrol);
2851 int chs = get_amp_channels(kcontrol);
2852 int dir = get_amp_direction(kcontrol);
2853 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 long *valp = ucontrol->value.integer.value;
2855 int change = 0;
2856
Takashi Iwaicb53c622007-08-10 17:21:45 +02002857 snd_hda_power_up(codec);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002858 if (chs & 1) {
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002859 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002860 HDA_AMP_MUTE,
2861 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002862 valp++;
2863 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002864 if (chs & 2)
2865 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
Takashi Iwai47fd8302007-08-10 17:11:07 +02002866 HDA_AMP_MUTE,
2867 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002868 hda_call_check_power_status(codec, nid);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002869 snd_hda_power_down(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 return change;
2871}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002872EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873
2874/*
Takashi Iwai985be542005-11-02 18:26:49 +01002875 * bound volume controls
2876 *
2877 * bind multiple volumes (# indices, from 0)
2878 */
2879
2880#define AMP_VAL_IDX_SHIFT 19
2881#define AMP_VAL_IDX_MASK (0x0f<<19)
2882
Takashi Iwaid5191e52009-11-16 14:58:17 +01002883/**
2884 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2885 *
2886 * The control element is supposed to have the private_value field
2887 * set up via HDA_BIND_MUTE*() macros.
2888 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002889int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2890 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002891{
2892 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2893 unsigned long pval;
2894 int err;
2895
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002896 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002897 pval = kcontrol->private_value;
2898 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2899 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2900 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002901 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002902 return err;
2903}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002904EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002905
Takashi Iwaid5191e52009-11-16 14:58:17 +01002906/**
2907 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2908 *
2909 * The control element is supposed to have the private_value field
2910 * set up via HDA_BIND_MUTE*() macros.
2911 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002912int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2913 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002914{
2915 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2916 unsigned long pval;
2917 int i, indices, err = 0, change = 0;
2918
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002919 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002920 pval = kcontrol->private_value;
2921 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2922 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002923 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2924 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002925 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2926 if (err < 0)
2927 break;
2928 change |= err;
2929 }
2930 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002931 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002932 return err < 0 ? err : change;
2933}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002934EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002935
Takashi Iwaid5191e52009-11-16 14:58:17 +01002936/**
2937 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2938 *
2939 * The control element is supposed to have the private_value field
2940 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002941 */
2942int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2943 struct snd_ctl_elem_info *uinfo)
2944{
2945 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2946 struct hda_bind_ctls *c;
2947 int err;
2948
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002949 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002950 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002951 kcontrol->private_value = *c->values;
2952 err = c->ops->info(kcontrol, uinfo);
2953 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002954 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002955 return err;
2956}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002957EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002958
Takashi Iwaid5191e52009-11-16 14:58:17 +01002959/**
2960 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2961 *
2962 * The control element is supposed to have the private_value field
2963 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2964 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002965int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2966 struct snd_ctl_elem_value *ucontrol)
2967{
2968 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2969 struct hda_bind_ctls *c;
2970 int err;
2971
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002972 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002973 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002974 kcontrol->private_value = *c->values;
2975 err = c->ops->get(kcontrol, ucontrol);
2976 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002977 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002978 return err;
2979}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01002980EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002981
Takashi Iwaid5191e52009-11-16 14:58:17 +01002982/**
2983 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2984 *
2985 * The control element is supposed to have the private_value field
2986 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2987 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002988int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2989 struct snd_ctl_elem_value *ucontrol)
2990{
2991 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2992 struct hda_bind_ctls *c;
2993 unsigned long *vals;
2994 int err = 0, change = 0;
2995
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002996 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002997 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002998 for (vals = c->values; *vals; vals++) {
2999 kcontrol->private_value = *vals;
3000 err = c->ops->put(kcontrol, ucontrol);
3001 if (err < 0)
3002 break;
3003 change |= err;
3004 }
3005 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003006 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003007 return err < 0 ? err : change;
3008}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003009EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02003010
Takashi Iwaid5191e52009-11-16 14:58:17 +01003011/**
3012 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
3013 *
3014 * The control element is supposed to have the private_value field
3015 * set up via HDA_BIND_VOL() macro.
3016 */
Takashi Iwai532d5382007-07-27 19:02:40 +02003017int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
3018 unsigned int size, unsigned int __user *tlv)
3019{
3020 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3021 struct hda_bind_ctls *c;
3022 int err;
3023
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003024 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01003025 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02003026 kcontrol->private_value = *c->values;
3027 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
3028 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08003029 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02003030 return err;
3031}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003032EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02003033
3034struct hda_ctl_ops snd_hda_bind_vol = {
3035 .info = snd_hda_mixer_amp_volume_info,
3036 .get = snd_hda_mixer_amp_volume_get,
3037 .put = snd_hda_mixer_amp_volume_put,
3038 .tlv = snd_hda_mixer_amp_tlv
3039};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003040EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02003041
3042struct hda_ctl_ops snd_hda_bind_sw = {
3043 .info = snd_hda_mixer_amp_switch_info,
3044 .get = snd_hda_mixer_amp_switch_get,
3045 .put = snd_hda_mixer_amp_switch_put,
3046 .tlv = snd_hda_mixer_amp_tlv
3047};
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003048EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02003049
3050/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 * SPDIF out controls
3052 */
3053
Takashi Iwai0ba21762007-04-16 11:29:14 +02003054static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
3055 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056{
3057 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
3058 uinfo->count = 1;
3059 return 0;
3060}
3061
Takashi Iwai0ba21762007-04-16 11:29:14 +02003062static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
3063 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064{
3065 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3066 IEC958_AES0_NONAUDIO |
3067 IEC958_AES0_CON_EMPHASIS_5015 |
3068 IEC958_AES0_CON_NOT_COPYRIGHT;
3069 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
3070 IEC958_AES1_CON_ORIGINAL;
3071 return 0;
3072}
3073
Takashi Iwai0ba21762007-04-16 11:29:14 +02003074static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
3075 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076{
3077 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3078 IEC958_AES0_NONAUDIO |
3079 IEC958_AES0_PRO_EMPHASIS_5015;
3080 return 0;
3081}
3082
Takashi Iwai0ba21762007-04-16 11:29:14 +02003083static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
3084 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085{
3086 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003087 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003088 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003090 mutex_lock(&codec->spdif_mutex);
3091 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003092 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
3093 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
3094 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
3095 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003096 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097
3098 return 0;
3099}
3100
3101/* convert from SPDIF status bits to HDA SPDIF bits
3102 * bit 0 (DigEn) is always set zero (to be filled later)
3103 */
3104static unsigned short convert_from_spdif_status(unsigned int sbits)
3105{
3106 unsigned short val = 0;
3107
3108 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003109 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02003111 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003113 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
3114 IEC958_AES0_PRO_EMPHASIS_5015)
3115 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003117 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
3118 IEC958_AES0_CON_EMPHASIS_5015)
3119 val |= AC_DIG1_EMPHASIS;
3120 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
3121 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02003123 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
3125 }
3126 return val;
3127}
3128
3129/* convert to SPDIF status bits from HDA SPDIF bits
3130 */
3131static unsigned int convert_to_spdif_status(unsigned short val)
3132{
3133 unsigned int sbits = 0;
3134
Takashi Iwai0ba21762007-04-16 11:29:14 +02003135 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003137 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 sbits |= IEC958_AES0_PROFESSIONAL;
3139 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003140 if (sbits & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
3142 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02003143 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003145 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02003147 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3149 sbits |= val & (0x7f << 8);
3150 }
3151 return sbits;
3152}
3153
Takashi Iwai2f728532008-09-25 16:32:41 +02003154/* set digital convert verbs both for the given NID and its slaves */
3155static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3156 int verb, int val)
3157{
Takashi Iwaidda14412011-05-02 11:29:30 +02003158 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02003159
Takashi Iwai9e976972008-11-25 08:17:20 +01003160 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003161 d = codec->slave_dig_outs;
3162 if (!d)
3163 return;
3164 for (; *d; d++)
Takashi Iwai9e976972008-11-25 08:17:20 +01003165 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02003166}
3167
3168static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3169 int dig1, int dig2)
3170{
3171 if (dig1 != -1)
3172 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3173 if (dig2 != -1)
3174 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3175}
3176
Takashi Iwai0ba21762007-04-16 11:29:14 +02003177static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3178 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179{
3180 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003181 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003182 struct hda_spdif_out *spdif;
3183 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 unsigned short val;
3185 int change;
3186
Ingo Molnar62932df2006-01-16 16:34:20 +01003187 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003188 spdif = snd_array_elem(&codec->spdif_out, idx);
3189 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003190 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3192 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3193 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c935972011-06-01 11:14:17 -06003194 val = convert_from_spdif_status(spdif->status);
3195 val |= spdif->ctls & 1;
3196 change = spdif->ctls != val;
3197 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003198 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02003199 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01003200 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 return change;
3202}
3203
Takashi Iwaia5ce8892007-07-23 15:42:26 +02003204#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205
Takashi Iwai0ba21762007-04-16 11:29:14 +02003206static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3207 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208{
3209 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003210 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003211 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003213 mutex_lock(&codec->spdif_mutex);
3214 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06003215 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003216 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 return 0;
3218}
3219
Stephen Warren74b654c2011-06-01 11:14:18 -06003220static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3221 int dig1, int dig2)
3222{
3223 set_dig_out_convert(codec, nid, dig1, dig2);
3224 /* unmute amp switch (if any) */
3225 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3226 (dig1 & AC_DIG1_ENABLE))
3227 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3228 HDA_AMP_MUTE, 0);
3229}
3230
Takashi Iwai0ba21762007-04-16 11:29:14 +02003231static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3232 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233{
3234 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06003235 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003236 struct hda_spdif_out *spdif;
3237 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 unsigned short val;
3239 int change;
3240
Ingo Molnar62932df2006-01-16 16:34:20 +01003241 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003242 spdif = snd_array_elem(&codec->spdif_out, idx);
3243 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06003244 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02003246 val |= AC_DIG1_ENABLE;
Stephen Warren7c935972011-06-01 11:14:17 -06003247 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06003248 spdif->ctls = val;
3249 if (change && nid != (u16)-1)
3250 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01003251 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 return change;
3253}
3254
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003255static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 {
3257 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3258 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003259 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 .info = snd_hda_spdif_mask_info,
3261 .get = snd_hda_spdif_cmask_get,
3262 },
3263 {
3264 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3265 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003266 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 .info = snd_hda_spdif_mask_info,
3268 .get = snd_hda_spdif_pmask_get,
3269 },
3270 {
3271 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003272 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 .info = snd_hda_spdif_mask_info,
3274 .get = snd_hda_spdif_default_get,
3275 .put = snd_hda_spdif_default_put,
3276 },
3277 {
3278 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003279 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 .info = snd_hda_spdif_out_switch_info,
3281 .get = snd_hda_spdif_out_switch_get,
3282 .put = snd_hda_spdif_out_switch_put,
3283 },
3284 { } /* end */
3285};
3286
3287/**
Takashi Iwaidcda5802012-10-12 17:24:51 +02003288 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 * @codec: the HDA codec
Takashi Iwaidcda5802012-10-12 17:24:51 +02003290 * @associated_nid: NID that new ctls associated with
3291 * @cvt_nid: converter NID
3292 * @type: HDA_PCM_TYPE_*
3293 * Creates controls related with the digital output.
3294 * Called from each patch supporting the digital out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 *
3296 * Returns 0 if successful, or a negative error code.
3297 */
Takashi Iwaidcda5802012-10-12 17:24:51 +02003298int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3299 hda_nid_t associated_nid,
3300 hda_nid_t cvt_nid,
3301 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302{
3303 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003304 struct snd_kcontrol *kctl;
3305 struct snd_kcontrol_new *dig_mix;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003306 int idx = 0;
3307 const int spdif_index = 16;
Stephen Warren7c935972011-06-01 11:14:17 -06003308 struct hda_spdif_out *spdif;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003309 struct hda_bus *bus = codec->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003311 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003312 type == HDA_PCM_TYPE_SPDIF) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003313 idx = spdif_index;
3314 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02003315 type == HDA_PCM_TYPE_HDMI) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003316 /* suppose a single SPDIF device */
3317 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3318 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
3319 if (!kctl)
3320 break;
3321 kctl->id.index = spdif_index;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003322 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003323 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003324 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003325 if (!bus->primary_dig_out_type)
3326 bus->primary_dig_out_type = type;
Takashi Iwaidcda5802012-10-12 17:24:51 +02003327
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01003328 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003329 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003330 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
3331 return -EBUSY;
3332 }
Stephen Warren7c935972011-06-01 11:14:17 -06003333 spdif = snd_array_new(&codec->spdif_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3335 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01003336 if (!kctl)
3337 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01003338 kctl->id.index = idx;
Stephen Warren7c935972011-06-01 11:14:17 -06003339 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06003340 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003341 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 return err;
3343 }
Stephen Warren74b654c2011-06-01 11:14:18 -06003344 spdif->nid = cvt_nid;
3345 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
Stephen Warren7c935972011-06-01 11:14:17 -06003346 AC_VERB_GET_DIGI_CONVERT_1, 0);
3347 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 return 0;
3349}
Takashi Iwaidcda5802012-10-12 17:24:51 +02003350EXPORT_SYMBOL_HDA(snd_hda_create_dig_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003352/* get the hda_spdif_out entry from the given NID
3353 * call within spdif_mutex lock
3354 */
Stephen Warren7c935972011-06-01 11:14:17 -06003355struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3356 hda_nid_t nid)
3357{
3358 int i;
3359 for (i = 0; i < codec->spdif_out.used; i++) {
3360 struct hda_spdif_out *spdif =
3361 snd_array_elem(&codec->spdif_out, i);
3362 if (spdif->nid == nid)
3363 return spdif;
3364 }
3365 return NULL;
3366}
3367EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
3368
Stephen Warren74b654c2011-06-01 11:14:18 -06003369void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3370{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003371 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003372
3373 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003374 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003375 spdif->nid = (u16)-1;
3376 mutex_unlock(&codec->spdif_mutex);
3377}
3378EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
3379
3380void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3381{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003382 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06003383 unsigned short val;
3384
3385 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003386 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06003387 if (spdif->nid != nid) {
3388 spdif->nid = nid;
3389 val = spdif->ctls;
3390 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3391 }
3392 mutex_unlock(&codec->spdif_mutex);
3393}
3394EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
3395
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396/*
Takashi Iwai9a081602008-02-12 18:37:26 +01003397 * SPDIF sharing with analog output
3398 */
3399static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3400 struct snd_ctl_elem_value *ucontrol)
3401{
3402 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3403 ucontrol->value.integer.value[0] = mout->share_spdif;
3404 return 0;
3405}
3406
3407static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3408 struct snd_ctl_elem_value *ucontrol)
3409{
3410 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3411 mout->share_spdif = !!ucontrol->value.integer.value[0];
3412 return 0;
3413}
3414
3415static struct snd_kcontrol_new spdif_share_sw = {
3416 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3417 .name = "IEC958 Default PCM Playback Switch",
3418 .info = snd_ctl_boolean_mono_info,
3419 .get = spdif_share_sw_get,
3420 .put = spdif_share_sw_put,
3421};
3422
Takashi Iwaid5191e52009-11-16 14:58:17 +01003423/**
3424 * snd_hda_create_spdif_share_sw - create Default PCM switch
3425 * @codec: the HDA codec
3426 * @mout: multi-out instance
3427 */
Takashi Iwai9a081602008-02-12 18:37:26 +01003428int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3429 struct hda_multi_out *mout)
3430{
3431 if (!mout->dig_out_nid)
3432 return 0;
3433 /* ATTENTION: here mout is passed as private_data, instead of codec */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003434 return snd_hda_ctl_add(codec, mout->dig_out_nid,
3435 snd_ctl_new1(&spdif_share_sw, mout));
Takashi Iwai9a081602008-02-12 18:37:26 +01003436}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003437EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01003438
3439/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 * SPDIF input
3441 */
3442
3443#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3444
Takashi Iwai0ba21762007-04-16 11:29:14 +02003445static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3446 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447{
3448 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3449
3450 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3451 return 0;
3452}
3453
Takashi Iwai0ba21762007-04-16 11:29:14 +02003454static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3455 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456{
3457 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3458 hda_nid_t nid = kcontrol->private_value;
3459 unsigned int val = !!ucontrol->value.integer.value[0];
3460 int change;
3461
Ingo Molnar62932df2006-01-16 16:34:20 +01003462 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003464 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 codec->spdif_in_enable = val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003466 snd_hda_codec_write_cache(codec, nid, 0,
3467 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003469 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 return change;
3471}
3472
Takashi Iwai0ba21762007-04-16 11:29:14 +02003473static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3474 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475{
3476 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3477 hda_nid_t nid = kcontrol->private_value;
3478 unsigned short val;
3479 unsigned int sbits;
3480
Andrew Paprocki3982d172007-12-19 12:13:44 +01003481 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 sbits = convert_to_spdif_status(val);
3483 ucontrol->value.iec958.status[0] = sbits;
3484 ucontrol->value.iec958.status[1] = sbits >> 8;
3485 ucontrol->value.iec958.status[2] = sbits >> 16;
3486 ucontrol->value.iec958.status[3] = sbits >> 24;
3487 return 0;
3488}
3489
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003490static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 {
3492 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003493 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 .info = snd_hda_spdif_in_switch_info,
3495 .get = snd_hda_spdif_in_switch_get,
3496 .put = snd_hda_spdif_in_switch_put,
3497 },
3498 {
3499 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3500 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003501 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 .info = snd_hda_spdif_mask_info,
3503 .get = snd_hda_spdif_in_status_get,
3504 },
3505 { } /* end */
3506};
3507
3508/**
3509 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3510 * @codec: the HDA codec
3511 * @nid: audio in widget NID
3512 *
3513 * Creates controls related with the SPDIF input.
3514 * Called from each patch supporting the SPDIF in.
3515 *
3516 * Returns 0 if successful, or a negative error code.
3517 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02003518int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519{
3520 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003521 struct snd_kcontrol *kctl;
3522 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01003523 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524
Takashi Iwaidcda5802012-10-12 17:24:51 +02003525 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003526 if (idx < 0) {
Takashi Iwai09f99702008-02-04 12:31:13 +01003527 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3528 return -EBUSY;
3529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3531 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01003532 if (!kctl)
3533 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003535 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003536 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 return err;
3538 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02003539 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01003540 snd_hda_codec_read(codec, nid, 0,
3541 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02003542 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 return 0;
3544}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003545EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003547/*
3548 * command cache
3549 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550
Takashi Iwaic370dd62012-12-13 18:30:04 +01003551/* build a 31bit cache key with the widget id and the command parameter */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003552#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3553#define get_cmd_cache_nid(key) ((key) & 0xff)
3554#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3555
3556/**
3557 * snd_hda_codec_write_cache - send a single command with caching
3558 * @codec: the HDA codec
3559 * @nid: NID to send the command
3560 * @direct: direct flag
3561 * @verb: the verb to send
3562 * @parm: the parameter for the verb
3563 *
3564 * Send a single command without waiting for response.
3565 *
3566 * Returns 0 if successful, or a negative error code.
3567 */
3568int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3569 int direct, unsigned int verb, unsigned int parm)
3570{
Takashi Iwaic370dd62012-12-13 18:30:04 +01003571 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003572 struct hda_cache_head *c;
3573 u32 key;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003574 unsigned int cache_only;
Takashi Iwai33fa35e2008-11-06 16:50:40 +01003575
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003576 cache_only = codec->cached_write;
3577 if (!cache_only) {
Takashi Iwaic370dd62012-12-13 18:30:04 +01003578 err = snd_hda_codec_write(codec, nid, direct, verb, parm);
3579 if (err < 0)
3580 return err;
3581 }
3582
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003583 /* parm may contain the verb stuff for get/set amp */
3584 verb = verb | (parm >> 8);
3585 parm &= 0xff;
3586 key = build_cmd_cache_key(nid, verb);
3587 mutex_lock(&codec->bus->cmd_mutex);
3588 c = get_alloc_hash(&codec->cmd_cache, key);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003589 if (c) {
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003590 c->val = parm;
Takashi Iwaide1e37b2012-12-20 11:00:21 +01003591 c->dirty = cache_only;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003592 }
Takashi Iwaiaa2936f2009-05-26 16:07:57 +02003593 mutex_unlock(&codec->bus->cmd_mutex);
3594 return 0;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003595}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003596EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003597
Takashi Iwaid5191e52009-11-16 14:58:17 +01003598/**
Takashi Iwaia68d5a52010-03-30 18:03:44 +02003599 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3600 * @codec: the HDA codec
3601 * @nid: NID to send the command
3602 * @direct: direct flag
3603 * @verb: the verb to send
3604 * @parm: the parameter for the verb
3605 *
3606 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3607 * command if the parameter is already identical with the cached value.
3608 * If not, it sends the command and refreshes the cache.
3609 *
3610 * Returns 0 if successful, or a negative error code.
3611 */
3612int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3613 int direct, unsigned int verb, unsigned int parm)
3614{
3615 struct hda_cache_head *c;
3616 u32 key;
3617
3618 /* parm may contain the verb stuff for get/set amp */
3619 verb = verb | (parm >> 8);
3620 parm &= 0xff;
3621 key = build_cmd_cache_key(nid, verb);
3622 mutex_lock(&codec->bus->cmd_mutex);
3623 c = get_hash(&codec->cmd_cache, key);
3624 if (c && c->val == parm) {
3625 mutex_unlock(&codec->bus->cmd_mutex);
3626 return 0;
3627 }
3628 mutex_unlock(&codec->bus->cmd_mutex);
3629 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3630}
3631EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3632
3633/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003634 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3635 * @codec: HD-audio codec
3636 *
3637 * Execute all verbs recorded in the command caches to resume.
3638 */
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003639void snd_hda_codec_resume_cache(struct hda_codec *codec)
3640{
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003641 int i;
3642
Takashi Iwaic370dd62012-12-13 18:30:04 +01003643 mutex_lock(&codec->hash_mutex);
Takashi Iwaiaa88a352012-12-20 11:02:00 +01003644 codec->cached_write = 0;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003645 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3646 struct hda_cache_head *buffer;
3647 u32 key;
3648
3649 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3650 key = buffer->key;
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003651 if (!key)
3652 continue;
Takashi Iwaic370dd62012-12-13 18:30:04 +01003653 if (!buffer->dirty)
3654 continue;
3655 buffer->dirty = 0;
3656 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003657 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3658 get_cmd_cache_cmd(key), buffer->val);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003659 mutex_lock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003660 }
Takashi Iwaic370dd62012-12-13 18:30:04 +01003661 mutex_unlock(&codec->hash_mutex);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003662}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003663EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003664
3665/**
3666 * snd_hda_sequence_write_cache - sequence writes with caching
3667 * @codec: the HDA codec
3668 * @seq: VERB array to send
3669 *
3670 * Send the commands sequentially from the given array.
3671 * Thte commands are recorded on cache for power-save and resume.
3672 * The array must be terminated with NID=0.
3673 */
3674void snd_hda_sequence_write_cache(struct hda_codec *codec,
3675 const struct hda_verb *seq)
3676{
3677 for (; seq->nid; seq++)
3678 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3679 seq->param);
3680}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003681EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
Takashi Iwaib3ac5632007-08-10 17:03:40 +02003682
Takashi Iwaidc870f32013-01-22 15:24:30 +01003683/**
3684 * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
3685 * @codec: HD-audio codec
3686 */
3687void snd_hda_codec_flush_cache(struct hda_codec *codec)
3688{
3689 snd_hda_codec_resume_amp(codec);
3690 snd_hda_codec_resume_cache(codec);
3691}
3692EXPORT_SYMBOL_HDA(snd_hda_codec_flush_cache);
3693
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003694void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003695 unsigned int power_state)
Takashi Iwai54d17402005-11-21 16:33:22 +01003696{
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003697 hda_nid_t nid = codec->start_nid;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003698 int i;
Takashi Iwai54d17402005-11-21 16:33:22 +01003699
Takashi Iwaicb53c622007-08-10 17:21:45 +02003700 for (i = 0; i < codec->num_nodes; i++, nid++) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01003701 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003702 unsigned int state = power_state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003703 if (!(wcaps & AC_WCAP_POWER))
3704 continue;
Takashi Iwai9419ab62013-01-24 17:23:35 +01003705 if (codec->power_filter) {
3706 state = codec->power_filter(codec, nid, power_state);
3707 if (state != power_state && power_state == AC_PWRST_D3)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003708 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02003709 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003710 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai9419ab62013-01-24 17:23:35 +01003711 state);
Takashi Iwai54d17402005-11-21 16:33:22 +01003712 }
Takashi Iwai54d17402005-11-21 16:33:22 +01003713}
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003714EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3715
3716/*
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003717 * supported power states check
3718 */
3719static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
3720 unsigned int power_state)
3721{
3722 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
3723
Mengdong Line037cb42012-08-10 14:11:58 +02003724 if (sup == -1)
Wang Xingchao0c7f46a2012-06-06 22:02:48 +08003725 return false;
3726 if (sup & power_state)
3727 return true;
3728 else
3729 return false;
3730}
3731
3732/*
Takashi Iwai432c6412012-08-28 09:59:20 -07003733 * wait until the state is reached, returns the current state
3734 */
3735static unsigned int hda_sync_power_state(struct hda_codec *codec,
3736 hda_nid_t fg,
3737 unsigned int power_state)
3738{
3739 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3740 unsigned int state, actual_state;
3741
3742 for (;;) {
3743 state = snd_hda_codec_read(codec, fg, 0,
3744 AC_VERB_GET_POWER_STATE, 0);
3745 if (state & AC_PWRST_ERROR)
3746 break;
3747 actual_state = (state >> 4) & 0x0f;
3748 if (actual_state == power_state)
3749 break;
3750 if (time_after_eq(jiffies, end_time))
3751 break;
3752 /* wait until the codec reachs to the target state */
3753 msleep(1);
3754 }
3755 return state;
3756}
3757
Takashi Iwai9419ab62013-01-24 17:23:35 +01003758/* don't power down the widget if it controls eapd and EAPD_BTLENABLE is set */
Takashi Iwaiba615b82013-03-13 14:47:21 +01003759unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
3760 hda_nid_t nid,
3761 unsigned int power_state)
Takashi Iwai9419ab62013-01-24 17:23:35 +01003762{
3763 if (power_state == AC_PWRST_D3 &&
3764 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
3765 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3766 int eapd = snd_hda_codec_read(codec, nid, 0,
3767 AC_VERB_GET_EAPD_BTLENABLE, 0);
3768 if (eapd & 0x02)
3769 return AC_PWRST_D0;
3770 }
3771 return power_state;
3772}
Takashi Iwaiba615b82013-03-13 14:47:21 +01003773EXPORT_SYMBOL_HDA(snd_hda_codec_eapd_power_filter);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003774
Takashi Iwai432c6412012-08-28 09:59:20 -07003775/*
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003776 * set power state of the codec, and return the power state
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003777 */
Takashi Iwaid8193872012-08-31 07:54:38 -07003778static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003779 unsigned int power_state)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003780{
Takashi Iwaid8193872012-08-31 07:54:38 -07003781 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
Wang Xingchao09617ce2012-06-08 10:26:08 +08003782 int count;
3783 unsigned int state;
3784
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003785 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003786 if (power_state == AC_PWRST_D3) {
3787 /* transition time less than 10ms for power down */
Takashi Iwai983f6b92012-08-28 09:18:01 -07003788 msleep(codec->epss ? 10 : 100);
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08003789 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08003790
3791 /* repeat power states setting at most 10 times*/
3792 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07003793 if (codec->patch_ops.set_power_state)
3794 codec->patch_ops.set_power_state(codec, fg,
3795 power_state);
3796 else {
3797 snd_hda_codec_read(codec, fg, 0,
3798 AC_VERB_SET_POWER_STATE,
3799 power_state);
Takashi Iwai9419ab62013-01-24 17:23:35 +01003800 snd_hda_codec_set_power_to_all(codec, fg, power_state);
Takashi Iwai432c6412012-08-28 09:59:20 -07003801 }
3802 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08003803 if (!(state & AC_PWRST_ERROR))
3804 break;
3805 }
Mengdong Linb8dfc4622012-08-23 17:32:30 +08003806
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003807 return state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02003808}
Takashi Iwai54d17402005-11-21 16:33:22 +01003809
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003810/* sync power states of all widgets;
3811 * this is called at the end of codec parsing
3812 */
3813static void sync_power_up_states(struct hda_codec *codec)
3814{
3815 hda_nid_t nid = codec->start_nid;
3816 int i;
3817
Takashi Iwaiba615b82013-03-13 14:47:21 +01003818 /* don't care if no filter is used */
3819 if (!codec->power_filter)
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003820 return;
3821
3822 for (i = 0; i < codec->num_nodes; i++, nid++) {
3823 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9040d102013-01-24 17:47:17 +01003824 unsigned int target;
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003825 if (!(wcaps & AC_WCAP_POWER))
3826 continue;
3827 target = codec->power_filter(codec, nid, AC_PWRST_D0);
3828 if (target == AC_PWRST_D0)
3829 continue;
Takashi Iwai9040d102013-01-24 17:47:17 +01003830 if (!snd_hda_check_power_state(codec, nid, target))
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003831 snd_hda_codec_write(codec, nid, 0,
3832 AC_VERB_SET_POWER_STATE, target);
3833 }
3834}
3835
Takashi Iwai11aeff02008-07-30 15:01:46 +02003836#ifdef CONFIG_SND_HDA_HWDEP
3837/* execute additional init verbs */
3838static void hda_exec_init_verbs(struct hda_codec *codec)
3839{
3840 if (codec->init_verbs.list)
3841 snd_hda_sequence_write(codec, codec->init_verbs.list);
3842}
3843#else
3844static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3845#endif
3846
Takashi Iwai2a439522011-07-26 09:52:50 +02003847#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003848/*
3849 * call suspend and power-down; used both from PM and power-save
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003850 * this function returns the power state in the end
Takashi Iwaicb53c622007-08-10 17:21:45 +02003851 */
Dylan Reidd17344b2012-09-28 15:57:01 -07003852static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003853{
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003854 unsigned int state;
3855
Takashi Iwai989c3182012-11-19 14:14:58 +01003856 codec->in_pm = 1;
3857
Takashi Iwaicb53c622007-08-10 17:21:45 +02003858 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02003859 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02003860 hda_cleanup_all_streams(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003861 state = hda_set_power_state(codec, AC_PWRST_D3);
Dylan Reidd17344b2012-09-28 15:57:01 -07003862 /* Cancel delayed work if we aren't currently running from it. */
3863 if (!in_wq)
3864 cancel_delayed_work_sync(&codec->power_work);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003865 spin_lock(&codec->power_lock);
3866 snd_hda_update_power_acct(codec);
3867 trace_hda_power_down(codec);
Takashi Iwai95e99fd2007-08-13 15:29:04 +02003868 codec->power_on = 0;
Takashi Iwaia221e282007-08-16 16:35:33 +02003869 codec->power_transition = 0;
Takashi Iwaia2f63092009-11-11 09:34:25 +01003870 codec->power_jiffies = jiffies;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02003871 spin_unlock(&codec->power_lock);
Takashi Iwai989c3182012-11-19 14:14:58 +01003872 codec->in_pm = 0;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003873 return state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003874}
3875
Takashi Iwaic370dd62012-12-13 18:30:04 +01003876/* mark all entries of cmd and amp caches dirty */
3877static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
3878{
3879 int i;
3880 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3881 struct hda_cache_head *cmd;
3882 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
3883 cmd->dirty = 1;
3884 }
3885 for (i = 0; i < codec->amp_cache.buf.used; i++) {
3886 struct hda_amp_info *amp;
David Henningssonf038fca2013-01-15 15:27:19 +01003887 amp = snd_array_elem(&codec->amp_cache.buf, i);
Takashi Iwaic370dd62012-12-13 18:30:04 +01003888 amp->head.dirty = 1;
3889 }
3890}
3891
Takashi Iwaicb53c622007-08-10 17:21:45 +02003892/*
3893 * kick up codec; used both from PM and power-save
3894 */
3895static void hda_call_codec_resume(struct hda_codec *codec)
3896{
Takashi Iwai989c3182012-11-19 14:14:58 +01003897 codec->in_pm = 1;
3898
Takashi Iwaic370dd62012-12-13 18:30:04 +01003899 hda_mark_cmd_cache_dirty(codec);
3900
Takashi Iwai7f308302012-05-08 16:52:23 +02003901 /* set as if powered on for avoiding re-entering the resume
3902 * in the resume / power-save sequence
3903 */
3904 hda_keep_power_on(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003905 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003906 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003907 hda_exec_init_verbs(codec);
Takashi Iwai31614bb2013-01-23 15:58:40 +01003908 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003909 if (codec->patch_ops.resume)
3910 codec->patch_ops.resume(codec);
3911 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003912 if (codec->patch_ops.init)
3913 codec->patch_ops.init(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003914 snd_hda_codec_resume_amp(codec);
3915 snd_hda_codec_resume_cache(codec);
3916 }
David Henningsson26a6cb62012-10-09 15:04:21 +02003917
3918 if (codec->jackpoll_interval)
3919 hda_jackpoll_work(&codec->jackpoll_work.work);
Takashi Iwai31614bb2013-01-23 15:58:40 +01003920 else
David Henningsson26a6cb62012-10-09 15:04:21 +02003921 snd_hda_jack_report_sync(codec);
Takashi Iwai989c3182012-11-19 14:14:58 +01003922
3923 codec->in_pm = 0;
Takashi Iwai7f308302012-05-08 16:52:23 +02003924 snd_hda_power_down(codec); /* flag down before returning */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003925}
Takashi Iwai2a439522011-07-26 09:52:50 +02003926#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003927
Takashi Iwai54d17402005-11-21 16:33:22 +01003928
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929/**
3930 * snd_hda_build_controls - build mixer controls
3931 * @bus: the BUS
3932 *
3933 * Creates mixer controls for each codec included in the bus.
3934 *
3935 * Returns 0 if successful, otherwise a negative error code.
3936 */
Takashi Iwai6a0f56a2012-12-07 07:41:56 +01003937int snd_hda_build_controls(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003939 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940
Takashi Iwai0ba21762007-04-16 11:29:14 +02003941 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003942 int err = snd_hda_codec_build_controls(codec);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003943 if (err < 0) {
Takashi Iwai28d1a852010-03-15 09:05:46 +01003944 printk(KERN_ERR "hda_codec: cannot build controls "
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003945 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwaif93d4612009-03-02 10:44:15 +01003946 err = snd_hda_codec_reset(codec);
3947 if (err < 0) {
3948 printk(KERN_ERR
3949 "hda_codec: cannot revert codec\n");
3950 return err;
3951 }
3952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 }
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003954 return 0;
3955}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01003956EXPORT_SYMBOL_HDA(snd_hda_build_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003958/*
3959 * add standard channel maps if not specified
3960 */
3961static int add_std_chmaps(struct hda_codec *codec)
3962{
3963 int i, str, err;
3964
3965 for (i = 0; i < codec->num_pcms; i++) {
3966 for (str = 0; str < 2; str++) {
3967 struct snd_pcm *pcm = codec->pcm_info[i].pcm;
3968 struct hda_pcm_stream *hinfo =
3969 &codec->pcm_info[i].stream[str];
3970 struct snd_pcm_chmap *chmap;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003971 const struct snd_pcm_chmap_elem *elem;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003972
3973 if (codec->pcm_info[i].own_chmap)
3974 continue;
3975 if (!pcm || !hinfo->substreams)
3976 continue;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003977 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
3978 err = snd_pcm_add_chmap_ctls(pcm, str, elem,
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003979 hinfo->channels_max,
3980 0, &chmap);
3981 if (err < 0)
3982 return err;
3983 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
3984 }
3985 }
3986 return 0;
3987}
3988
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003989/* default channel maps for 2.1 speakers;
3990 * since HD-audio supports only stereo, odd number channels are omitted
3991 */
3992const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
3993 { .channels = 2,
3994 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
3995 { .channels = 4,
3996 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
3997 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
3998 { }
3999};
4000EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
4001
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004002int snd_hda_codec_build_controls(struct hda_codec *codec)
4003{
4004 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02004005 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004006 /* continue to initialize... */
4007 if (codec->patch_ops.init)
4008 err = codec->patch_ops.init(codec);
4009 if (!err && codec->patch_ops.build_controls)
4010 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004011 if (err < 0)
4012 return err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02004013
4014 /* we create chmaps here instead of build_pcms */
4015 err = add_std_chmaps(codec);
4016 if (err < 0)
4017 return err;
4018
David Henningsson26a6cb62012-10-09 15:04:21 +02004019 if (codec->jackpoll_interval)
4020 hda_jackpoll_work(&codec->jackpoll_work.work);
4021 else
4022 snd_hda_jack_report_sync(codec); /* call at the last init point */
Takashi Iwaib9c590b2013-01-24 17:27:32 +01004023 sync_power_up_states(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 return 0;
4025}
4026
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027/*
4028 * stream formats
4029 */
Takashi Iwaibefdf312005-08-22 13:57:55 +02004030struct hda_rate_tbl {
4031 unsigned int hz;
4032 unsigned int alsa_bits;
4033 unsigned int hda_fmt;
4034};
4035
Takashi Iwai92f10b32010-08-03 14:21:00 +02004036/* rate = base * mult / div */
4037#define HDA_RATE(base, mult, div) \
4038 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
4039 (((div) - 1) << AC_FMT_DIV_SHIFT))
4040
Takashi Iwaibefdf312005-08-22 13:57:55 +02004041static struct hda_rate_tbl rate_bits[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 /* rate in Hz, ALSA rate bitmask, HDA format value */
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004043
4044 /* autodetected value used in snd_hda_query_supported_pcm */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004045 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
4046 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
4047 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
4048 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
4049 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
4050 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
4051 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
4052 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
4053 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
4054 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
4055 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004056#define AC_PAR_PCM_RATE_BITS 11
4057 /* up to bits 10, 384kHZ isn't supported properly */
4058
4059 /* not autodetected value */
Takashi Iwai92f10b32010-08-03 14:21:00 +02004060 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
Nicolas Graziano9d8f53f2005-08-22 13:47:16 +02004061
Takashi Iwaibefdf312005-08-22 13:57:55 +02004062 { 0 } /* terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063};
4064
4065/**
4066 * snd_hda_calc_stream_format - calculate format bitset
4067 * @rate: the sample rate
4068 * @channels: the number of channels
4069 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
4070 * @maxbps: the max. bps
4071 *
4072 * Calculate the format bitset from the given rate, channels and th PCM format.
4073 *
4074 * Return zero if invalid.
4075 */
4076unsigned int snd_hda_calc_stream_format(unsigned int rate,
4077 unsigned int channels,
4078 unsigned int format,
Anssi Hannula32c168c2010-08-03 13:28:57 +03004079 unsigned int maxbps,
4080 unsigned short spdif_ctls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081{
4082 int i;
4083 unsigned int val = 0;
4084
Takashi Iwaibefdf312005-08-22 13:57:55 +02004085 for (i = 0; rate_bits[i].hz; i++)
4086 if (rate_bits[i].hz == rate) {
4087 val = rate_bits[i].hda_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 break;
4089 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02004090 if (!rate_bits[i].hz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 snd_printdd("invalid rate %d\n", rate);
4092 return 0;
4093 }
4094
4095 if (channels == 0 || channels > 8) {
4096 snd_printdd("invalid channels %d\n", channels);
4097 return 0;
4098 }
4099 val |= channels - 1;
4100
4101 switch (snd_pcm_format_width(format)) {
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004102 case 8:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004103 val |= AC_FMT_BITS_8;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004104 break;
4105 case 16:
Takashi Iwai92f10b32010-08-03 14:21:00 +02004106 val |= AC_FMT_BITS_16;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004107 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 case 20:
4109 case 24:
4110 case 32:
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004111 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004112 val |= AC_FMT_BITS_32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 else if (maxbps >= 24)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004114 val |= AC_FMT_BITS_24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 else
Takashi Iwai92f10b32010-08-03 14:21:00 +02004116 val |= AC_FMT_BITS_20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 break;
4118 default:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004119 snd_printdd("invalid format width %d\n",
4120 snd_pcm_format_width(format));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121 return 0;
4122 }
4123
Anssi Hannula32c168c2010-08-03 13:28:57 +03004124 if (spdif_ctls & AC_DIG1_NONAUDIO)
Takashi Iwai92f10b32010-08-03 14:21:00 +02004125 val |= AC_FMT_TYPE_NON_PCM;
Anssi Hannula32c168c2010-08-03 13:28:57 +03004126
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 return val;
4128}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004129EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004131static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
4132 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004133{
4134 unsigned int val = 0;
4135 if (nid != codec->afg &&
4136 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
4137 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
4138 if (!val || val == -1)
4139 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
4140 if (!val || val == -1)
4141 return 0;
4142 return val;
4143}
4144
4145static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
4146{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004147 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004148 get_pcm_param);
4149}
4150
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004151static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
4152 int dir)
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004153{
4154 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
4155 if (!streams || streams == -1)
4156 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
4157 if (!streams || streams == -1)
4158 return 0;
4159 return streams;
4160}
4161
4162static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
4163{
Takashi Iwaic3b6bcc2012-05-10 16:11:15 +02004164 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004165 get_stream_param);
4166}
4167
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168/**
4169 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4170 * @codec: the HDA codec
4171 * @nid: NID to query
4172 * @ratesp: the pointer to store the detected rate bitflags
4173 * @formatsp: the pointer to store the detected formats
4174 * @bpsp: the pointer to store the detected format widths
4175 *
4176 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4177 * or @bsps argument is ignored.
4178 *
4179 * Returns 0 if successful, otherwise a negative error code.
4180 */
Stephen Warren384a48d2011-06-01 11:14:21 -06004181int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
4183{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004184 unsigned int i, val, wcaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004186 wcaps = get_wcaps(codec, nid);
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004187 val = query_pcm_param(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188
4189 if (ratesp) {
4190 u32 rates = 0;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004191 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 if (val & (1 << i))
Takashi Iwaibefdf312005-08-22 13:57:55 +02004193 rates |= rate_bits[i].alsa_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004195 if (rates == 0) {
4196 snd_printk(KERN_ERR "hda_codec: rates == 0 "
4197 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
4198 nid, val,
4199 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
4200 return -EIO;
4201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202 *ratesp = rates;
4203 }
4204
4205 if (formatsp || bpsp) {
4206 u64 formats = 0;
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004207 unsigned int streams, bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004209 streams = query_stream_param(codec, nid);
4210 if (!streams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212
4213 bps = 0;
4214 if (streams & AC_SUPFMT_PCM) {
4215 if (val & AC_SUPPCM_BITS_8) {
4216 formats |= SNDRV_PCM_FMTBIT_U8;
4217 bps = 8;
4218 }
4219 if (val & AC_SUPPCM_BITS_16) {
4220 formats |= SNDRV_PCM_FMTBIT_S16_LE;
4221 bps = 16;
4222 }
4223 if (wcaps & AC_WCAP_DIGITAL) {
4224 if (val & AC_SUPPCM_BITS_32)
4225 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4226 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4227 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4228 if (val & AC_SUPPCM_BITS_24)
4229 bps = 24;
4230 else if (val & AC_SUPPCM_BITS_20)
4231 bps = 20;
Takashi Iwai0ba21762007-04-16 11:29:14 +02004232 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4233 AC_SUPPCM_BITS_32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4235 if (val & AC_SUPPCM_BITS_32)
4236 bps = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 else if (val & AC_SUPPCM_BITS_24)
4238 bps = 24;
Nicolas Graziano33ef76512006-09-19 14:23:14 +02004239 else if (val & AC_SUPPCM_BITS_20)
4240 bps = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 }
4242 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004243#if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
Takashi Iwaib5025c52009-07-01 18:05:27 +02004244 if (streams & AC_SUPFMT_FLOAT32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
Takashi Iwaib0bb3aa2009-07-03 23:25:37 +02004246 if (!bps)
4247 bps = 32;
Takashi Iwaib5025c52009-07-01 18:05:27 +02004248 }
Takashi Iwai8c7dd892012-05-12 09:38:05 +02004249#endif
Takashi Iwaib5025c52009-07-01 18:05:27 +02004250 if (streams == AC_SUPFMT_AC3) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02004251 /* should be exclusive */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 /* temporary hack: we have still no proper support
4253 * for the direct AC3 stream...
4254 */
4255 formats |= SNDRV_PCM_FMTBIT_U8;
4256 bps = 8;
4257 }
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004258 if (formats == 0) {
4259 snd_printk(KERN_ERR "hda_codec: formats == 0 "
4260 "(nid=0x%x, val=0x%x, ovrd=%i, "
4261 "streams=0x%x)\n",
4262 nid, val,
4263 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4264 streams);
4265 return -EIO;
4266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267 if (formatsp)
4268 *formatsp = formats;
4269 if (bpsp)
4270 *bpsp = bps;
4271 }
4272
4273 return 0;
4274}
Stephen Warren384a48d2011-06-01 11:14:21 -06004275EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276
4277/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004278 * snd_hda_is_supported_format - Check the validity of the format
4279 * @codec: HD-audio codec
4280 * @nid: NID to check
4281 * @format: the HD-audio format value to check
4282 *
4283 * Check whether the given node supports the format value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 *
4285 * Returns 1 if supported, 0 if not.
4286 */
4287int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4288 unsigned int format)
4289{
4290 int i;
4291 unsigned int val = 0, rate, stream;
4292
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004293 val = query_pcm_param(codec, nid);
4294 if (!val)
4295 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296
4297 rate = format & 0xff00;
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004298 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
Takashi Iwaibefdf312005-08-22 13:57:55 +02004299 if (rate_bits[i].hda_fmt == rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 if (val & (1 << i))
4301 break;
4302 return 0;
4303 }
Takashi Iwaia961f9f2007-04-12 13:08:09 +02004304 if (i >= AC_PAR_PCM_RATE_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305 return 0;
4306
Takashi Iwai92c7c8a2009-03-24 07:32:14 +01004307 stream = query_stream_param(codec, nid);
4308 if (!stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 return 0;
4310
4311 if (stream & AC_SUPFMT_PCM) {
4312 switch (format & 0xf0) {
4313 case 0x00:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004314 if (!(val & AC_SUPPCM_BITS_8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 return 0;
4316 break;
4317 case 0x10:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004318 if (!(val & AC_SUPPCM_BITS_16))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 return 0;
4320 break;
4321 case 0x20:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004322 if (!(val & AC_SUPPCM_BITS_20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 return 0;
4324 break;
4325 case 0x30:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004326 if (!(val & AC_SUPPCM_BITS_24))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 return 0;
4328 break;
4329 case 0x40:
Takashi Iwai0ba21762007-04-16 11:29:14 +02004330 if (!(val & AC_SUPPCM_BITS_32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 return 0;
4332 break;
4333 default:
4334 return 0;
4335 }
4336 } else {
4337 /* FIXME: check for float32 and AC3? */
4338 }
4339
4340 return 1;
4341}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004342EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343
4344/*
4345 * PCM stuff
4346 */
4347static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4348 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004349 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350{
4351 return 0;
4352}
4353
4354static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4355 struct hda_codec *codec,
4356 unsigned int stream_tag,
4357 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004358 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359{
4360 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4361 return 0;
4362}
4363
4364static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4365 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004366 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367{
Takashi Iwai888afa12008-03-18 09:57:50 +01004368 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369 return 0;
4370}
4371
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02004372static int set_pcm_default_values(struct hda_codec *codec,
4373 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004375 int err;
4376
Takashi Iwai0ba21762007-04-16 11:29:14 +02004377 /* query support PCM information from the given NID */
4378 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004379 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02004380 info->rates ? NULL : &info->rates,
4381 info->formats ? NULL : &info->formats,
4382 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01004383 if (err < 0)
4384 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 }
4386 if (info->ops.open == NULL)
4387 info->ops.open = hda_pcm_default_open_close;
4388 if (info->ops.close == NULL)
4389 info->ops.close = hda_pcm_default_open_close;
4390 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004391 if (snd_BUG_ON(!info->nid))
4392 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 info->ops.prepare = hda_pcm_default_prepare;
4394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02004396 if (snd_BUG_ON(!info->nid))
4397 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 info->ops.cleanup = hda_pcm_default_cleanup;
4399 }
4400 return 0;
4401}
4402
Takashi Iwaieb541332010-08-06 13:48:11 +02004403/*
4404 * codec prepare/cleanup entries
4405 */
4406int snd_hda_codec_prepare(struct hda_codec *codec,
4407 struct hda_pcm_stream *hinfo,
4408 unsigned int stream,
4409 unsigned int format,
4410 struct snd_pcm_substream *substream)
4411{
4412 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004413 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004414 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4415 if (ret >= 0)
4416 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004417 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004418 return ret;
4419}
4420EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
4421
4422void snd_hda_codec_cleanup(struct hda_codec *codec,
4423 struct hda_pcm_stream *hinfo,
4424 struct snd_pcm_substream *substream)
4425{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004426 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004427 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02004428 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02004429}
4430EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
4431
Takashi Iwaid5191e52009-11-16 14:58:17 +01004432/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01004433const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4434 "Audio", "SPDIF", "HDMI", "Modem"
4435};
4436
Takashi Iwai176d5332008-07-30 15:01:44 +02004437/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004438 * get the empty PCM device number to assign
Takashi Iwaic8936222010-01-28 17:08:53 +01004439 *
4440 * note the max device number is limited by HDA_MAX_PCMS, currently 10
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004441 */
4442static int get_empty_pcm_device(struct hda_bus *bus, int type)
4443{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004444 /* audio device indices; not linear to keep compatibility */
4445 static int audio_idx[HDA_PCM_NTYPES][5] = {
4446 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4447 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01004448 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004449 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004450 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004451 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004452
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004453 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004454 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
4455 return -EINVAL;
4456 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004457
4458 for (i = 0; audio_idx[type][i] >= 0 ; i++)
4459 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4460 return audio_idx[type][i];
4461
Takashi Iwai01b65bf2011-11-24 14:31:46 +01004462 /* non-fixed slots starting from 10 */
4463 for (i = 10; i < 32; i++) {
4464 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4465 return i;
4466 }
4467
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004468 snd_printk(KERN_WARNING "Too many %s devices\n",
4469 snd_hda_pcm_type_name[type]);
Wu Fengguangf5d6def2009-10-30 11:38:26 +01004470 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004471}
4472
4473/*
Takashi Iwai176d5332008-07-30 15:01:44 +02004474 * attach a new PCM stream
4475 */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004476static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
Takashi Iwai176d5332008-07-30 15:01:44 +02004477{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004478 struct hda_bus *bus = codec->bus;
Takashi Iwai176d5332008-07-30 15:01:44 +02004479 struct hda_pcm_stream *info;
4480 int stream, err;
4481
Takashi Iwaib91f0802008-11-04 08:43:08 +01004482 if (snd_BUG_ON(!pcm->name))
Takashi Iwai176d5332008-07-30 15:01:44 +02004483 return -EINVAL;
4484 for (stream = 0; stream < 2; stream++) {
4485 info = &pcm->stream[stream];
4486 if (info->substreams) {
4487 err = set_pcm_default_values(codec, info);
4488 if (err < 0)
4489 return err;
4490 }
4491 }
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004492 return bus->ops.attach_pcm(bus, codec, pcm);
Takashi Iwai176d5332008-07-30 15:01:44 +02004493}
4494
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004495/* assign all PCMs of the given codec */
4496int snd_hda_codec_build_pcms(struct hda_codec *codec)
4497{
4498 unsigned int pcm;
4499 int err;
4500
4501 if (!codec->num_pcms) {
4502 if (!codec->patch_ops.build_pcms)
4503 return 0;
4504 err = codec->patch_ops.build_pcms(codec);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004505 if (err < 0) {
4506 printk(KERN_ERR "hda_codec: cannot build PCMs"
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004507 "for #%d (error %d)\n", codec->addr, err);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004508 err = snd_hda_codec_reset(codec);
4509 if (err < 0) {
4510 printk(KERN_ERR
4511 "hda_codec: cannot revert codec\n");
4512 return err;
4513 }
4514 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004515 }
4516 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4517 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4518 int dev;
4519
4520 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01004521 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004522
4523 if (!cpcm->pcm) {
4524 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4525 if (dev < 0)
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004526 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004527 cpcm->device = dev;
4528 err = snd_hda_attach_pcm(codec, cpcm);
Takashi Iwai6e655bf2009-03-02 10:46:03 +01004529 if (err < 0) {
4530 printk(KERN_ERR "hda_codec: cannot attach "
4531 "PCM stream %d for codec #%d\n",
4532 dev, codec->addr);
4533 continue; /* no fatal error */
4534 }
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004535 }
4536 }
4537 return 0;
4538}
4539
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540/**
4541 * snd_hda_build_pcms - build PCM information
4542 * @bus: the BUS
4543 *
4544 * Create PCM information for each codec included in the bus.
4545 *
4546 * The build_pcms codec patch is requested to set up codec->num_pcms and
4547 * codec->pcm_info properly. The array is referred by the top-level driver
4548 * to create its PCM instances.
4549 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4550 * callback.
4551 *
4552 * At least, substreams, channels_min and channels_max must be filled for
4553 * each stream. substreams = 0 indicates that the stream doesn't exist.
4554 * When rates and/or formats are zero, the supported values are queried
4555 * from the given nid. The nid is used also by the default ops.prepare
4556 * and ops.cleanup callbacks.
4557 *
4558 * The driver needs to call ops.open in its open callback. Similarly,
4559 * ops.close is supposed to be called in the close callback.
4560 * ops.prepare should be called in the prepare or hw_params callback
4561 * with the proper parameters for set up.
4562 * ops.cleanup should be called in hw_free for clean up of streams.
4563 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004564 * This function returns 0 if successful, or a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 */
Takashi Iwai5cb543d2012-08-09 13:49:23 +02004566int snd_hda_build_pcms(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004568 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569
Takashi Iwai0ba21762007-04-16 11:29:14 +02004570 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai529bd6c2008-11-27 14:17:01 +01004571 int err = snd_hda_codec_build_pcms(codec);
4572 if (err < 0)
4573 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574 }
4575 return 0;
4576}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004577EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579/**
4580 * snd_hda_check_board_config - compare the current codec with the config table
4581 * @codec: the HDA codec
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004582 * @num_configs: number of config enums
4583 * @models: array of model name strings
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 * @tbl: configuration table, terminated by null entries
4585 *
4586 * Compares the modelname or PCI subsystem id of the current codec with the
4587 * given configuration table. If a matching entry is found, returns its
4588 * config value (supposed to be 0 or positive).
4589 *
4590 * If no entries are matching, the function returns a negative value.
4591 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02004592int snd_hda_check_board_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004593 int num_configs, const char * const *models,
Takashi Iwai12f288b2007-08-02 15:51:59 +02004594 const struct snd_pci_quirk *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595{
Takashi Iwaif44ac832008-07-30 15:01:45 +02004596 if (codec->modelname && models) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004597 int i;
4598 for (i = 0; i < num_configs; i++) {
4599 if (models[i] &&
Takashi Iwaif44ac832008-07-30 15:01:45 +02004600 !strcmp(codec->modelname, models[i])) {
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004601 snd_printd(KERN_INFO "hda_codec: model '%s' is "
4602 "selected\n", models[i]);
4603 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604 }
4605 }
4606 }
4607
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004608 if (!codec->bus->pci || !tbl)
4609 return -1;
4610
4611 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
4612 if (!tbl)
4613 return -1;
4614 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwai62cf8722008-05-20 12:15:15 +02004615#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004616 char tmp[10];
4617 const char *model = NULL;
4618 if (models)
4619 model = models[tbl->value];
4620 if (!model) {
4621 sprintf(tmp, "#%d", tbl->value);
4622 model = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623 }
Takashi Iwaif5fcc132006-11-24 17:07:44 +01004624 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4625 "for config %x:%x (%s)\n",
4626 model, tbl->subvendor, tbl->subdevice,
4627 (tbl->name ? tbl->name : "Unknown device"));
4628#endif
4629 return tbl->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 }
4631 return -1;
4632}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004633EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634
4635/**
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004636 * snd_hda_check_board_codec_sid_config - compare the current codec
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004637 subsystem ID with the
4638 config table
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004639
4640 This is important for Gateway notebooks with SB450 HDA Audio
4641 where the vendor ID of the PCI device is:
4642 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4643 and the vendor/subvendor are found only at the codec.
4644
4645 * @codec: the HDA codec
4646 * @num_configs: number of config enums
4647 * @models: array of model name strings
4648 * @tbl: configuration table, terminated by null entries
4649 *
4650 * Compares the modelname or PCI subsystem id of the current codec with the
4651 * given configuration table. If a matching entry is found, returns its
4652 * config value (supposed to be 0 or positive).
4653 *
4654 * If no entries are matching, the function returns a negative value.
4655 */
4656int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
Takashi Iwaiea734962011-01-17 11:29:34 +01004657 int num_configs, const char * const *models,
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004658 const struct snd_pci_quirk *tbl)
4659{
4660 const struct snd_pci_quirk *q;
4661
4662 /* Search for codec ID */
4663 for (q = tbl; q->subvendor; q++) {
Takashi Iwaie2301a42011-11-22 19:58:56 +01004664 unsigned int mask = 0xffff0000 | q->subdevice_mask;
4665 unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
4666 if ((codec->subsystem_id & mask) == id)
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004667 break;
4668 }
4669
4670 if (!q->subvendor)
4671 return -1;
4672
4673 tbl = q;
4674
4675 if (tbl->value >= 0 && tbl->value < num_configs) {
Takashi Iwaid94ff6b2009-09-02 00:20:21 +02004676#ifdef CONFIG_SND_DEBUG_VERBOSE
Mauro Carvalho Chehab2eda3442008-08-11 10:18:39 +02004677 char tmp[10];
4678 const char *model = NULL;
4679 if (models)
4680 model = models[tbl->value];
4681 if (!model) {
4682 sprintf(tmp, "#%d", tbl->value);
4683 model = tmp;
4684 }
4685 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4686 "for config %x:%x (%s)\n",
4687 model, tbl->subvendor, tbl->subdevice,
4688 (tbl->name ? tbl->name : "Unknown device"));
4689#endif
4690 return tbl->value;
4691 }
4692 return -1;
4693}
4694EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4695
4696/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 * snd_hda_add_new_ctls - create controls from the array
4698 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004699 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700 *
4701 * This helper function creates and add new controls in the given array.
4702 * The array must be terminated with an empty entry as terminator.
4703 *
4704 * Returns 0 if successful, or a negative error code.
4705 */
Takashi Iwai031024e2011-05-02 11:29:30 +02004706int snd_hda_add_new_ctls(struct hda_codec *codec,
4707 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01004709 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710
4711 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004712 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004713 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01004714 if (knew->iface == -1) /* skip this codec private value */
4715 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004716 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01004717 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02004718 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01004719 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01004720 if (addr > 0)
4721 kctl->id.device = addr;
4722 if (idx > 0)
4723 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01004724 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004725 if (!err)
4726 break;
4727 /* try first with another device index corresponding to
4728 * the codec addr; if it still fails (or it's the
4729 * primary codec), then try another control index
4730 */
4731 if (!addr && codec->addr)
4732 addr = codec->addr;
4733 else if (!idx && !knew->index) {
4734 idx = find_empty_mixer_ctl_idx(codec,
Takashi Iwaidcda5802012-10-12 17:24:51 +02004735 knew->name, 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01004736 if (idx <= 0)
4737 return err;
4738 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01004739 return err;
4740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741 }
4742 return 0;
4743}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004744EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745
Takashi Iwai83012a72012-08-24 18:38:08 +02004746#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02004747static void hda_power_work(struct work_struct *work)
4748{
4749 struct hda_codec *codec =
4750 container_of(work, struct hda_codec, power_work.work);
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004751 struct hda_bus *bus = codec->bus;
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004752 unsigned int state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004753
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004754 spin_lock(&codec->power_lock);
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004755 if (codec->power_transition > 0) { /* during power-up sequence? */
4756 spin_unlock(&codec->power_lock);
4757 return;
4758 }
Maxim Levitsky2e492462007-09-03 15:26:57 +02004759 if (!codec->power_on || codec->power_count) {
4760 codec->power_transition = 0;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004761 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004762 return;
Maxim Levitsky2e492462007-09-03 15:26:57 +02004763 }
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004764 spin_unlock(&codec->power_lock);
4765
Dylan Reidd17344b2012-09-28 15:57:01 -07004766 state = hda_call_codec_suspend(codec, true);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004767 codec->pm_down_notified = 0;
4768 if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK)) {
4769 codec->pm_down_notified = 1;
Takashi Iwai68467f52012-08-28 09:14:29 -07004770 hda_call_pm_notify(bus, false);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004771 }
Takashi Iwaicb53c622007-08-10 17:21:45 +02004772}
4773
4774static void hda_keep_power_on(struct hda_codec *codec)
4775{
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004776 spin_lock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004777 codec->power_count++;
4778 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004779 codec->power_jiffies = jiffies;
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004780 spin_unlock(&codec->power_lock);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004781}
4782
Takashi Iwaid5191e52009-11-16 14:58:17 +01004783/* update the power on/off account with the current jiffies */
Takashi Iwaia2f63092009-11-11 09:34:25 +01004784void snd_hda_update_power_acct(struct hda_codec *codec)
4785{
4786 unsigned long delta = jiffies - codec->power_jiffies;
4787 if (codec->power_on)
4788 codec->power_on_acct += delta;
4789 else
4790 codec->power_off_acct += delta;
4791 codec->power_jiffies += delta;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004792}
4793
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004794/* Transition to powered up, if wait_power_down then wait for a pending
4795 * transition to D3 to complete. A pending D3 transition is indicated
4796 * with power_transition == -1. */
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004797/* call this with codec->power_lock held! */
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004798static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004799{
Takashi Iwai33fa35e2008-11-06 16:50:40 +01004800 struct hda_bus *bus = codec->bus;
4801
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004802 /* Return if power_on or transitioning to power_on, unless currently
4803 * powering down. */
4804 if ((codec->power_on || codec->power_transition > 0) &&
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004805 !(wait_power_down && codec->power_transition < 0))
Takashi Iwaicb53c622007-08-10 17:21:45 +02004806 return;
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004807 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004808
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004809 cancel_delayed_work_sync(&codec->power_work);
4810
4811 spin_lock(&codec->power_lock);
Dylan Reidb43d2242012-06-21 21:51:22 -07004812 /* If the power down delayed work was cancelled above before starting,
4813 * then there is no need to go through power up here.
4814 */
4815 if (codec->power_on) {
Takashi Iwai535b6c52012-08-20 21:25:22 +02004816 if (codec->power_transition < 0)
4817 codec->power_transition = 0;
Dylan Reidb43d2242012-06-21 21:51:22 -07004818 return;
4819 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004820
Takashi Iwaid66fee52011-08-02 15:39:31 +02004821 trace_hda_power_up(codec);
Takashi Iwaia2f63092009-11-11 09:34:25 +01004822 snd_hda_update_power_acct(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004823 codec->power_on = 1;
Takashi Iwaia2f63092009-11-11 09:34:25 +01004824 codec->power_jiffies = jiffies;
Takashi Iwai7f308302012-05-08 16:52:23 +02004825 codec->power_transition = 1; /* avoid reentrance */
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004826 spin_unlock(&codec->power_lock);
4827
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004828 if (codec->pm_down_notified) {
4829 codec->pm_down_notified = 0;
Takashi Iwai68467f52012-08-28 09:14:29 -07004830 hda_call_pm_notify(bus, true);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07004831 }
4832
Takashi Iwaicb53c622007-08-10 17:21:45 +02004833 hda_call_codec_resume(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004834
4835 spin_lock(&codec->power_lock);
Takashi Iwaia221e282007-08-16 16:35:33 +02004836 codec->power_transition = 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004837}
Dylan Reidb4a91cf2012-06-15 19:36:23 -07004838
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004839#define power_save(codec) \
4840 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004841
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004842/* Transition to powered down */
4843static void __snd_hda_power_down(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004844{
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004845 if (!codec->power_on || codec->power_count || codec->power_transition)
Takashi Iwaicb53c622007-08-10 17:21:45 +02004846 return;
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004847
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004848 if (power_save(codec)) {
Takashi Iwaia2d96e72012-05-09 12:36:22 +02004849 codec->power_transition = -1; /* avoid reentrance */
Takashi Iwaic107b412009-01-13 17:46:37 +01004850 queue_delayed_work(codec->bus->workq, &codec->power_work,
Takashi Iwaifee2fba2008-11-27 12:43:28 +01004851 msecs_to_jiffies(power_save(codec) * 1000));
Takashi Iwaia221e282007-08-16 16:35:33 +02004852 }
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004853}
4854
4855/**
4856 * snd_hda_power_save - Power-up/down/sync the codec
4857 * @codec: HD-audio codec
4858 * @delta: the counter delta to change
4859 *
4860 * Change the power-up counter via @delta, and power up or down the hardware
4861 * appropriately. For the power-down, queue to the delayed action.
4862 * Passing zero to @delta means to synchronize the power state.
4863 */
4864void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
4865{
4866 spin_lock(&codec->power_lock);
4867 codec->power_count += delta;
4868 trace_hda_power_count(codec);
4869 if (delta > 0)
4870 __snd_hda_power_up(codec, d3wait);
4871 else
4872 __snd_hda_power_down(codec);
Takashi Iwai5536c6d2012-05-08 17:08:10 +02004873 spin_unlock(&codec->power_lock);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004874}
Takashi Iwaic376e2c2012-08-14 17:12:47 +02004875EXPORT_SYMBOL_HDA(snd_hda_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004876
Takashi Iwaid5191e52009-11-16 14:58:17 +01004877/**
4878 * snd_hda_check_amp_list_power - Check the amp list and update the power
4879 * @codec: HD-audio codec
4880 * @check: the object containing an AMP list and the status
4881 * @nid: NID to check / update
4882 *
4883 * Check whether the given NID is in the amp list. If it's in the list,
4884 * check the current AMP status, and update the the power-status according
4885 * to the mute status.
4886 *
4887 * This function is supposed to be set or called from the check_power_status
4888 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01004889 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02004890int snd_hda_check_amp_list_power(struct hda_codec *codec,
4891 struct hda_loopback_check *check,
4892 hda_nid_t nid)
4893{
Takashi Iwai031024e2011-05-02 11:29:30 +02004894 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02004895 int ch, v;
4896
4897 if (!check->amplist)
4898 return 0;
4899 for (p = check->amplist; p->nid; p++) {
4900 if (p->nid == nid)
4901 break;
4902 }
4903 if (!p->nid)
4904 return 0; /* nothing changed */
4905
4906 for (p = check->amplist; p->nid; p++) {
4907 for (ch = 0; ch < 2; ch++) {
4908 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4909 p->idx);
4910 if (!(v & HDA_AMP_MUTE) && v > 0) {
4911 if (!check->power_on) {
4912 check->power_on = 1;
4913 snd_hda_power_up(codec);
4914 }
4915 return 1;
4916 }
4917 }
4918 }
4919 if (check->power_on) {
4920 check->power_on = 0;
4921 snd_hda_power_down(codec);
4922 }
4923 return 0;
4924}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004925EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02004926#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01004928/*
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004929 * Channel mode helper
4930 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004931
4932/**
4933 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4934 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004935int snd_hda_ch_mode_info(struct hda_codec *codec,
4936 struct snd_ctl_elem_info *uinfo,
4937 const struct hda_channel_mode *chmode,
4938 int num_chmodes)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004939{
4940 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4941 uinfo->count = 1;
4942 uinfo->value.enumerated.items = num_chmodes;
4943 if (uinfo->value.enumerated.item >= num_chmodes)
4944 uinfo->value.enumerated.item = num_chmodes - 1;
4945 sprintf(uinfo->value.enumerated.name, "%dch",
4946 chmode[uinfo->value.enumerated.item].channels);
4947 return 0;
4948}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004949EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004950
Takashi Iwaid5191e52009-11-16 14:58:17 +01004951/**
4952 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4953 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004954int snd_hda_ch_mode_get(struct hda_codec *codec,
4955 struct snd_ctl_elem_value *ucontrol,
4956 const struct hda_channel_mode *chmode,
4957 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004958 int max_channels)
4959{
4960 int i;
4961
4962 for (i = 0; i < num_chmodes; i++) {
4963 if (max_channels == chmode[i].channels) {
4964 ucontrol->value.enumerated.item[0] = i;
4965 break;
4966 }
4967 }
4968 return 0;
4969}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004970EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004971
Takashi Iwaid5191e52009-11-16 14:58:17 +01004972/**
4973 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4974 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02004975int snd_hda_ch_mode_put(struct hda_codec *codec,
4976 struct snd_ctl_elem_value *ucontrol,
4977 const struct hda_channel_mode *chmode,
4978 int num_chmodes,
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004979 int *max_channelsp)
4980{
4981 unsigned int mode;
4982
4983 mode = ucontrol->value.enumerated.item[0];
Takashi Iwai68ea7b22007-11-15 15:54:38 +01004984 if (mode >= num_chmodes)
4985 return -EINVAL;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004986 if (*max_channelsp == chmode[mode].channels)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004987 return 0;
4988 /* change the current channel setting */
4989 *max_channelsp = chmode[mode].channels;
4990 if (chmode[mode].sequence)
Takashi Iwai82beb8f2007-08-10 17:09:26 +02004991 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004992 return 1;
4993}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01004994EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +01004995
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996/*
4997 * input MUX helper
4998 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01004999
5000/**
5001 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
5002 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005003int snd_hda_input_mux_info(const struct hda_input_mux *imux,
5004 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005{
5006 unsigned int index;
5007
5008 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5009 uinfo->count = 1;
5010 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02005011 if (!imux->num_items)
5012 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013 index = uinfo->value.enumerated.item;
5014 if (index >= imux->num_items)
5015 index = imux->num_items - 1;
5016 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
5017 return 0;
5018}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005019EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020
Takashi Iwaid5191e52009-11-16 14:58:17 +01005021/**
5022 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
5023 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005024int snd_hda_input_mux_put(struct hda_codec *codec,
5025 const struct hda_input_mux *imux,
5026 struct snd_ctl_elem_value *ucontrol,
5027 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028 unsigned int *cur_val)
5029{
5030 unsigned int idx;
5031
Takashi Iwai5513b0c2007-10-09 11:58:41 +02005032 if (!imux->num_items)
5033 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034 idx = ucontrol->value.enumerated.item[0];
5035 if (idx >= imux->num_items)
5036 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005037 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02005039 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
5040 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041 *cur_val = idx;
5042 return 1;
5043}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005044EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045
5046
5047/*
Takashi Iwaidda415d2012-11-30 18:34:38 +01005048 * process kcontrol info callback of a simple string enum array
5049 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
5050 */
5051int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
5052 struct snd_ctl_elem_info *uinfo,
5053 int num_items, const char * const *texts)
5054{
5055 static const char * const texts_default[] = {
5056 "Disabled", "Enabled"
5057 };
5058
5059 if (!texts || !num_items) {
5060 num_items = 2;
5061 texts = texts_default;
5062 }
5063
5064 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5065 uinfo->count = 1;
5066 uinfo->value.enumerated.items = num_items;
5067 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
5068 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
5069 strcpy(uinfo->value.enumerated.name,
5070 texts[uinfo->value.enumerated.item]);
5071 return 0;
5072}
5073EXPORT_SYMBOL_HDA(snd_hda_enum_helper_info);
5074
5075/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076 * Multi-channel / digital-out PCM helper functions
5077 */
5078
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005079/* setup SPDIF output stream */
5080static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
5081 unsigned int stream_tag, unsigned int format)
5082{
Laurence Darby3bef1c32012-11-03 17:00:06 +00005083 struct hda_spdif_out *spdif;
5084 unsigned int curr_fmt;
5085 bool reset;
Stephen Warren7c935972011-06-01 11:14:17 -06005086
Laurence Darby3bef1c32012-11-03 17:00:06 +00005087 spdif = snd_hda_spdif_out_of_nid(codec, nid);
5088 curr_fmt = snd_hda_codec_read(codec, nid, 0,
5089 AC_VERB_GET_STREAM_FORMAT, 0);
5090 reset = codec->spdif_status_reset &&
5091 (spdif->ctls & AC_DIG1_ENABLE) &&
5092 curr_fmt != format;
5093
5094 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
5095 updated */
5096 if (reset)
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005097 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06005098 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02005099 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005100 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02005101 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02005102 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02005103 for (d = codec->slave_dig_outs; *d; d++)
5104 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
5105 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005106 }
Takashi Iwai2f728532008-09-25 16:32:41 +02005107 /* turn on again (if needed) */
Laurence Darby3bef1c32012-11-03 17:00:06 +00005108 if (reset)
Takashi Iwai2f728532008-09-25 16:32:41 +02005109 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06005110 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02005111}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04005112
Takashi Iwai2f728532008-09-25 16:32:41 +02005113static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
5114{
5115 snd_hda_codec_cleanup_stream(codec, nid);
5116 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02005117 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02005118 for (d = codec->slave_dig_outs; *d; d++)
5119 snd_hda_codec_cleanup_stream(codec, *d);
5120 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005121}
5122
Takashi Iwaid5191e52009-11-16 14:58:17 +01005123/**
5124 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
5125 * @bus: HD-audio bus
5126 */
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005127void snd_hda_bus_reboot_notify(struct hda_bus *bus)
5128{
5129 struct hda_codec *codec;
5130
5131 if (!bus)
5132 return;
5133 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005134 if (hda_codec_is_power_on(codec) &&
5135 codec->patch_ops.reboot_notify)
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005136 codec->patch_ops.reboot_notify(codec);
5137 }
5138}
Takashi Iwai8f217a22009-11-10 18:26:12 +01005139EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
Takashi Iwaifb8d1a32009-11-10 16:02:29 +01005140
Takashi Iwaid5191e52009-11-16 14:58:17 +01005141/**
5142 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005144int snd_hda_multi_out_dig_open(struct hda_codec *codec,
5145 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146{
Ingo Molnar62932df2006-01-16 16:34:20 +01005147 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02005148 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
5149 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02005150 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01005152 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153 return 0;
5154}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005155EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156
Takashi Iwaid5191e52009-11-16 14:58:17 +01005157/**
5158 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
5159 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005160int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
5161 struct hda_multi_out *mout,
5162 unsigned int stream_tag,
5163 unsigned int format,
5164 struct snd_pcm_substream *substream)
5165{
5166 mutex_lock(&codec->spdif_mutex);
5167 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
5168 mutex_unlock(&codec->spdif_mutex);
5169 return 0;
5170}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005171EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005172
Takashi Iwaid5191e52009-11-16 14:58:17 +01005173/**
5174 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
5175 */
Takashi Iwai9411e212009-02-13 11:32:28 +01005176int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
5177 struct hda_multi_out *mout)
5178{
5179 mutex_lock(&codec->spdif_mutex);
5180 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5181 mutex_unlock(&codec->spdif_mutex);
5182 return 0;
5183}
5184EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
5185
Takashi Iwaid5191e52009-11-16 14:58:17 +01005186/**
5187 * snd_hda_multi_out_dig_close - release the digital out stream
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005189int snd_hda_multi_out_dig_close(struct hda_codec *codec,
5190 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191{
Ingo Molnar62932df2006-01-16 16:34:20 +01005192 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01005194 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195 return 0;
5196}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005197EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198
Takashi Iwaid5191e52009-11-16 14:58:17 +01005199/**
5200 * snd_hda_multi_out_analog_open - open analog outputs
5201 *
5202 * Open analog outputs and set up the hw-constraints.
5203 * If the digital outputs can be opened as slave, open the digital
5204 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005206int snd_hda_multi_out_analog_open(struct hda_codec *codec,
5207 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01005208 struct snd_pcm_substream *substream,
5209 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210{
Takashi Iwai9a081602008-02-12 18:37:26 +01005211 struct snd_pcm_runtime *runtime = substream->runtime;
5212 runtime->hw.channels_max = mout->max_channels;
5213 if (mout->dig_out_nid) {
5214 if (!mout->analog_rates) {
5215 mout->analog_rates = hinfo->rates;
5216 mout->analog_formats = hinfo->formats;
5217 mout->analog_maxbps = hinfo->maxbps;
5218 } else {
5219 runtime->hw.rates = mout->analog_rates;
5220 runtime->hw.formats = mout->analog_formats;
5221 hinfo->maxbps = mout->analog_maxbps;
5222 }
5223 if (!mout->spdif_rates) {
5224 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
5225 &mout->spdif_rates,
5226 &mout->spdif_formats,
5227 &mout->spdif_maxbps);
5228 }
5229 mutex_lock(&codec->spdif_mutex);
5230 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02005231 if ((runtime->hw.rates & mout->spdif_rates) &&
5232 (runtime->hw.formats & mout->spdif_formats)) {
5233 runtime->hw.rates &= mout->spdif_rates;
5234 runtime->hw.formats &= mout->spdif_formats;
5235 if (mout->spdif_maxbps < hinfo->maxbps)
5236 hinfo->maxbps = mout->spdif_maxbps;
5237 } else {
5238 mout->share_spdif = 0;
5239 /* FIXME: need notify? */
5240 }
Takashi Iwai9a081602008-02-12 18:37:26 +01005241 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02005242 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01005243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244 return snd_pcm_hw_constraint_step(substream->runtime, 0,
5245 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
5246}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005247EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248
Takashi Iwaid5191e52009-11-16 14:58:17 +01005249/**
5250 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
5251 *
5252 * Set up the i/o for analog out.
5253 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005255int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5256 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257 unsigned int stream_tag,
5258 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01005259 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260{
Takashi Iwaidda14412011-05-02 11:29:30 +02005261 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005263 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005264 int i;
5265
Ingo Molnar62932df2006-01-16 16:34:20 +01005266 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02005267 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01005268 if (mout->dig_out_nid && mout->share_spdif &&
5269 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02005271 snd_hda_is_supported_format(codec, mout->dig_out_nid,
5272 format) &&
Stephen Warren7c935972011-06-01 11:14:17 -06005273 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02005275 setup_dig_out_stream(codec, mout->dig_out_nid,
5276 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277 } else {
5278 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02005279 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280 }
5281 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005282 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283
5284 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005285 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5286 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005287 if (!mout->no_share_stream &&
5288 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005290 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5291 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005292 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005293 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5294 if (!mout->no_share_stream && mout->hp_out_nid[i])
5295 snd_hda_codec_setup_stream(codec,
5296 mout->hp_out_nid[i],
5297 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005298 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
Takashi Iwaid29240c2007-10-26 12:35:56 +02005299 if (!mout->no_share_stream && mout->extra_out_nid[i])
Takashi Iwai82bc9552006-03-21 11:24:42 +01005300 snd_hda_codec_setup_stream(codec,
5301 mout->extra_out_nid[i],
5302 stream_tag, 0, format);
5303
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304 /* surrounds */
5305 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02005306 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005307 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5308 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02005309 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005310 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5311 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312 }
5313 return 0;
5314}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005315EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316
Takashi Iwaid5191e52009-11-16 14:58:17 +01005317/**
5318 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02005320int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5321 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322{
Takashi Iwaidda14412011-05-02 11:29:30 +02005323 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324 int i;
5325
5326 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01005327 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01005329 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02005330 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5331 if (mout->hp_out_nid[i])
5332 snd_hda_codec_cleanup_stream(codec,
5333 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01005334 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5335 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01005336 snd_hda_codec_cleanup_stream(codec,
5337 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01005338 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02005340 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341 mout->dig_out_used = 0;
5342 }
Ingo Molnar62932df2006-01-16 16:34:20 +01005343 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344 return 0;
5345}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005346EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347
Takashi Iwai47408602012-04-20 13:06:53 +02005348/**
5349 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
5350 *
5351 * Guess the suitable VREF pin bits to be set as the pin-control value.
5352 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5353 */
5354unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5355{
5356 unsigned int pincap;
5357 unsigned int oldval;
5358 oldval = snd_hda_codec_read(codec, pin, 0,
5359 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5360 pincap = snd_hda_query_pin_caps(codec, pin);
5361 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5362 /* Exception: if the default pin setup is vref50, we give it priority */
5363 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5364 return AC_PINCTL_VREF_80;
5365 else if (pincap & AC_PINCAP_VREF_50)
5366 return AC_PINCTL_VREF_50;
5367 else if (pincap & AC_PINCAP_VREF_100)
5368 return AC_PINCTL_VREF_100;
5369 else if (pincap & AC_PINCAP_VREF_GRD)
5370 return AC_PINCTL_VREF_GRD;
5371 return AC_PINCTL_VREF_HIZ;
5372}
5373EXPORT_SYMBOL_HDA(snd_hda_get_default_vref);
5374
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005375/* correct the pin ctl value for matching with the pin cap */
5376unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
5377 hda_nid_t pin, unsigned int val)
5378{
5379 static unsigned int cap_lists[][2] = {
5380 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
5381 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
5382 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
5383 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
5384 };
5385 unsigned int cap;
5386
5387 if (!val)
5388 return 0;
5389 cap = snd_hda_query_pin_caps(codec, pin);
5390 if (!cap)
5391 return val; /* don't know what to do... */
5392
5393 if (val & AC_PINCTL_OUT_EN) {
5394 if (!(cap & AC_PINCAP_OUT))
5395 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5396 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
5397 val &= ~AC_PINCTL_HP_EN;
5398 }
5399
5400 if (val & AC_PINCTL_IN_EN) {
5401 if (!(cap & AC_PINCAP_IN))
5402 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5403 else {
5404 unsigned int vcap, vref;
5405 int i;
5406 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5407 vref = val & AC_PINCTL_VREFEN;
5408 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
5409 if (vref == cap_lists[i][0] &&
5410 !(vcap & cap_lists[i][1])) {
5411 if (i == ARRAY_SIZE(cap_lists) - 1)
5412 vref = AC_PINCTL_VREF_HIZ;
5413 else
5414 vref = cap_lists[i + 1][0];
5415 }
5416 }
5417 val &= ~AC_PINCTL_VREFEN;
5418 val |= vref;
5419 }
5420 }
5421
5422 return val;
5423}
5424EXPORT_SYMBOL_HDA(snd_hda_correct_pin_ctl);
5425
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005426int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5427 unsigned int val, bool cached)
5428{
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01005429 val = snd_hda_correct_pin_ctl(codec, pin, val);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01005430 snd_hda_codec_set_pin_target(codec, pin, val);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02005431 if (cached)
5432 return snd_hda_codec_update_cache(codec, pin, 0,
5433 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5434 else
5435 return snd_hda_codec_write(codec, pin, 0,
5436 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5437}
5438EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl);
5439
Takashi Iwai990061c2010-09-09 22:08:44 +02005440/**
5441 * snd_hda_add_imux_item - Add an item to input_mux
5442 *
5443 * When the same label is used already in the existing items, the number
5444 * suffix is appended to the label. This label index number is stored
5445 * to type_idx when non-NULL pointer is given.
5446 */
Takashi Iwai10a20af2010-09-09 16:28:02 +02005447int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5448 int index, int *type_idx)
5449{
5450 int i, label_idx = 0;
5451 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5452 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5453 return -EINVAL;
5454 }
5455 for (i = 0; i < imux->num_items; i++) {
5456 if (!strncmp(label, imux->items[i].label, strlen(label)))
5457 label_idx++;
5458 }
5459 if (type_idx)
5460 *type_idx = label_idx;
5461 if (label_idx > 0)
5462 snprintf(imux->items[imux->num_items].label,
5463 sizeof(imux->items[imux->num_items].label),
5464 "%s %d", label, label_idx);
5465 else
5466 strlcpy(imux->items[imux->num_items].label, label,
5467 sizeof(imux->items[imux->num_items].label));
5468 imux->items[imux->num_items].index = index;
5469 imux->num_items++;
5470 return 0;
5471}
5472EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02005473
Takashi Iwai4a471b72005-12-07 13:56:29 +01005474
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475#ifdef CONFIG_PM
5476/*
5477 * power management
5478 */
5479
5480/**
5481 * snd_hda_suspend - suspend the codecs
5482 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483 *
5484 * Returns 0 if successful.
5485 */
Takashi Iwai8dd78332009-06-02 01:16:07 +02005486int snd_hda_suspend(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005488 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489
Takashi Iwai0ba21762007-04-16 11:29:14 +02005490 list_for_each_entry(codec, &bus->codec_list, list) {
David Henningsson26a6cb62012-10-09 15:04:21 +02005491 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwaie581f3d2011-07-26 10:19:20 +02005492 if (hda_codec_is_power_on(codec))
Dylan Reidd17344b2012-09-28 15:57:01 -07005493 hda_call_codec_suspend(codec, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494 }
5495 return 0;
5496}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005497EXPORT_SYMBOL_HDA(snd_hda_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498
5499/**
5500 * snd_hda_resume - resume the codecs
5501 * @bus: the HDA bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 *
5503 * Returns 0 if successful.
5504 */
5505int snd_hda_resume(struct hda_bus *bus)
5506{
Takashi Iwai0ba21762007-04-16 11:29:14 +02005507 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508
Takashi Iwai0ba21762007-04-16 11:29:14 +02005509 list_for_each_entry(codec, &bus->codec_list, list) {
Takashi Iwai7f308302012-05-08 16:52:23 +02005510 hda_call_codec_resume(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512 return 0;
5513}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005514EXPORT_SYMBOL_HDA(snd_hda_resume);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005515#endif /* CONFIG_PM */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005516
5517/*
5518 * generic arrays
5519 */
5520
Takashi Iwaid5191e52009-11-16 14:58:17 +01005521/**
5522 * snd_array_new - get a new element from the given array
5523 * @array: the array object
Norberto Lopes28aedaf2010-02-28 20:16:53 +01005524 *
Takashi Iwaid5191e52009-11-16 14:58:17 +01005525 * Get a new element from the given array. If it exceeds the
5526 * pre-allocated array size, re-allocate the array.
5527 *
5528 * Returns NULL if allocation failed.
Takashi Iwaib2e18592008-07-30 15:01:44 +02005529 */
5530void *snd_array_new(struct snd_array *array)
5531{
Takashi Iwai12f17712012-10-10 08:59:14 +02005532 if (snd_BUG_ON(!array->elem_size))
5533 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005534 if (array->used >= array->alloced) {
5535 int num = array->alloced + array->alloc_align;
Takashi Iwai3101ba02011-07-12 08:05:16 +02005536 int size = (num + 1) * array->elem_size;
Takashi Iwaib910d9a2008-11-07 00:26:52 +01005537 void *nlist;
5538 if (snd_BUG_ON(num >= 4096))
5539 return NULL;
Takashi Iwai5265fd92013-03-13 12:15:28 +01005540 nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005541 if (!nlist)
5542 return NULL;
Takashi Iwaib2e18592008-07-30 15:01:44 +02005543 array->list = nlist;
5544 array->alloced = num;
5545 }
Takashi Iwaif43aa022008-11-10 16:24:26 +01005546 return snd_array_elem(array, array->used++);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005547}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005548EXPORT_SYMBOL_HDA(snd_array_new);
Takashi Iwaib2e18592008-07-30 15:01:44 +02005549
Takashi Iwaid5191e52009-11-16 14:58:17 +01005550/**
5551 * snd_array_free - free the given array elements
5552 * @array: the array object
5553 */
Takashi Iwaib2e18592008-07-30 15:01:44 +02005554void snd_array_free(struct snd_array *array)
5555{
5556 kfree(array->list);
5557 array->used = 0;
5558 array->alloced = 0;
5559 array->list = NULL;
5560}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005561EXPORT_SYMBOL_HDA(snd_array_free);
Takashi Iwaib2022262008-11-21 21:24:03 +01005562
Takashi Iwaid5191e52009-11-16 14:58:17 +01005563/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01005564 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5565 * @pcm: PCM caps bits
5566 * @buf: the string buffer to write
5567 * @buflen: the max buffer length
5568 *
5569 * used by hda_proc.c and hda_eld.c
5570 */
Takashi Iwaib2022262008-11-21 21:24:03 +01005571void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5572{
5573 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5574 int i, j;
5575
5576 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5577 if (pcm & (AC_SUPPCM_BITS_8 << i))
5578 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5579
5580 buf[j] = '\0'; /* necessary when j == 0 */
5581}
Takashi Iwaiff7a3262008-11-28 15:17:06 +01005582EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01005583
5584MODULE_DESCRIPTION("HDA codec core");
5585MODULE_LICENSE("GPL");