blob: 83741887faa189c1700abc34bebbd3c55234384d [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>
Ingo Molnar62932df2006-01-16 16:34:20 +010026#include <linux/mutex.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040027#include <linux/module.h>
Takashi Iwaif4d6a552013-12-05 11:55:05 +010028#include <linux/async.h>
Takashi Iwaicc72da72015-02-19 16:00:22 +010029#include <linux/pm.h>
30#include <linux/pm_runtime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <sound/core.h>
32#include "hda_codec.h"
33#include <sound/asoundef.h>
Jaroslav Kysela302e9c52006-07-05 17:39:49 +020034#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <sound/initval.h>
Takashi Iwaicd372fb2011-03-03 14:40:14 +010036#include <sound/jack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "hda_local.h"
Jaroslav Kysela123c07a2009-10-21 14:48:23 +020038#include "hda_beep.h"
Takashi Iwai1835a0f2011-10-27 22:12:46 +020039#include "hda_jack.h"
Takashi Iwai28073142007-07-27 18:58:06 +020040#include <sound/hda_hwdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Takashi Iwai83012a72012-08-24 18:38:08 +020042#ifdef CONFIG_PM
Takashi Iwai7639a062015-03-03 10:07:24 +010043#define codec_in_pm(codec) atomic_read(&(codec)->core.in_pm)
Takashi Iwaicc72da72015-02-19 16:00:22 +010044#define hda_codec_is_power_on(codec) \
45 (!pm_runtime_suspended(hda_codec_dev(codec)))
Takashi Iwaicb53c622007-08-10 17:21:45 +020046#else
Takashi Iwaid846b172012-11-24 11:58:24 +010047#define codec_in_pm(codec) 0
Takashi Iwaie581f3d2011-07-26 10:19:20 +020048#define hda_codec_is_power_on(codec) 1
Takashi Iwaicb53c622007-08-10 17:21:45 +020049#endif
50
Takashi Iwai7639a062015-03-03 10:07:24 +010051#define codec_has_epss(codec) \
52 ((codec)->core.power_caps & AC_PWRST_EPSS)
53#define codec_has_clkstop(codec) \
54 ((codec)->core.power_caps & AC_PWRST_CLKSTOP)
55
Takashi Iwai33fa35e2008-11-06 16:50:40 +010056/*
Takashi Iwai05852442015-03-03 15:40:08 +010057 * Send and receive a verb - passed to exec_verb override for hdac_device
Takashi Iwaiaa2936f2009-05-26 16:07:57 +020058 */
Takashi Iwai05852442015-03-03 15:40:08 +010059static int codec_exec_verb(struct hdac_device *dev, unsigned int cmd,
60 unsigned int flags, unsigned int *res)
Takashi Iwaiaa2936f2009-05-26 16:07:57 +020061{
Takashi Iwai05852442015-03-03 15:40:08 +010062 struct hda_codec *codec = container_of(dev, struct hda_codec, core);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +020063 struct hda_bus *bus = codec->bus;
Takashi Iwai8dd78332009-06-02 01:16:07 +020064 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +020065
Wu Fengguang6430aee2009-07-17 16:49:19 +080066 if (cmd == ~0)
67 return -1;
68
Takashi Iwai8dd78332009-06-02 01:16:07 +020069 again:
Takashi Iwai664c7152015-04-08 11:43:14 +020070 snd_hda_power_up_pm(codec);
Takashi Iwaid068ebc2015-03-02 23:22:59 +010071 mutex_lock(&bus->core.cmd_mutex);
Takashi Iwai63e51fd2013-06-06 14:20:19 +020072 if (flags & HDA_RW_NO_RESPONSE_FALLBACK)
73 bus->no_response_fallback = 1;
Takashi Iwaid068ebc2015-03-02 23:22:59 +010074 err = snd_hdac_bus_exec_verb_unlocked(&bus->core, codec->core.addr,
75 cmd, res);
Takashi Iwai63e51fd2013-06-06 14:20:19 +020076 bus->no_response_fallback = 0;
Takashi Iwaid068ebc2015-03-02 23:22:59 +010077 mutex_unlock(&bus->core.cmd_mutex);
Takashi Iwai664c7152015-04-08 11:43:14 +020078 snd_hda_power_down_pm(codec);
Takashi Iwaicad372f2015-03-25 17:57:00 +010079 if (!codec_in_pm(codec) && res && err == -EAGAIN) {
Takashi Iwai8dd78332009-06-02 01:16:07 +020080 if (bus->response_reset) {
Takashi Iwai4e76a882014-02-25 12:21:03 +010081 codec_dbg(codec,
82 "resetting BUS due to fatal communication error\n");
Takashi Iwai0a505752015-04-16 23:25:02 +020083 snd_hda_bus_reset(bus);
Takashi Iwai8dd78332009-06-02 01:16:07 +020084 }
85 goto again;
86 }
87 /* clear reset-flag when the communication gets recovered */
Takashi Iwaid846b172012-11-24 11:58:24 +010088 if (!err || codec_in_pm(codec))
Takashi Iwai8dd78332009-06-02 01:16:07 +020089 bus->response_reset = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +020090 return err;
91}
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 * snd_hda_sequence_write - sequence writes
95 * @codec: the HDA codec
96 * @seq: VERB array to send
97 *
98 * Send the commands sequentially from the given array.
99 * The array must be terminated with NID=0.
100 */
101void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
102{
103 for (; seq->nid; seq++)
104 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
105}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100106EXPORT_SYMBOL_GPL(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100108/* connection list element */
109struct hda_conn_list {
110 struct list_head list;
111 int len;
112 hda_nid_t nid;
113 hda_nid_t conns[0];
114};
115
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200116/* look up the cached results */
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100117static struct hda_conn_list *
118lookup_conn_list(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200119{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100120 struct hda_conn_list *p;
121 list_for_each_entry(p, &codec->conn_list, list) {
122 if (p->nid == nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200123 return p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200124 }
125 return NULL;
126}
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200127
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100128static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
129 const hda_nid_t *list)
130{
131 struct hda_conn_list *p;
132
133 p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL);
134 if (!p)
135 return -ENOMEM;
136 p->len = len;
137 p->nid = nid;
138 memcpy(p->conns, list, len * sizeof(hda_nid_t));
139 list_add(&p->list, &codec->conn_list);
140 return 0;
141}
142
143static void remove_conn_list(struct hda_codec *codec)
144{
145 while (!list_empty(&codec->conn_list)) {
146 struct hda_conn_list *p;
147 p = list_first_entry(&codec->conn_list, typeof(*p), list);
148 list_del(&p->list);
149 kfree(p);
150 }
151}
152
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200153/* read the connection and add to the cache */
154static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200155{
Takashi Iwai4eea3092013-02-07 18:18:19 +0100156 hda_nid_t list[32];
157 hda_nid_t *result = list;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200158 int len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200159
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200160 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
Takashi Iwai4eea3092013-02-07 18:18:19 +0100161 if (len == -ENOSPC) {
162 len = snd_hda_get_num_raw_conns(codec, nid);
163 result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL);
164 if (!result)
165 return -ENOMEM;
166 len = snd_hda_get_raw_connections(codec, nid, result, len);
167 }
168 if (len >= 0)
169 len = snd_hda_override_conn_list(codec, nid, len, result);
170 if (result != list)
171 kfree(result);
172 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200173}
Takashi Iwaidce20792011-06-24 14:10:28 +0200174
175/**
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100176 * snd_hda_get_conn_list - get connection list
177 * @codec: the HDA codec
178 * @nid: NID to parse
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100179 * @listp: the pointer to store NID list
180 *
181 * Parses the connection list of the given widget and stores the pointer
182 * to the list of NIDs.
183 *
184 * Returns the number of connections, or a negative error code.
185 *
186 * Note that the returned pointer isn't protected against the list
187 * modification. If snd_hda_override_conn_list() might be called
188 * concurrently, protect with a mutex appropriately.
189 */
190int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
191 const hda_nid_t **listp)
192{
193 bool added = false;
194
195 for (;;) {
196 int err;
197 const struct hda_conn_list *p;
198
199 /* if the connection-list is already cached, read it */
200 p = lookup_conn_list(codec, nid);
201 if (p) {
202 if (listp)
203 *listp = p->conns;
204 return p->len;
205 }
206 if (snd_BUG_ON(added))
207 return -EINVAL;
208
209 err = read_and_add_raw_conns(codec, nid);
210 if (err < 0)
211 return err;
212 added = true;
213 }
214}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100215EXPORT_SYMBOL_GPL(snd_hda_get_conn_list);
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100216
217/**
Takashi Iwaidce20792011-06-24 14:10:28 +0200218 * snd_hda_get_connections - copy connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 * @codec: the HDA codec
220 * @nid: NID to parse
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200221 * @conn_list: connection list array; when NULL, checks only the size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 * @max_conns: max. number of connections to store
223 *
224 * Parses the connection list of the given widget and stores the list
225 * of NIDs.
226 *
227 * Returns the number of connections, or a negative error code.
228 */
229int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200230 hda_nid_t *conn_list, int max_conns)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200231{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100232 const hda_nid_t *list;
233 int len = snd_hda_get_conn_list(codec, nid, &list);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200234
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100235 if (len > 0 && conn_list) {
236 if (len > max_conns) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100237 codec_err(codec, "Too many connections %d for NID 0x%x\n",
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200238 len, nid);
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200239 return -EINVAL;
240 }
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100241 memcpy(conn_list, list, len * sizeof(hda_nid_t));
Takashi Iwaidce20792011-06-24 14:10:28 +0200242 }
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200243
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100244 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200245}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100246EXPORT_SYMBOL_GPL(snd_hda_get_connections);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200249 * snd_hda_override_conn_list - add/modify the connection-list to cache
250 * @codec: the HDA codec
251 * @nid: NID to parse
252 * @len: number of connection list entries
253 * @list: the list of connection entries
254 *
255 * Add or modify the given connection-list to the cache. If the corresponding
256 * cache already exists, invalidate it and append a new one.
257 *
258 * Returns zero or a negative error code.
259 */
260int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
261 const hda_nid_t *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100263 struct hda_conn_list *p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200264
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100265 p = lookup_conn_list(codec, nid);
266 if (p) {
267 list_del(&p->list);
268 kfree(p);
269 }
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200270
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100271 return add_conn_list(codec, nid, len, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100273EXPORT_SYMBOL_GPL(snd_hda_override_conn_list);
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200274
275/**
Takashi Iwai8d087c72011-06-28 12:45:47 +0200276 * snd_hda_get_conn_index - get the connection index of the given NID
277 * @codec: the HDA codec
278 * @mux: NID containing the list
279 * @nid: NID to select
280 * @recursive: 1 when searching NID recursively, otherwise 0
281 *
282 * Parses the connection list of the widget @mux and checks whether the
283 * widget @nid is present. If it is, return the connection index.
284 * Otherwise it returns -1.
285 */
286int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
287 hda_nid_t nid, int recursive)
288{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100289 const hda_nid_t *conn;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200290 int i, nums;
291
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100292 nums = snd_hda_get_conn_list(codec, mux, &conn);
Takashi Iwai8d087c72011-06-28 12:45:47 +0200293 for (i = 0; i < nums; i++)
294 if (conn[i] == nid)
295 return i;
296 if (!recursive)
297 return -1;
Takashi Iwaid94ddd82012-12-20 14:42:42 +0100298 if (recursive > 10) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100299 codec_dbg(codec, "too deep connection for 0x%x\n", nid);
Takashi Iwai8d087c72011-06-28 12:45:47 +0200300 return -1;
301 }
302 recursive++;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200303 for (i = 0; i < nums; i++) {
304 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
305 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
306 continue;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200307 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
308 return i;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200309 }
Takashi Iwai8d087c72011-06-28 12:45:47 +0200310 return -1;
311}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100312EXPORT_SYMBOL_GPL(snd_hda_get_conn_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Mengdong Linf1aa0682013-08-26 21:35:21 -0400314
315/* return DEVLIST_LEN parameter of the given widget */
316static unsigned int get_num_devices(struct hda_codec *codec, hda_nid_t nid)
317{
318 unsigned int wcaps = get_wcaps(codec, nid);
319 unsigned int parm;
320
321 if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) ||
322 get_wcaps_type(wcaps) != AC_WID_PIN)
323 return 0;
324
Dave Airlie132bd962015-06-09 13:39:31 +1000325 parm = snd_hdac_read_parm_uncached(&codec->core, nid, AC_PAR_DEVLIST_LEN);
Takashi Iwai86548442015-06-09 07:22:26 +0200326 if (parm == -1)
Mengdong Linf1aa0682013-08-26 21:35:21 -0400327 parm = 0;
328 return parm & AC_DEV_LIST_LEN_MASK;
329}
330
331/**
332 * snd_hda_get_devices - copy device list without cache
333 * @codec: the HDA codec
334 * @nid: NID of the pin to parse
335 * @dev_list: device list array
336 * @max_devices: max. number of devices to store
337 *
338 * Copy the device list. This info is dynamic and so not cached.
339 * Currently called only from hda_proc.c, so not exported.
340 */
341int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid,
342 u8 *dev_list, int max_devices)
343{
344 unsigned int parm;
345 int i, dev_len, devices;
346
347 parm = get_num_devices(codec, nid);
348 if (!parm) /* not multi-stream capable */
349 return 0;
350
351 dev_len = parm + 1;
352 dev_len = dev_len < max_devices ? dev_len : max_devices;
353
354 devices = 0;
355 while (devices < dev_len) {
Takashi Iwaicad372f2015-03-25 17:57:00 +0100356 if (snd_hdac_read(&codec->core, nid,
357 AC_VERB_GET_DEVICE_LIST, devices, &parm))
358 break; /* error */
Mengdong Linf1aa0682013-08-26 21:35:21 -0400359
360 for (i = 0; i < 8; i++) {
361 dev_list[devices] = (u8)parm;
362 parm >>= 4;
363 devices++;
364 if (devices >= dev_len)
365 break;
366 }
367 }
368 return devices;
369}
370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100372 * read widget caps for each widget and store in cache
373 */
374static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
375{
376 int i;
377 hda_nid_t nid;
378
Takashi Iwai7639a062015-03-03 10:07:24 +0100379 codec->wcaps = kmalloc(codec->core.num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200380 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100381 return -ENOMEM;
Takashi Iwai7639a062015-03-03 10:07:24 +0100382 nid = codec->core.start_nid;
383 for (i = 0; i < codec->core.num_nodes; i++, nid++)
Takashi Iwai9ba17b42015-03-03 23:29:47 +0100384 codec->wcaps[i] = snd_hdac_read_parm_uncached(&codec->core,
385 nid, AC_PAR_AUDIO_WIDGET_CAP);
Takashi Iwai54d17402005-11-21 16:33:22 +0100386 return 0;
387}
388
Takashi Iwai3be14142009-02-20 14:11:16 +0100389/* read all pin default configurations and save codec->init_pins */
390static int read_pin_defaults(struct hda_codec *codec)
391{
Takashi Iwai7639a062015-03-03 10:07:24 +0100392 hda_nid_t nid;
Takashi Iwai3be14142009-02-20 14:11:16 +0100393
Takashi Iwai7639a062015-03-03 10:07:24 +0100394 for_each_hda_codec_node(nid, codec) {
Takashi Iwai3be14142009-02-20 14:11:16 +0100395 struct hda_pincfg *pin;
396 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +0200397 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +0100398 if (wid_type != AC_WID_PIN)
399 continue;
400 pin = snd_array_new(&codec->init_pins);
401 if (!pin)
402 return -ENOMEM;
403 pin->nid = nid;
404 pin->cfg = snd_hda_codec_read(codec, nid, 0,
405 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200406 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
407 AC_VERB_GET_PIN_WIDGET_CONTROL,
408 0);
Takashi Iwai3be14142009-02-20 14:11:16 +0100409 }
410 return 0;
411}
412
413/* look up the given pin config list and return the item matching with NID */
414static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
415 struct snd_array *array,
416 hda_nid_t nid)
417{
418 int i;
419 for (i = 0; i < array->used; i++) {
420 struct hda_pincfg *pin = snd_array_elem(array, i);
421 if (pin->nid == nid)
422 return pin;
423 }
424 return NULL;
425}
426
Takashi Iwai3be14142009-02-20 14:11:16 +0100427/* set the current pin config value for the given NID.
428 * the value is cached, and read via snd_hda_codec_get_pincfg()
429 */
430int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
431 hda_nid_t nid, unsigned int cfg)
432{
433 struct hda_pincfg *pin;
434
Takashi Iwaid5657ec2013-04-18 09:59:28 +0200435 /* the check below may be invalid when pins are added by a fixup
436 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
437 * for now
438 */
439 /*
Takashi Iwaib82855a2009-12-27 11:24:56 +0100440 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
441 return -EINVAL;
Takashi Iwaid5657ec2013-04-18 09:59:28 +0200442 */
Takashi Iwaib82855a2009-12-27 11:24:56 +0100443
Takashi Iwai3be14142009-02-20 14:11:16 +0100444 pin = look_up_pincfg(codec, list, nid);
445 if (!pin) {
446 pin = snd_array_new(list);
447 if (!pin)
448 return -ENOMEM;
449 pin->nid = nid;
450 }
451 pin->cfg = cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100452 return 0;
453}
454
Takashi Iwaid5191e52009-11-16 14:58:17 +0100455/**
456 * snd_hda_codec_set_pincfg - Override a pin default configuration
457 * @codec: the HDA codec
458 * @nid: NID to set the pin config
459 * @cfg: the pin default config value
460 *
461 * Override a pin default configuration value in the cache.
462 * This value can be read by snd_hda_codec_get_pincfg() in a higher
463 * priority than the real hardware value.
464 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100465int snd_hda_codec_set_pincfg(struct hda_codec *codec,
466 hda_nid_t nid, unsigned int cfg)
467{
Takashi Iwai346ff702009-02-23 09:42:57 +0100468 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100469}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100470EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100471
Takashi Iwaid5191e52009-11-16 14:58:17 +0100472/**
473 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
474 * @codec: the HDA codec
475 * @nid: NID to get the pin config
476 *
477 * Get the current pin config value of the given pin NID.
478 * If the pincfg value is cached or overridden via sysfs or driver,
479 * returns the cached value.
480 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100481unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
482{
483 struct hda_pincfg *pin;
484
Takashi Iwai648a8d22014-02-25 10:38:13 +0100485#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai09b70e82013-01-10 18:21:56 +0100486 {
487 unsigned int cfg = 0;
488 mutex_lock(&codec->user_mutex);
489 pin = look_up_pincfg(codec, &codec->user_pins, nid);
490 if (pin)
491 cfg = pin->cfg;
492 mutex_unlock(&codec->user_mutex);
493 if (cfg)
494 return cfg;
495 }
Takashi Iwai3be14142009-02-20 14:11:16 +0100496#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100497 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
498 if (pin)
499 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100500 pin = look_up_pincfg(codec, &codec->init_pins, nid);
501 if (pin)
502 return pin->cfg;
503 return 0;
504}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100505EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100506
Takashi Iwai95a962c2014-10-29 16:03:58 +0100507/**
508 * snd_hda_codec_set_pin_target - remember the current pinctl target value
509 * @codec: the HDA codec
510 * @nid: pin NID
511 * @val: assigned pinctl value
512 *
513 * This function stores the given value to a pinctl target value in the
514 * pincfg table. This isn't always as same as the actually written value
515 * but can be referred at any time via snd_hda_codec_get_pin_target().
516 */
Takashi Iwaid7fdc002013-01-10 08:38:04 +0100517int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
518 unsigned int val)
519{
520 struct hda_pincfg *pin;
521
522 pin = look_up_pincfg(codec, &codec->init_pins, nid);
523 if (!pin)
524 return -EINVAL;
525 pin->target = val;
526 return 0;
527}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100528EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target);
Takashi Iwaid7fdc002013-01-10 08:38:04 +0100529
Takashi Iwai95a962c2014-10-29 16:03:58 +0100530/**
531 * snd_hda_codec_get_pin_target - return the current pinctl target value
532 * @codec: the HDA codec
533 * @nid: pin NID
534 */
Takashi Iwaid7fdc002013-01-10 08:38:04 +0100535int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
536{
537 struct hda_pincfg *pin;
538
539 pin = look_up_pincfg(codec, &codec->init_pins, nid);
540 if (!pin)
541 return 0;
542 return pin->target;
543}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100544EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target);
Takashi Iwaid7fdc002013-01-10 08:38:04 +0100545
Takashi Iwai92ee6162009-12-27 11:18:59 +0100546/**
547 * snd_hda_shutup_pins - Shut up all pins
548 * @codec: the HDA codec
549 *
550 * Clear all pin controls to shup up before suspend for avoiding click noise.
551 * The controls aren't cached so that they can be resumed properly.
552 */
553void snd_hda_shutup_pins(struct hda_codec *codec)
554{
555 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200556 /* don't shut up pins when unloading the driver; otherwise it breaks
557 * the default pin setup at the next load of the driver
558 */
559 if (codec->bus->shutdown)
560 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +0100561 for (i = 0; i < codec->init_pins.used; i++) {
562 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
563 /* use read here for syncing after issuing each verb */
564 snd_hda_codec_read(codec, pin->nid, 0,
565 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
566 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200567 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +0100568}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100569EXPORT_SYMBOL_GPL(snd_hda_shutup_pins);
Takashi Iwai92ee6162009-12-27 11:18:59 +0100570
Takashi Iwai2a439522011-07-26 09:52:50 +0200571#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200572/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
573static void restore_shutup_pins(struct hda_codec *codec)
574{
575 int i;
576 if (!codec->pins_shutup)
577 return;
578 if (codec->bus->shutdown)
579 return;
580 for (i = 0; i < codec->init_pins.used; i++) {
581 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
582 snd_hda_codec_write(codec, pin->nid, 0,
583 AC_VERB_SET_PIN_WIDGET_CONTROL,
584 pin->ctrl);
585 }
586 codec->pins_shutup = 0;
587}
Mike Waychison1c7276c2011-04-20 12:04:36 -0700588#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200589
David Henningsson26a6cb62012-10-09 15:04:21 +0200590static void hda_jackpoll_work(struct work_struct *work)
591{
592 struct hda_codec *codec =
593 container_of(work, struct hda_codec, jackpoll_work.work);
David Henningsson26a6cb62012-10-09 15:04:21 +0200594
595 snd_hda_jack_set_dirty_all(codec);
596 snd_hda_jack_poll_all(codec);
Wang Xingchao18e60622013-07-25 23:34:45 -0400597
598 if (!codec->jackpoll_interval)
599 return;
600
Takashi Iwai2f35c632015-02-27 22:43:26 +0100601 schedule_delayed_work(&codec->jackpoll_work,
602 codec->jackpoll_interval);
David Henningsson26a6cb62012-10-09 15:04:21 +0200603}
604
Takashi Iwai3fdf1462012-11-22 08:16:31 +0100605/* release all pincfg lists */
606static void free_init_pincfgs(struct hda_codec *codec)
Takashi Iwai3be14142009-02-20 14:11:16 +0100607{
Takashi Iwai346ff702009-02-23 09:42:57 +0100608 snd_array_free(&codec->driver_pins);
Takashi Iwai648a8d22014-02-25 10:38:13 +0100609#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai346ff702009-02-23 09:42:57 +0100610 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +0100611#endif
Takashi Iwai3be14142009-02-20 14:11:16 +0100612 snd_array_free(&codec->init_pins);
613}
614
Takashi Iwai54d17402005-11-21 16:33:22 +0100615/*
Takashi Iwaieb541332010-08-06 13:48:11 +0200616 * audio-converter setup caches
617 */
618struct hda_cvt_setup {
619 hda_nid_t nid;
620 u8 stream_tag;
621 u8 channel_id;
622 u16 format_id;
623 unsigned char active; /* cvt is currently used */
624 unsigned char dirty; /* setups should be cleared */
625};
626
627/* get or create a cache entry for the given audio converter NID */
628static struct hda_cvt_setup *
629get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
630{
631 struct hda_cvt_setup *p;
632 int i;
633
634 for (i = 0; i < codec->cvt_setups.used; i++) {
635 p = snd_array_elem(&codec->cvt_setups, i);
636 if (p->nid == nid)
637 return p;
638 }
639 p = snd_array_new(&codec->cvt_setups);
640 if (p)
641 p->nid = nid;
642 return p;
643}
644
645/*
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100646 * PCM device
647 */
648static void release_pcm(struct kref *kref)
649{
650 struct hda_pcm *pcm = container_of(kref, struct hda_pcm, kref);
651
652 if (pcm->pcm)
653 snd_device_free(pcm->codec->card, pcm->pcm);
654 clear_bit(pcm->device, pcm->codec->bus->pcm_dev_bits);
655 kfree(pcm->name);
656 kfree(pcm);
657}
658
659void snd_hda_codec_pcm_put(struct hda_pcm *pcm)
660{
661 kref_put(&pcm->kref, release_pcm);
662}
663EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_put);
664
665struct hda_pcm *snd_hda_codec_pcm_new(struct hda_codec *codec,
666 const char *fmt, ...)
667{
668 struct hda_pcm *pcm;
669 va_list args;
670
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100671 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
672 if (!pcm)
673 return NULL;
674
675 pcm->codec = codec;
676 kref_init(&pcm->kref);
Takashi Iwai30e5f002015-04-27 16:39:19 +0200677 va_start(args, fmt);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100678 pcm->name = kvasprintf(GFP_KERNEL, fmt, args);
Takashi Iwai30e5f002015-04-27 16:39:19 +0200679 va_end(args);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100680 if (!pcm->name) {
681 kfree(pcm);
682 return NULL;
683 }
684
685 list_add_tail(&pcm->list, &codec->pcm_list_head);
686 return pcm;
687}
688EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_new);
689
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100690/*
691 * codec destructor
692 */
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100693static void codec_release_pcms(struct hda_codec *codec)
694{
695 struct hda_pcm *pcm, *n;
696
697 list_for_each_entry_safe(pcm, n, &codec->pcm_list_head, list) {
698 list_del_init(&pcm->list);
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100699 if (pcm->pcm)
700 snd_device_disconnect(codec->card, pcm->pcm);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100701 snd_hda_codec_pcm_put(pcm);
702 }
703}
704
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100705void snd_hda_codec_cleanup_for_unbind(struct hda_codec *codec)
706{
Takashi Iwaic4c25332015-03-03 17:22:12 +0100707 if (codec->registered) {
708 /* pm_runtime_put() is called in snd_hdac_device_exit() */
709 pm_runtime_get_noresume(hda_codec_dev(codec));
710 pm_runtime_disable(hda_codec_dev(codec));
711 codec->registered = 0;
712 }
713
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100714 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100715 if (!codec->in_freeing)
716 snd_hda_ctls_clear(codec);
717 codec_release_pcms(codec);
718 snd_hda_detach_beep_device(codec);
719 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
720 snd_hda_jack_tbl_clear(codec);
721 codec->proc_widget_hook = NULL;
722 codec->spec = NULL;
723
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100724 /* free only driver_pins so that init_pins + user_pins are restored */
725 snd_array_free(&codec->driver_pins);
726 snd_array_free(&codec->cvt_setups);
727 snd_array_free(&codec->spdif_out);
728 snd_array_free(&codec->verbs);
729 codec->preset = NULL;
730 codec->slave_dig_outs = NULL;
731 codec->spdif_status_reset = 0;
732 snd_array_free(&codec->mixers);
733 snd_array_free(&codec->nids);
734 remove_conn_list(codec);
Takashi Iwai4d75faa02015-02-25 14:42:38 +0100735 snd_hdac_regmap_exit(&codec->core);
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100736}
737
Takashi Iwaid8193872012-08-31 07:54:38 -0700738static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwaibb6ac722009-03-13 09:02:42 +0100739 unsigned int power_state);
740
Takashi Iwaic4c25332015-03-03 17:22:12 +0100741/* also called from hda_bind.c */
742void snd_hda_codec_register(struct hda_codec *codec)
743{
744 if (codec->registered)
745 return;
746 if (device_is_registered(hda_codec_dev(codec))) {
747 snd_hda_register_beep_device(codec);
Mengdong Lina5e7e072015-04-29 17:43:20 +0800748 snd_hdac_link_power(&codec->core, true);
Takashi Iwaic4c25332015-03-03 17:22:12 +0100749 pm_runtime_enable(hda_codec_dev(codec));
750 /* it was powered up in snd_hda_codec_new(), now all done */
751 snd_hda_power_down(codec);
752 codec->registered = 1;
753 }
754}
755
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100756static int snd_hda_codec_dev_register(struct snd_device *device)
757{
Takashi Iwaic4c25332015-03-03 17:22:12 +0100758 snd_hda_codec_register(device->device_data);
Takashi Iwaid604b392014-02-28 13:42:09 +0100759 return 0;
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100760}
761
762static int snd_hda_codec_dev_disconnect(struct snd_device *device)
763{
764 struct hda_codec *codec = device->device_data;
765
Takashi Iwaid604b392014-02-28 13:42:09 +0100766 snd_hda_detach_beep_device(codec);
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100767 return 0;
768}
769
Takashi Iwai2565c892014-02-19 11:41:09 +0100770static int snd_hda_codec_dev_free(struct snd_device *device)
771{
Takashi Iwaid56db742015-02-27 23:25:35 +0100772 struct hda_codec *codec = device->device_data;
773
774 codec->in_freeing = 1;
Takashi Iwai3256be62015-02-24 14:59:42 +0100775 snd_hdac_device_unregister(&codec->core);
Takashi Iwaia4577822015-06-11 14:02:49 +0200776 snd_hdac_link_power(&codec->core, false);
Takashi Iwaid56db742015-02-27 23:25:35 +0100777 put_device(hda_codec_dev(codec));
Takashi Iwai2565c892014-02-19 11:41:09 +0100778 return 0;
779}
780
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100781static void snd_hda_codec_dev_release(struct device *dev)
782{
Takashi Iwaid56db742015-02-27 23:25:35 +0100783 struct hda_codec *codec = dev_to_hda_codec(dev);
784
785 free_init_pincfgs(codec);
Takashi Iwai7639a062015-03-03 10:07:24 +0100786 snd_hdac_device_exit(&codec->core);
Takashi Iwaid56db742015-02-27 23:25:35 +0100787 snd_hda_sysfs_clear(codec);
Takashi Iwaid56db742015-02-27 23:25:35 +0100788 kfree(codec->modelname);
789 kfree(codec->wcaps);
Takashi Iwaid56db742015-02-27 23:25:35 +0100790 kfree(codec);
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100791}
792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793/**
794 * snd_hda_codec_new - create a HDA codec
795 * @bus: the bus to assign
796 * @codec_addr: the codec address
797 * @codecp: the pointer to store the generated codec
798 *
799 * Returns 0 if successful, or a negative error code.
800 */
Takashi Iwai6efdd852015-02-27 16:09:22 +0100801int snd_hda_codec_new(struct hda_bus *bus, struct snd_card *card,
802 unsigned int codec_addr, struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803{
804 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +0200805 char component[31];
Takashi Iwaid8193872012-08-31 07:54:38 -0700806 hda_nid_t fg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 int err;
Takashi Iwai2565c892014-02-19 11:41:09 +0100808 static struct snd_device_ops dev_ops = {
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100809 .dev_register = snd_hda_codec_dev_register,
810 .dev_disconnect = snd_hda_codec_dev_disconnect,
Takashi Iwai2565c892014-02-19 11:41:09 +0100811 .dev_free = snd_hda_codec_dev_free,
812 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Takashi Iwaida3cec32008-08-08 17:12:14 +0200814 if (snd_BUG_ON(!bus))
815 return -EINVAL;
816 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
817 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200819 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Takashi Iwaif4de8fe2015-02-27 16:17:18 +0100820 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Takashi Iwai7639a062015-03-03 10:07:24 +0100823 sprintf(component, "hdaudioC%dD%d", card->number, codec_addr);
824 err = snd_hdac_device_init(&codec->core, &bus->core, component,
825 codec_addr);
826 if (err < 0) {
827 kfree(codec);
828 return err;
829 }
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100830
Takashi Iwai7639a062015-03-03 10:07:24 +0100831 codec->core.dev.release = snd_hda_codec_dev_release;
832 codec->core.type = HDA_DEV_LEGACY;
Takashi Iwai05852442015-03-03 15:40:08 +0100833 codec->core.exec_verb = codec_exec_verb;
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 codec->bus = bus;
Takashi Iwai6efdd852015-02-27 16:09:22 +0100836 codec->card = card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +0100838 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +0800839 mutex_init(&codec->control_mutex);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +0100840 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
841 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +0100842 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +0100843 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +0200844 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Stephen Warren7c9359762011-06-01 11:14:17 -0600845 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai361dab32012-05-09 14:35:27 +0200846 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100847 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100848 INIT_LIST_HEAD(&codec->conn_list);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100849 INIT_LIST_HEAD(&codec->pcm_list_head);
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100850
David Henningsson26a6cb62012-10-09 15:04:21 +0200851 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
Mengdong Lin7f132922013-11-29 01:48:45 -0500852 codec->depop_delay = -1;
David Henningssonf5662e12014-07-22 14:09:34 +0200853 codec->fixup_id = HDA_FIXUP_ID_NOT_SET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Takashi Iwai83012a72012-08-24 18:38:08 +0200855#ifdef CONFIG_PM
Takashi Iwaicc72da72015-02-19 16:00:22 +0100856 codec->power_jiffies = jiffies;
Takashi Iwaicb53c622007-08-10 17:21:45 +0200857#endif
858
Takashi Iwai648a8d22014-02-25 10:38:13 +0100859 snd_hda_sysfs_init(codec);
860
Takashi Iwaic382a9f2012-05-07 15:01:02 +0200861 if (codec->bus->modelname) {
862 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
863 if (!codec->modelname) {
Heloise NH6986a0e2015-07-17 09:42:06 +0800864 err = -ENOMEM;
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100865 goto error;
Takashi Iwaic382a9f2012-05-07 15:01:02 +0200866 }
867 }
868
Takashi Iwai7639a062015-03-03 10:07:24 +0100869 fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
Takashi Iwaid8193872012-08-31 07:54:38 -0700870 err = read_widget_caps(codec, fg);
Takashi Iwaif4de8fe2015-02-27 16:17:18 +0100871 if (err < 0)
Takashi Iwai3be14142009-02-20 14:11:16 +0100872 goto error;
Takashi Iwai3be14142009-02-20 14:11:16 +0100873 err = read_pin_defaults(codec);
874 if (err < 0)
875 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +0100876
Takashi Iwaibb6ac722009-03-13 09:02:42 +0100877 /* power-up all before initialization */
Takashi Iwaid8193872012-08-31 07:54:38 -0700878 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaibb6ac722009-03-13 09:02:42 +0100879
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200880 snd_hda_codec_proc_new(codec);
881
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200882 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200883
Takashi Iwai7639a062015-03-03 10:07:24 +0100884 sprintf(component, "HDA:%08x,%08x,%08x", codec->core.vendor_id,
885 codec->core.subsystem_id, codec->core.revision_id);
Takashi Iwai6efdd852015-02-27 16:09:22 +0100886 snd_component_add(card, component);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200887
Takashi Iwai6efdd852015-02-27 16:09:22 +0100888 err = snd_device_new(card, SNDRV_DEV_CODEC, codec, &dev_ops);
Takashi Iwai2565c892014-02-19 11:41:09 +0100889 if (err < 0)
890 goto error;
891
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200892 if (codecp)
893 *codecp = codec;
894 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +0100895
896 error:
Takashi Iwaid56db742015-02-27 23:25:35 +0100897 put_device(hda_codec_dev(codec));
Takashi Iwai3be14142009-02-20 14:11:16 +0100898 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200899}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100900EXPORT_SYMBOL_GPL(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200901
Takashi Iwai95a962c2014-10-29 16:03:58 +0100902/**
903 * snd_hda_codec_update_widgets - Refresh widget caps and pin defaults
904 * @codec: the HDA codec
905 *
906 * Forcibly refresh the all widget caps and the init pin configurations of
907 * the given codec.
908 */
Mengdong Lina15d05d2013-02-08 17:09:31 -0500909int snd_hda_codec_update_widgets(struct hda_codec *codec)
910{
911 hda_nid_t fg;
912 int err;
913
Takashi Iwai9dcc144b2015-08-25 08:52:55 +0200914 err = snd_hdac_refresh_widget_sysfs(&codec->core);
Takashi Iwai7639a062015-03-03 10:07:24 +0100915 if (err < 0)
916 return err;
917
Mengdong Lina15d05d2013-02-08 17:09:31 -0500918 /* Assume the function group node does not change,
919 * only the widget nodes may change.
920 */
921 kfree(codec->wcaps);
Takashi Iwai7639a062015-03-03 10:07:24 +0100922 fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
Mengdong Lina15d05d2013-02-08 17:09:31 -0500923 err = read_widget_caps(codec, fg);
Takashi Iwaif4de8fe2015-02-27 16:17:18 +0100924 if (err < 0)
Mengdong Lina15d05d2013-02-08 17:09:31 -0500925 return err;
Mengdong Lina15d05d2013-02-08 17:09:31 -0500926
927 snd_array_free(&codec->init_pins);
928 err = read_pin_defaults(codec);
929
930 return err;
931}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100932EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets);
Mengdong Lina15d05d2013-02-08 17:09:31 -0500933
Takashi Iwaied360812012-08-08 17:12:52 +0200934/* update the stream-id if changed */
935static void update_pcm_stream_id(struct hda_codec *codec,
936 struct hda_cvt_setup *p, hda_nid_t nid,
937 u32 stream_tag, int channel_id)
938{
939 unsigned int oldval, newval;
940
941 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
942 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
943 newval = (stream_tag << 4) | channel_id;
944 if (oldval != newval)
945 snd_hda_codec_write(codec, nid, 0,
946 AC_VERB_SET_CHANNEL_STREAMID,
947 newval);
948 p->stream_tag = stream_tag;
949 p->channel_id = channel_id;
950 }
951}
952
953/* update the format-id if changed */
954static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
955 hda_nid_t nid, int format)
956{
957 unsigned int oldval;
958
959 if (p->format_id != format) {
960 oldval = snd_hda_codec_read(codec, nid, 0,
961 AC_VERB_GET_STREAM_FORMAT, 0);
962 if (oldval != format) {
963 msleep(1);
964 snd_hda_codec_write(codec, nid, 0,
965 AC_VERB_SET_STREAM_FORMAT,
966 format);
967 }
968 p->format_id = format;
969 }
970}
971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972/**
973 * snd_hda_codec_setup_stream - set up the codec for streaming
974 * @codec: the CODEC to set up
975 * @nid: the NID to set up
976 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
977 * @channel_id: channel id to pass, zero based.
978 * @format: stream format.
979 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200980void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
981 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 int channel_id, int format)
983{
Takashi Iwai3f50ac62010-08-20 09:44:36 +0200984 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +0200985 struct hda_cvt_setup *p;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +0200986 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +0200987 int i;
988
Takashi Iwai0ba21762007-04-16 11:29:14 +0200989 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +0200990 return;
991
Takashi Iwai4e76a882014-02-25 12:21:03 +0100992 codec_dbg(codec,
993 "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
994 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +0200995 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +0200996 if (!p)
Takashi Iwaieb541332010-08-06 13:48:11 +0200997 return;
Takashi Iwaied360812012-08-08 17:12:52 +0200998
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100999 if (codec->patch_ops.stream_pm)
1000 codec->patch_ops.stream_pm(codec, nid, true);
Takashi Iwaied360812012-08-08 17:12:52 +02001001 if (codec->pcm_format_first)
1002 update_pcm_format(codec, p, nid, format);
1003 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1004 if (!codec->pcm_format_first)
1005 update_pcm_format(codec, p, nid, format);
1006
Takashi Iwaieb541332010-08-06 13:48:11 +02001007 p->active = 1;
1008 p->dirty = 0;
1009
1010 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001011 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwaid068ebc2015-03-02 23:22:59 +01001012 list_for_each_codec(c, codec->bus) {
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001013 for (i = 0; i < c->cvt_setups.used; i++) {
1014 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001015 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001016 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001017 p->dirty = 1;
1018 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001019 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001021EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Takashi Iwaif0cea792010-08-13 11:56:53 +02001023static void really_cleanup_stream(struct hda_codec *codec,
1024 struct hda_cvt_setup *q);
1025
Takashi Iwaid5191e52009-11-16 14:58:17 +01001026/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001027 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001028 * @codec: the CODEC to clean up
1029 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001030 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001031 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001032void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1033 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001034{
Takashi Iwaieb541332010-08-06 13:48:11 +02001035 struct hda_cvt_setup *p;
1036
Takashi Iwai888afa12008-03-18 09:57:50 +01001037 if (!nid)
1038 return;
1039
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001040 if (codec->no_sticky_stream)
1041 do_now = 1;
1042
Takashi Iwai4e76a882014-02-25 12:21:03 +01001043 codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001044 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001045 if (p) {
Takashi Iwaif0cea792010-08-13 11:56:53 +02001046 /* here we just clear the active flag when do_now isn't set;
1047 * actual clean-ups will be done later in
1048 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1049 */
1050 if (do_now)
1051 really_cleanup_stream(codec, p);
1052 else
1053 p->active = 0;
1054 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001055}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001056EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001057
Takashi Iwaieb541332010-08-06 13:48:11 +02001058static void really_cleanup_stream(struct hda_codec *codec,
1059 struct hda_cvt_setup *q)
1060{
1061 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001062 if (q->stream_tag || q->channel_id)
1063 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1064 if (q->format_id)
1065 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1066);
Takashi Iwaieb541332010-08-06 13:48:11 +02001067 memset(q, 0, sizeof(*q));
1068 q->nid = nid;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01001069 if (codec->patch_ops.stream_pm)
1070 codec->patch_ops.stream_pm(codec, nid, false);
Takashi Iwaieb541332010-08-06 13:48:11 +02001071}
1072
1073/* clean up the all conflicting obsolete streams */
1074static void purify_inactive_streams(struct hda_codec *codec)
1075{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001076 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001077 int i;
1078
Takashi Iwaid068ebc2015-03-02 23:22:59 +01001079 list_for_each_codec(c, codec->bus) {
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001080 for (i = 0; i < c->cvt_setups.used; i++) {
1081 struct hda_cvt_setup *p;
1082 p = snd_array_elem(&c->cvt_setups, i);
1083 if (p->dirty)
1084 really_cleanup_stream(c, p);
1085 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001086 }
1087}
1088
Takashi Iwai2a439522011-07-26 09:52:50 +02001089#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001090/* clean up all streams; called from suspend */
1091static void hda_cleanup_all_streams(struct hda_codec *codec)
1092{
1093 int i;
1094
1095 for (i = 0; i < codec->cvt_setups.used; i++) {
1096 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1097 if (p->stream_tag)
1098 really_cleanup_stream(codec, p);
1099 }
1100}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001101#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001102
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103/*
1104 * amp access functions
1105 */
1106
Takashi Iwaid5191e52009-11-16 14:58:17 +01001107/**
1108 * query_amp_caps - query AMP capabilities
1109 * @codec: the HD-auio codec
1110 * @nid: the NID to query
1111 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1112 *
1113 * Query AMP capabilities for the given widget and direction.
1114 * Returns the obtained capability bits.
1115 *
1116 * When cap bits have been already read, this doesn't read again but
1117 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001119u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120{
Takashi Iwaifaa75f82015-02-26 08:54:56 +01001121 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1122 nid = codec->core.afg;
1123 return snd_hda_param_read(codec, nid,
1124 direction == HDA_OUTPUT ?
1125 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001127EXPORT_SYMBOL_GPL(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Takashi Iwaid5191e52009-11-16 14:58:17 +01001129/**
David Henningsson861a04e2014-09-23 10:38:17 +02001130 * snd_hda_check_amp_caps - query AMP capabilities
1131 * @codec: the HD-audio codec
1132 * @nid: the NID to query
1133 * @dir: either #HDA_INPUT or #HDA_OUTPUT
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001134 * @bits: bit mask to check the result
David Henningsson861a04e2014-09-23 10:38:17 +02001135 *
1136 * Check whether the widget has the given amp capability for the direction.
1137 */
1138bool snd_hda_check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
1139 int dir, unsigned int bits)
1140{
1141 if (!nid)
1142 return false;
1143 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
1144 if (query_amp_caps(codec, nid, dir) & bits)
1145 return true;
1146 return false;
1147}
1148EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps);
1149
1150/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01001151 * snd_hda_override_amp_caps - Override the AMP capabilities
1152 * @codec: the CODEC to clean up
1153 * @nid: the NID to clean up
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001154 * @dir: either #HDA_INPUT or #HDA_OUTPUT
Takashi Iwaid5191e52009-11-16 14:58:17 +01001155 * @caps: the capability bits to set
1156 *
1157 * Override the cached AMP caps bits value by the given one.
1158 * This function is useful if the driver needs to adjust the AMP ranges,
1159 * e.g. limit to 0dB, etc.
1160 *
1161 * Returns zero if successful or a negative error code.
1162 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001163int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1164 unsigned int caps)
1165{
Takashi Iwaifaa75f82015-02-26 08:54:56 +01001166 unsigned int parm;
1167
1168 snd_hda_override_wcaps(codec, nid,
1169 get_wcaps(codec, nid) | AC_WCAP_AMP_OVRD);
1170 parm = dir == HDA_OUTPUT ? AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP;
1171 return snd_hdac_override_parm(&codec->core, nid, parm, caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001172}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001173EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001174
Takashi Iwaid5191e52009-11-16 14:58:17 +01001175/**
Takashi Iwaia686ec42015-06-11 10:51:28 +02001176 * snd_hda_codec_amp_update - update the AMP mono value
1177 * @codec: HD-audio codec
1178 * @nid: NID to read the AMP value
1179 * @ch: channel to update (0 or 1)
1180 * @dir: #HDA_INPUT or #HDA_OUTPUT
1181 * @idx: the index value (only for input direction)
1182 * @mask: bit mask to set
1183 * @val: the bits value to set
1184 *
1185 * Update the AMP values for the given channel, direction and index.
1186 */
1187int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid,
1188 int ch, int dir, int idx, int mask, int val)
1189{
1190 unsigned int cmd = snd_hdac_regmap_encode_amp(nid, ch, dir, idx);
1191
1192 /* enable fake mute if no h/w mute but min=mute */
1193 if ((query_amp_caps(codec, nid, dir) &
1194 (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) == AC_AMPCAP_MIN_MUTE)
1195 cmd |= AC_AMP_FAKE_MUTE;
1196 return snd_hdac_regmap_update_raw(&codec->core, cmd, mask, val);
1197}
1198EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update);
1199
1200/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01001201 * snd_hda_codec_amp_stereo - update the AMP stereo values
1202 * @codec: HD-audio codec
1203 * @nid: NID to read the AMP value
1204 * @direction: #HDA_INPUT or #HDA_OUTPUT
1205 * @idx: the index value (only for input direction)
1206 * @mask: bit mask to set
1207 * @val: the bits value to set
1208 *
1209 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1210 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001211 */
1212int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1213 int direction, int idx, int mask, int val)
1214{
1215 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001216
1217 if (snd_BUG_ON(mask & ~0xff))
1218 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001219 for (ch = 0; ch < 2; ch++)
1220 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1221 idx, mask, val);
1222 return ret;
1223}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001224EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001225
Takashi Iwai95a962c2014-10-29 16:03:58 +01001226/**
1227 * snd_hda_codec_amp_init - initialize the AMP value
1228 * @codec: the HDA codec
1229 * @nid: NID to read the AMP value
1230 * @ch: channel (left=0 or right=1)
1231 * @dir: #HDA_INPUT or #HDA_OUTPUT
1232 * @idx: the index value (only for input direction)
1233 * @mask: bit mask to set
1234 * @val: the bits value to set
1235 *
1236 * Works like snd_hda_codec_amp_update() but it writes the value only at
Takashi Iwai280e57d2012-12-14 10:32:21 +01001237 * the first access. If the amp was already initialized / updated beforehand,
1238 * this does nothing.
1239 */
1240int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
1241 int dir, int idx, int mask, int val)
1242{
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01001243 int orig;
1244
1245 if (!codec->core.regmap)
1246 return -EINVAL;
1247 regcache_cache_only(codec->core.regmap, true);
1248 orig = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1249 regcache_cache_only(codec->core.regmap, false);
1250 if (orig >= 0)
1251 return 0;
1252 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx, mask, val);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001253}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001254EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001255
Takashi Iwai95a962c2014-10-29 16:03:58 +01001256/**
1257 * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value
1258 * @codec: the HDA codec
1259 * @nid: NID to read the AMP value
1260 * @dir: #HDA_INPUT or #HDA_OUTPUT
1261 * @idx: the index value (only for input direction)
1262 * @mask: bit mask to set
1263 * @val: the bits value to set
1264 *
1265 * Call snd_hda_codec_amp_init() for both stereo channels.
1266 */
Takashi Iwai280e57d2012-12-14 10:32:21 +01001267int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
1268 int dir, int idx, int mask, int val)
1269{
1270 int ch, ret = 0;
1271
1272 if (snd_BUG_ON(mask & ~0xff))
1273 mask &= 0xff;
1274 for (ch = 0; ch < 2; ch++)
1275 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
1276 idx, mask, val);
1277 return ret;
1278}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001279EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001280
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001281static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1282 unsigned int ofs)
1283{
1284 u32 caps = query_amp_caps(codec, nid, dir);
1285 /* get num steps */
1286 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1287 if (ofs < caps)
1288 caps -= ofs;
1289 return caps;
1290}
1291
Takashi Iwaid5191e52009-11-16 14:58:17 +01001292/**
1293 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001294 * @kcontrol: referred ctl element
1295 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01001296 *
1297 * The control element is supposed to have the private_value field
1298 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1299 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001300int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1301 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302{
1303 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1304 u16 nid = get_amp_nid(kcontrol);
1305 u8 chs = get_amp_channels(kcontrol);
1306 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001307 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001309 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1310 uinfo->count = chs == 3 ? 2 : 1;
1311 uinfo->value.integer.min = 0;
1312 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1313 if (!uinfo->value.integer.max) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001314 codec_warn(codec,
1315 "num_steps = 0 for NID=0x%x (ctl = %s)\n",
1316 nid, kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 return -EINVAL;
1318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 return 0;
1320}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001321EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001323
1324static inline unsigned int
1325read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1326 int ch, int dir, int idx, unsigned int ofs)
1327{
1328 unsigned int val;
1329 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1330 val &= HDA_AMP_VOLMASK;
1331 if (val >= ofs)
1332 val -= ofs;
1333 else
1334 val = 0;
1335 return val;
1336}
1337
1338static inline int
1339update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1340 int ch, int dir, int idx, unsigned int ofs,
1341 unsigned int val)
1342{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001343 unsigned int maxval;
1344
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001345 if (val > 0)
1346 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02001347 /* ofs = 0: raw max value */
1348 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001349 if (val > maxval)
1350 val = maxval;
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01001351 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1352 HDA_AMP_VOLMASK, val);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001353}
1354
Takashi Iwaid5191e52009-11-16 14:58:17 +01001355/**
1356 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001357 * @kcontrol: ctl element
1358 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01001359 *
1360 * The control element is supposed to have the private_value field
1361 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1362 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001363int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1364 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365{
1366 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1367 hda_nid_t nid = get_amp_nid(kcontrol);
1368 int chs = get_amp_channels(kcontrol);
1369 int dir = get_amp_direction(kcontrol);
1370 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001371 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 long *valp = ucontrol->value.integer.value;
1373
1374 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001375 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001377 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 return 0;
1379}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001380EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Takashi Iwaid5191e52009-11-16 14:58:17 +01001382/**
1383 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001384 * @kcontrol: ctl element
1385 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01001386 *
1387 * The control element is supposed to have the private_value field
1388 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1389 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001390int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1391 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392{
1393 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1394 hda_nid_t nid = get_amp_nid(kcontrol);
1395 int chs = get_amp_channels(kcontrol);
1396 int dir = get_amp_direction(kcontrol);
1397 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001398 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 long *valp = ucontrol->value.integer.value;
1400 int change = 0;
1401
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001402 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001403 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001404 valp++;
1405 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001406 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001407 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 return change;
1409}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001410EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Takashi Iwaid5191e52009-11-16 14:58:17 +01001412/**
1413 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001414 * @kcontrol: ctl element
1415 * @op_flag: operation flag
1416 * @size: byte size of input TLV
1417 * @_tlv: TLV data
Takashi Iwaid5191e52009-11-16 14:58:17 +01001418 *
1419 * The control element is supposed to have the private_value field
1420 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1421 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001422int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1423 unsigned int size, unsigned int __user *_tlv)
1424{
1425 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1426 hda_nid_t nid = get_amp_nid(kcontrol);
1427 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001428 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02001429 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001430 u32 caps, val1, val2;
1431
1432 if (size < 4 * sizeof(unsigned int))
1433 return -ENOMEM;
1434 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001435 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1436 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001437 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001438 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001439 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01001440 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02001441 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001442 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1443 return -EFAULT;
1444 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1445 return -EFAULT;
1446 if (put_user(val1, _tlv + 2))
1447 return -EFAULT;
1448 if (put_user(val2, _tlv + 3))
1449 return -EFAULT;
1450 return 0;
1451}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001452EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001453
Takashi Iwaid5191e52009-11-16 14:58:17 +01001454/**
1455 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
1456 * @codec: HD-audio codec
1457 * @nid: NID of a reference widget
1458 * @dir: #HDA_INPUT or #HDA_OUTPUT
1459 * @tlv: TLV data to be stored, at least 4 elements
1460 *
1461 * Set (static) TLV data for a virtual master volume using the AMP caps
1462 * obtained from the reference NID.
1463 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01001464 */
1465void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1466 unsigned int *tlv)
1467{
1468 u32 caps;
1469 int nums, step;
1470
1471 caps = query_amp_caps(codec, nid, dir);
1472 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1473 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1474 step = (step + 1) * 25;
1475 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1476 tlv[1] = 2 * sizeof(unsigned int);
1477 tlv[2] = -nums * step;
1478 tlv[3] = step;
1479}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001480EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001481
1482/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01001483static struct snd_kcontrol *
Takashi Iwaidcda5802012-10-12 17:24:51 +02001484find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01001485{
1486 struct snd_ctl_elem_id id;
1487 memset(&id, 0, sizeof(id));
1488 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwaidcda5802012-10-12 17:24:51 +02001489 id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01001490 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02001491 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
1492 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01001493 strcpy(id.name, name);
Takashi Iwai6efdd852015-02-27 16:09:22 +01001494 return snd_ctl_find_id(codec->card, &id);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001495}
1496
Takashi Iwaid5191e52009-11-16 14:58:17 +01001497/**
1498 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
1499 * @codec: HD-audio codec
1500 * @name: ctl id name string
1501 *
1502 * Get the control element with the given id string and IFACE_MIXER.
1503 */
Takashi Iwai09f99702008-02-04 12:31:13 +01001504struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1505 const char *name)
1506{
Takashi Iwaidcda5802012-10-12 17:24:51 +02001507 return find_mixer_ctl(codec, name, 0, 0);
Takashi Iwai09f99702008-02-04 12:31:13 +01001508}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001509EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01001510
Takashi Iwaidcda5802012-10-12 17:24:51 +02001511static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01001512 int start_idx)
Takashi Iwai1afe2062010-12-23 10:17:52 +01001513{
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01001514 int i, idx;
1515 /* 16 ctlrs should be large enough */
1516 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
1517 if (!find_mixer_ctl(codec, name, 0, idx))
Takashi Iwai1afe2062010-12-23 10:17:52 +01001518 return idx;
1519 }
1520 return -EBUSY;
1521}
1522
Takashi Iwaid5191e52009-11-16 14:58:17 +01001523/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001524 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01001525 * @codec: HD-audio codec
1526 * @nid: corresponding NID (optional)
1527 * @kctl: the control element to assign
1528 *
1529 * Add the given control element to an array inside the codec instance.
1530 * All control elements belonging to a codec are supposed to be added
1531 * by this function so that a proper clean-up works at the free or
1532 * reconfiguration time.
1533 *
1534 * If non-zero @nid is passed, the NID is assigned to the control element.
1535 * The assignment is shown in the codec proc file.
1536 *
1537 * snd_hda_ctl_add() checks the control subdev id field whether
1538 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001539 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
1540 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01001541 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001542int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
1543 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02001544{
1545 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001546 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001547 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001548
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001549 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001550 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001551 if (nid == 0)
1552 nid = get_amp_nid_(kctl->private_value);
1553 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001554 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
1555 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001556 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01001557 kctl->id.subdevice = 0;
Takashi Iwai6efdd852015-02-27 16:09:22 +01001558 err = snd_ctl_add(codec->card, kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001559 if (err < 0)
1560 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001561 item = snd_array_new(&codec->mixers);
1562 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02001563 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001564 item->kctl = kctl;
1565 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001566 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001567 return 0;
1568}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001569EXPORT_SYMBOL_GPL(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001570
Takashi Iwaid5191e52009-11-16 14:58:17 +01001571/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001572 * snd_hda_add_nid - Assign a NID to a control element
1573 * @codec: HD-audio codec
1574 * @nid: corresponding NID (optional)
1575 * @kctl: the control element to assign
1576 * @index: index to kctl
1577 *
1578 * Add the given control element to an array inside the codec instance.
1579 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
1580 * NID:KCTL mapping - for example "Capture Source" selector.
1581 */
1582int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
1583 unsigned int index, hda_nid_t nid)
1584{
1585 struct hda_nid_item *item;
1586
1587 if (nid > 0) {
1588 item = snd_array_new(&codec->nids);
1589 if (!item)
1590 return -ENOMEM;
1591 item->kctl = kctl;
1592 item->index = index;
1593 item->nid = nid;
1594 return 0;
1595 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01001596 codec_err(codec, "no NID for mapping control %s:%d:%d\n",
1597 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001598 return -EINVAL;
1599}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001600EXPORT_SYMBOL_GPL(snd_hda_add_nid);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001601
1602/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01001603 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
1604 * @codec: HD-audio codec
1605 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02001606void snd_hda_ctls_clear(struct hda_codec *codec)
1607{
1608 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001609 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001610 for (i = 0; i < codec->mixers.used; i++)
Takashi Iwai6efdd852015-02-27 16:09:22 +01001611 snd_ctl_remove(codec->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001612 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001613 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001614}
1615
Takashi Iwai95a962c2014-10-29 16:03:58 +01001616/**
1617 * snd_hda_lock_devices - pseudo device locking
1618 * @bus: the BUS
1619 *
Takashi Iwaia65d6292009-02-23 16:57:04 +01001620 * toggle card->shutdown to allow/disallow the device access (as a hack)
1621 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001622int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001623{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001624 struct snd_card *card = bus->card;
1625 struct hda_codec *codec;
1626
Takashi Iwaia65d6292009-02-23 16:57:04 +01001627 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001628 if (card->shutdown)
1629 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01001630 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001631 if (!list_empty(&card->ctl_files))
1632 goto err_clear;
1633
Takashi Iwaid068ebc2015-03-02 23:22:59 +01001634 list_for_each_codec(codec, bus) {
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01001635 struct hda_pcm *cpcm;
1636 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001637 if (!cpcm->pcm)
1638 continue;
1639 if (cpcm->pcm->streams[0].substream_opened ||
1640 cpcm->pcm->streams[1].substream_opened)
1641 goto err_clear;
1642 }
1643 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01001644 spin_unlock(&card->files_lock);
1645 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01001646
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001647 err_clear:
1648 card->shutdown = 0;
1649 err_unlock:
1650 spin_unlock(&card->files_lock);
1651 return -EINVAL;
1652}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001653EXPORT_SYMBOL_GPL(snd_hda_lock_devices);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001654
Takashi Iwai95a962c2014-10-29 16:03:58 +01001655/**
1656 * snd_hda_unlock_devices - pseudo device unlocking
1657 * @bus: the BUS
1658 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001659void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01001660{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001661 struct snd_card *card = bus->card;
1662
Takashi Iwaia65d6292009-02-23 16:57:04 +01001663 spin_lock(&card->files_lock);
1664 card->shutdown = 0;
1665 spin_unlock(&card->files_lock);
1666}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001667EXPORT_SYMBOL_GPL(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01001668
Takashi Iwaid5191e52009-11-16 14:58:17 +01001669/**
1670 * snd_hda_codec_reset - Clear all objects assigned to the codec
1671 * @codec: HD-audio codec
1672 *
1673 * This frees the all PCM and control elements assigned to the codec, and
1674 * clears the caches and restores the pin default configurations.
1675 *
1676 * When a device is being used, it returns -EBSY. If successfully freed,
1677 * returns zero.
1678 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01001679int snd_hda_codec_reset(struct hda_codec *codec)
1680{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001681 struct hda_bus *bus = codec->bus;
Takashi Iwaia65d6292009-02-23 16:57:04 +01001682
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001683 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01001684 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01001685
1686 /* OK, let it free */
Takashi Iwai3256be62015-02-24 14:59:42 +01001687 snd_hdac_device_unregister(&codec->core);
Takashi Iwaid8a766a2015-02-17 15:25:37 +01001688
Takashi Iwaia65d6292009-02-23 16:57:04 +01001689 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001690 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01001691 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001692}
1693
Takashi Iwai6194b992014-06-06 18:12:16 +02001694typedef int (*map_slave_func_t)(struct hda_codec *, void *, struct snd_kcontrol *);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01001695
1696/* apply the function to all matching slave ctls in the mixer list */
1697static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01001698 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01001699{
1700 struct hda_nid_item *items;
1701 const char * const *s;
1702 int i, err;
1703
1704 items = codec->mixers.list;
1705 for (i = 0; i < codec->mixers.used; i++) {
1706 struct snd_kcontrol *sctl = items[i].kctl;
Takashi Iwaica16ec02013-10-28 12:00:35 +01001707 if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01001708 continue;
1709 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01001710 char tmpname[sizeof(sctl->id.name)];
1711 const char *name = *s;
1712 if (suffix) {
1713 snprintf(tmpname, sizeof(tmpname), "%s %s",
1714 name, suffix);
1715 name = tmpname;
1716 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01001717 if (!strcmp(sctl->id.name, name)) {
Takashi Iwai6194b992014-06-06 18:12:16 +02001718 err = func(codec, data, sctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01001719 if (err)
1720 return err;
1721 break;
1722 }
1723 }
1724 }
1725 return 0;
1726}
1727
Takashi Iwai6194b992014-06-06 18:12:16 +02001728static int check_slave_present(struct hda_codec *codec,
1729 void *data, struct snd_kcontrol *sctl)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01001730{
1731 return 1;
1732}
1733
Takashi Iwai18478e82012-03-09 17:51:10 +01001734/* guess the value corresponding to 0dB */
Takashi Iwai6194b992014-06-06 18:12:16 +02001735static int get_kctl_0dB_offset(struct hda_codec *codec,
1736 struct snd_kcontrol *kctl, int *step_to_check)
Takashi Iwai18478e82012-03-09 17:51:10 +01001737{
1738 int _tlv[4];
1739 const int *tlv = NULL;
1740 int val = -1;
1741
1742 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
1743 /* FIXME: set_fs() hack for obtaining user-space TLV data */
1744 mm_segment_t fs = get_fs();
1745 set_fs(get_ds());
1746 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
1747 tlv = _tlv;
1748 set_fs(fs);
1749 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
1750 tlv = kctl->tlv.p;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01001751 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) {
1752 int step = tlv[3];
1753 step &= ~TLV_DB_SCALE_MUTE;
1754 if (!step)
1755 return -1;
Takashi Iwai485e3e02013-11-04 15:51:00 +01001756 if (*step_to_check && *step_to_check != step) {
Takashi Iwai6194b992014-06-06 18:12:16 +02001757 codec_err(codec, "Mismatching dB step for vmaster slave (%d!=%d)\n",
Takashi Iwai4e76a882014-02-25 12:21:03 +01001758- *step_to_check, step);
Takashi Iwai485e3e02013-11-04 15:51:00 +01001759 return -1;
1760 }
1761 *step_to_check = step;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01001762 val = -tlv[2] / step;
1763 }
Takashi Iwai18478e82012-03-09 17:51:10 +01001764 return val;
1765}
1766
1767/* call kctl->put with the given value(s) */
1768static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
1769{
1770 struct snd_ctl_elem_value *ucontrol;
1771 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
1772 if (!ucontrol)
1773 return -ENOMEM;
1774 ucontrol->value.integer.value[0] = val;
1775 ucontrol->value.integer.value[1] = val;
1776 kctl->put(kctl, ucontrol);
1777 kfree(ucontrol);
1778 return 0;
1779}
1780
1781/* initialize the slave volume with 0dB */
Takashi Iwai6194b992014-06-06 18:12:16 +02001782static int init_slave_0dB(struct hda_codec *codec,
1783 void *data, struct snd_kcontrol *slave)
Takashi Iwai18478e82012-03-09 17:51:10 +01001784{
Takashi Iwai6194b992014-06-06 18:12:16 +02001785 int offset = get_kctl_0dB_offset(codec, slave, data);
Takashi Iwai18478e82012-03-09 17:51:10 +01001786 if (offset > 0)
1787 put_kctl_with_value(slave, offset);
1788 return 0;
1789}
1790
1791/* unmute the slave */
Takashi Iwai6194b992014-06-06 18:12:16 +02001792static int init_slave_unmute(struct hda_codec *codec,
1793 void *data, struct snd_kcontrol *slave)
Takashi Iwai18478e82012-03-09 17:51:10 +01001794{
1795 return put_kctl_with_value(slave, 1);
1796}
1797
Takashi Iwaie8750942014-06-30 14:02:39 +02001798static int add_slave(struct hda_codec *codec,
1799 void *data, struct snd_kcontrol *slave)
1800{
1801 return snd_ctl_add_slave(data, slave);
1802}
1803
Takashi Iwaid5191e52009-11-16 14:58:17 +01001804/**
Takashi Iwai95a962c2014-10-29 16:03:58 +01001805 * __snd_hda_add_vmaster - create a virtual master control and add slaves
Takashi Iwaid5191e52009-11-16 14:58:17 +01001806 * @codec: HD-audio codec
1807 * @name: vmaster control name
1808 * @tlv: TLV data (optional)
1809 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01001810 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01001811 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01001812 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01001813 *
1814 * Create a virtual master control with the given name. The TLV data
1815 * must be either NULL or a valid data.
1816 *
1817 * @slaves is a NULL-terminated array of strings, each of which is a
1818 * slave control name. All controls with these names are assigned to
1819 * the new virtual master control.
1820 *
1821 * This function returns zero if successful or a negative error code.
1822 */
Takashi Iwai18478e82012-03-09 17:51:10 +01001823int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01001824 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01001825 const char *suffix, bool init_slave_vol,
1826 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01001827{
1828 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01001829 int err;
1830
Takashi Iwai29e58532012-03-12 12:25:03 +01001831 if (ctl_ret)
1832 *ctl_ret = NULL;
1833
Takashi Iwai9322ca52012-02-03 14:28:01 +01001834 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01001835 if (err != 1) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001836 codec_dbg(codec, "No slave found for %s\n", name);
Takashi Iwai2f085542008-02-22 18:43:50 +01001837 return 0;
1838 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01001839 kctl = snd_ctl_make_virtual_master(name, tlv);
1840 if (!kctl)
1841 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001842 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001843 if (err < 0)
1844 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001845
Takashi Iwaie8750942014-06-30 14:02:39 +02001846 err = map_slaves(codec, slaves, suffix, add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01001847 if (err < 0)
1848 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01001849
1850 /* init with master mute & zero volume */
1851 put_kctl_with_value(kctl, 0);
Takashi Iwai485e3e02013-11-04 15:51:00 +01001852 if (init_slave_vol) {
1853 int step = 0;
Takashi Iwai18478e82012-03-09 17:51:10 +01001854 map_slaves(codec, slaves, suffix,
Takashi Iwai485e3e02013-11-04 15:51:00 +01001855 tlv ? init_slave_0dB : init_slave_unmute, &step);
1856 }
Takashi Iwai18478e82012-03-09 17:51:10 +01001857
Takashi Iwai29e58532012-03-12 12:25:03 +01001858 if (ctl_ret)
1859 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01001860 return 0;
1861}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001862EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001863
Takashi Iwaid2f344b2012-03-12 16:59:58 +01001864/*
1865 * mute-LED control using vmaster
1866 */
1867static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
1868 struct snd_ctl_elem_info *uinfo)
1869{
1870 static const char * const texts[] = {
David Henningssonc86c2d42013-01-03 14:12:29 +01001871 "On", "Off", "Follow Master"
Takashi Iwaid2f344b2012-03-12 16:59:58 +01001872 };
Takashi Iwaid2f344b2012-03-12 16:59:58 +01001873
Takashi Iwai3ff72212014-10-20 18:17:28 +02001874 return snd_ctl_enum_info(uinfo, 1, 3, texts);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01001875}
1876
1877static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
1878 struct snd_ctl_elem_value *ucontrol)
1879{
1880 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
1881 ucontrol->value.enumerated.item[0] = hook->mute_mode;
1882 return 0;
1883}
1884
1885static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
1886 struct snd_ctl_elem_value *ucontrol)
1887{
1888 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
1889 unsigned int old_mode = hook->mute_mode;
1890
1891 hook->mute_mode = ucontrol->value.enumerated.item[0];
1892 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
1893 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
1894 if (old_mode == hook->mute_mode)
1895 return 0;
1896 snd_hda_sync_vmaster_hook(hook);
1897 return 1;
1898}
1899
1900static struct snd_kcontrol_new vmaster_mute_mode = {
1901 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1902 .name = "Mute-LED Mode",
1903 .info = vmaster_mute_mode_info,
1904 .get = vmaster_mute_mode_get,
1905 .put = vmaster_mute_mode_put,
1906};
1907
Takashi Iwaiee52e562015-04-27 10:36:11 +02001908/* meta hook to call each driver's vmaster hook */
1909static void vmaster_hook(void *private_data, int enabled)
1910{
1911 struct hda_vmaster_mute_hook *hook = private_data;
1912
1913 if (hook->mute_mode != HDA_VMUTE_FOLLOW_MASTER)
1914 enabled = hook->mute_mode;
1915 hook->hook(hook->codec, enabled);
1916}
1917
Takashi Iwai95a962c2014-10-29 16:03:58 +01001918/**
1919 * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED
1920 * @codec: the HDA codec
1921 * @hook: the vmaster hook object
1922 * @expose_enum_ctl: flag to create an enum ctl
1923 *
1924 * Add a mute-LED hook with the given vmaster switch kctl.
1925 * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically
1926 * created and associated with the given hook.
Takashi Iwaid2f344b2012-03-12 16:59:58 +01001927 */
1928int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735c2012-03-13 07:55:10 +01001929 struct hda_vmaster_mute_hook *hook,
1930 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01001931{
1932 struct snd_kcontrol *kctl;
1933
1934 if (!hook->hook || !hook->sw_kctl)
1935 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01001936 hook->codec = codec;
1937 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaiee52e562015-04-27 10:36:11 +02001938 snd_ctl_add_vmaster_hook(hook->sw_kctl, vmaster_hook, hook);
Takashi Iwaif29735c2012-03-13 07:55:10 +01001939 if (!expose_enum_ctl)
1940 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01001941 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
1942 if (!kctl)
1943 return -ENOMEM;
1944 return snd_hda_ctl_add(codec, 0, kctl);
1945}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001946EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01001947
Takashi Iwai95a962c2014-10-29 16:03:58 +01001948/**
1949 * snd_hda_sync_vmaster_hook - Sync vmaster hook
1950 * @hook: the vmaster hook
1951 *
1952 * Call the hook with the current value for synchronization.
1953 * Should be called in init callback.
Takashi Iwaid2f344b2012-03-12 16:59:58 +01001954 */
1955void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
1956{
1957 if (!hook->hook || !hook->codec)
1958 return;
Takashi Iwai594813f2013-04-17 18:16:05 +02001959 /* don't call vmaster hook in the destructor since it might have
1960 * been already destroyed
1961 */
1962 if (hook->codec->bus->shutdown)
1963 return;
Takashi Iwaiee52e562015-04-27 10:36:11 +02001964 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01001965}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001966EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01001967
1968
Takashi Iwaid5191e52009-11-16 14:58:17 +01001969/**
1970 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001971 * @kcontrol: referred ctl element
1972 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01001973 *
1974 * The control element is supposed to have the private_value field
1975 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1976 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001977int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
1978 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979{
1980 int chs = get_amp_channels(kcontrol);
1981
1982 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1983 uinfo->count = chs == 3 ? 2 : 1;
1984 uinfo->value.integer.min = 0;
1985 uinfo->value.integer.max = 1;
1986 return 0;
1987}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001988EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
Takashi Iwaid5191e52009-11-16 14:58:17 +01001990/**
1991 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001992 * @kcontrol: ctl element
1993 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01001994 *
1995 * The control element is supposed to have the private_value field
1996 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1997 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001998int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
1999 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000{
2001 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2002 hda_nid_t nid = get_amp_nid(kcontrol);
2003 int chs = get_amp_channels(kcontrol);
2004 int dir = get_amp_direction(kcontrol);
2005 int idx = get_amp_index(kcontrol);
2006 long *valp = ucontrol->value.integer.value;
2007
2008 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002009 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002010 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002012 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002013 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 return 0;
2015}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002016EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
Takashi Iwaid5191e52009-11-16 14:58:17 +01002018/**
2019 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002020 * @kcontrol: ctl element
2021 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002022 *
2023 * The control element is supposed to have the private_value field
2024 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2025 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002026int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2027 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028{
2029 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2030 hda_nid_t nid = get_amp_nid(kcontrol);
2031 int chs = get_amp_channels(kcontrol);
2032 int dir = get_amp_direction(kcontrol);
2033 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 long *valp = ucontrol->value.integer.value;
2035 int change = 0;
2036
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002037 if (chs & 1) {
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01002038 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
2039 HDA_AMP_MUTE,
2040 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002041 valp++;
2042 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002043 if (chs & 2)
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01002044 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
2045 HDA_AMP_MUTE,
2046 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002047 hda_call_check_power_status(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 return change;
2049}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002050EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
2052/*
Takashi Iwai985be542005-11-02 18:26:49 +01002053 * bound volume controls
2054 *
2055 * bind multiple volumes (# indices, from 0)
2056 */
2057
2058#define AMP_VAL_IDX_SHIFT 19
2059#define AMP_VAL_IDX_MASK (0x0f<<19)
2060
Takashi Iwaid5191e52009-11-16 14:58:17 +01002061/**
2062 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002063 * @kcontrol: ctl element
2064 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002065 *
2066 * The control element is supposed to have the private_value field
2067 * set up via HDA_BIND_MUTE*() macros.
2068 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002069int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2070 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002071{
2072 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2073 unsigned long pval;
2074 int err;
2075
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002076 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002077 pval = kcontrol->private_value;
2078 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2079 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2080 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002081 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002082 return err;
2083}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002084EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002085
Takashi Iwaid5191e52009-11-16 14:58:17 +01002086/**
2087 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002088 * @kcontrol: ctl element
2089 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002090 *
2091 * The control element is supposed to have the private_value field
2092 * set up via HDA_BIND_MUTE*() macros.
2093 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002094int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2095 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002096{
2097 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2098 unsigned long pval;
2099 int i, indices, err = 0, change = 0;
2100
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002101 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002102 pval = kcontrol->private_value;
2103 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2104 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002105 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2106 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002107 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2108 if (err < 0)
2109 break;
2110 change |= err;
2111 }
2112 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002113 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002114 return err < 0 ? err : change;
2115}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002116EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002117
Takashi Iwaid5191e52009-11-16 14:58:17 +01002118/**
2119 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002120 * @kcontrol: referred ctl element
2121 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002122 *
2123 * The control element is supposed to have the private_value field
2124 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002125 */
2126int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2127 struct snd_ctl_elem_info *uinfo)
2128{
2129 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2130 struct hda_bind_ctls *c;
2131 int err;
2132
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002133 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002134 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002135 kcontrol->private_value = *c->values;
2136 err = c->ops->info(kcontrol, uinfo);
2137 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002138 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002139 return err;
2140}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002141EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002142
Takashi Iwaid5191e52009-11-16 14:58:17 +01002143/**
2144 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002145 * @kcontrol: ctl element
2146 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002147 *
2148 * The control element is supposed to have the private_value field
2149 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2150 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002151int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2152 struct snd_ctl_elem_value *ucontrol)
2153{
2154 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2155 struct hda_bind_ctls *c;
2156 int err;
2157
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002158 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002159 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002160 kcontrol->private_value = *c->values;
2161 err = c->ops->get(kcontrol, ucontrol);
2162 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002163 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002164 return err;
2165}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002166EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002167
Takashi Iwaid5191e52009-11-16 14:58:17 +01002168/**
2169 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002170 * @kcontrol: ctl element
2171 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002172 *
2173 * The control element is supposed to have the private_value field
2174 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2175 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002176int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2177 struct snd_ctl_elem_value *ucontrol)
2178{
2179 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2180 struct hda_bind_ctls *c;
2181 unsigned long *vals;
2182 int err = 0, change = 0;
2183
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002184 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002185 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002186 for (vals = c->values; *vals; vals++) {
2187 kcontrol->private_value = *vals;
2188 err = c->ops->put(kcontrol, ucontrol);
2189 if (err < 0)
2190 break;
2191 change |= err;
2192 }
2193 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002194 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002195 return err < 0 ? err : change;
2196}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002197EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002198
Takashi Iwaid5191e52009-11-16 14:58:17 +01002199/**
2200 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002201 * @kcontrol: ctl element
2202 * @op_flag: operation flag
2203 * @size: byte size of input TLV
2204 * @tlv: TLV data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002205 *
2206 * The control element is supposed to have the private_value field
2207 * set up via HDA_BIND_VOL() macro.
2208 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002209int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2210 unsigned int size, unsigned int __user *tlv)
2211{
2212 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2213 struct hda_bind_ctls *c;
2214 int err;
2215
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002216 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002217 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002218 kcontrol->private_value = *c->values;
2219 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2220 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002221 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002222 return err;
2223}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002224EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002225
2226struct hda_ctl_ops snd_hda_bind_vol = {
2227 .info = snd_hda_mixer_amp_volume_info,
2228 .get = snd_hda_mixer_amp_volume_get,
2229 .put = snd_hda_mixer_amp_volume_put,
2230 .tlv = snd_hda_mixer_amp_tlv
2231};
Takashi Iwai2698ea92013-12-18 07:45:52 +01002232EXPORT_SYMBOL_GPL(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002233
2234struct hda_ctl_ops snd_hda_bind_sw = {
2235 .info = snd_hda_mixer_amp_switch_info,
2236 .get = snd_hda_mixer_amp_switch_get,
2237 .put = snd_hda_mixer_amp_switch_put,
2238 .tlv = snd_hda_mixer_amp_tlv
2239};
Takashi Iwai2698ea92013-12-18 07:45:52 +01002240EXPORT_SYMBOL_GPL(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002241
2242/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 * SPDIF out controls
2244 */
2245
Takashi Iwai0ba21762007-04-16 11:29:14 +02002246static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2247 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248{
2249 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2250 uinfo->count = 1;
2251 return 0;
2252}
2253
Takashi Iwai0ba21762007-04-16 11:29:14 +02002254static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2255 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256{
2257 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2258 IEC958_AES0_NONAUDIO |
2259 IEC958_AES0_CON_EMPHASIS_5015 |
2260 IEC958_AES0_CON_NOT_COPYRIGHT;
2261 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2262 IEC958_AES1_CON_ORIGINAL;
2263 return 0;
2264}
2265
Takashi Iwai0ba21762007-04-16 11:29:14 +02002266static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2267 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268{
2269 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2270 IEC958_AES0_NONAUDIO |
2271 IEC958_AES0_PRO_EMPHASIS_5015;
2272 return 0;
2273}
2274
Takashi Iwai0ba21762007-04-16 11:29:14 +02002275static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2276 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277{
2278 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002279 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002280 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002282 mutex_lock(&codec->spdif_mutex);
2283 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c9359762011-06-01 11:14:17 -06002284 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2285 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2286 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2287 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002288 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289
2290 return 0;
2291}
2292
2293/* convert from SPDIF status bits to HDA SPDIF bits
2294 * bit 0 (DigEn) is always set zero (to be filled later)
2295 */
2296static unsigned short convert_from_spdif_status(unsigned int sbits)
2297{
2298 unsigned short val = 0;
2299
2300 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002301 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002303 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002305 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2306 IEC958_AES0_PRO_EMPHASIS_5015)
2307 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002309 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2310 IEC958_AES0_CON_EMPHASIS_5015)
2311 val |= AC_DIG1_EMPHASIS;
2312 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2313 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002315 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2317 }
2318 return val;
2319}
2320
2321/* convert to SPDIF status bits from HDA SPDIF bits
2322 */
2323static unsigned int convert_to_spdif_status(unsigned short val)
2324{
2325 unsigned int sbits = 0;
2326
Takashi Iwai0ba21762007-04-16 11:29:14 +02002327 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002329 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 sbits |= IEC958_AES0_PROFESSIONAL;
2331 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwaia686fd12013-03-20 15:42:00 +01002332 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2334 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002335 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002337 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002339 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2341 sbits |= val & (0x7f << 8);
2342 }
2343 return sbits;
2344}
2345
Takashi Iwai2f728532008-09-25 16:32:41 +02002346/* set digital convert verbs both for the given NID and its slaves */
2347static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaia551d912015-02-26 12:34:49 +01002348 int mask, int val)
Takashi Iwai2f728532008-09-25 16:32:41 +02002349{
Takashi Iwaidda14412011-05-02 11:29:30 +02002350 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02002351
Takashi Iwaia551d912015-02-26 12:34:49 +01002352 snd_hdac_regmap_update(&codec->core, nid, AC_VERB_SET_DIGI_CONVERT_1,
2353 mask, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002354 d = codec->slave_dig_outs;
2355 if (!d)
2356 return;
2357 for (; *d; d++)
Takashi Iwai7d4b5e92015-04-16 15:14:53 +02002358 snd_hdac_regmap_update(&codec->core, *d,
Takashi Iwaia551d912015-02-26 12:34:49 +01002359 AC_VERB_SET_DIGI_CONVERT_1, mask, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002360}
2361
2362static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2363 int dig1, int dig2)
2364{
Takashi Iwaia551d912015-02-26 12:34:49 +01002365 unsigned int mask = 0;
2366 unsigned int val = 0;
2367
2368 if (dig1 != -1) {
2369 mask |= 0xff;
2370 val = dig1;
2371 }
2372 if (dig2 != -1) {
2373 mask |= 0xff00;
2374 val |= dig2 << 8;
2375 }
2376 set_dig_out(codec, nid, mask, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002377}
2378
Takashi Iwai0ba21762007-04-16 11:29:14 +02002379static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2380 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381{
2382 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002383 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002384 struct hda_spdif_out *spdif;
2385 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 unsigned short val;
2387 int change;
2388
Ingo Molnar62932df2006-01-16 16:34:20 +01002389 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002390 spdif = snd_array_elem(&codec->spdif_out, idx);
2391 nid = spdif->nid;
Stephen Warren7c9359762011-06-01 11:14:17 -06002392 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2394 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2395 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c9359762011-06-01 11:14:17 -06002396 val = convert_from_spdif_status(spdif->status);
2397 val |= spdif->ctls & 1;
2398 change = spdif->ctls != val;
2399 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06002400 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02002401 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01002402 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 return change;
2404}
2405
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002406#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
Takashi Iwai0ba21762007-04-16 11:29:14 +02002408static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2409 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410{
2411 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002412 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002413 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002415 mutex_lock(&codec->spdif_mutex);
2416 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c9359762011-06-01 11:14:17 -06002417 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002418 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 return 0;
2420}
2421
Stephen Warren74b654c2011-06-01 11:14:18 -06002422static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
2423 int dig1, int dig2)
2424{
2425 set_dig_out_convert(codec, nid, dig1, dig2);
2426 /* unmute amp switch (if any) */
2427 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
2428 (dig1 & AC_DIG1_ENABLE))
2429 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2430 HDA_AMP_MUTE, 0);
2431}
2432
Takashi Iwai0ba21762007-04-16 11:29:14 +02002433static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2434 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435{
2436 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c9359762011-06-01 11:14:17 -06002437 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002438 struct hda_spdif_out *spdif;
2439 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 unsigned short val;
2441 int change;
2442
Ingo Molnar62932df2006-01-16 16:34:20 +01002443 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002444 spdif = snd_array_elem(&codec->spdif_out, idx);
2445 nid = spdif->nid;
Stephen Warren7c9359762011-06-01 11:14:17 -06002446 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02002448 val |= AC_DIG1_ENABLE;
Stephen Warren7c9359762011-06-01 11:14:17 -06002449 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06002450 spdif->ctls = val;
2451 if (change && nid != (u16)-1)
2452 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01002453 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 return change;
2455}
2456
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002457static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 {
2459 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2460 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002461 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 .info = snd_hda_spdif_mask_info,
2463 .get = snd_hda_spdif_cmask_get,
2464 },
2465 {
2466 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2467 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002468 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 .info = snd_hda_spdif_mask_info,
2470 .get = snd_hda_spdif_pmask_get,
2471 },
2472 {
2473 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002474 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 .info = snd_hda_spdif_mask_info,
2476 .get = snd_hda_spdif_default_get,
2477 .put = snd_hda_spdif_default_put,
2478 },
2479 {
2480 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002481 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 .info = snd_hda_spdif_out_switch_info,
2483 .get = snd_hda_spdif_out_switch_get,
2484 .put = snd_hda_spdif_out_switch_put,
2485 },
2486 { } /* end */
2487};
2488
2489/**
Takashi Iwaidcda5802012-10-12 17:24:51 +02002490 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 * @codec: the HDA codec
Takashi Iwaidcda5802012-10-12 17:24:51 +02002492 * @associated_nid: NID that new ctls associated with
2493 * @cvt_nid: converter NID
2494 * @type: HDA_PCM_TYPE_*
2495 * Creates controls related with the digital output.
2496 * Called from each patch supporting the digital out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 *
2498 * Returns 0 if successful, or a negative error code.
2499 */
Takashi Iwaidcda5802012-10-12 17:24:51 +02002500int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
2501 hda_nid_t associated_nid,
2502 hda_nid_t cvt_nid,
2503 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504{
2505 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002506 struct snd_kcontrol *kctl;
2507 struct snd_kcontrol_new *dig_mix;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002508 int idx = 0;
Takashi Iwaia551d912015-02-26 12:34:49 +01002509 int val = 0;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002510 const int spdif_index = 16;
Stephen Warren7c9359762011-06-01 11:14:17 -06002511 struct hda_spdif_out *spdif;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002512 struct hda_bus *bus = codec->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002514 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02002515 type == HDA_PCM_TYPE_SPDIF) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002516 idx = spdif_index;
2517 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02002518 type == HDA_PCM_TYPE_HDMI) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002519 /* suppose a single SPDIF device */
2520 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2521 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
2522 if (!kctl)
2523 break;
2524 kctl->id.index = spdif_index;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002525 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002526 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002527 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002528 if (!bus->primary_dig_out_type)
2529 bus->primary_dig_out_type = type;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002530
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002531 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
Takashi Iwai1afe2062010-12-23 10:17:52 +01002532 if (idx < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002533 codec_err(codec, "too many IEC958 outputs\n");
Takashi Iwai09f99702008-02-04 12:31:13 +01002534 return -EBUSY;
2535 }
Stephen Warren7c9359762011-06-01 11:14:17 -06002536 spdif = snd_array_new(&codec->spdif_out);
Mengdong Lin25336e82013-03-07 14:10:25 -05002537 if (!spdif)
2538 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2540 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01002541 if (!kctl)
2542 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01002543 kctl->id.index = idx;
Stephen Warren7c9359762011-06-01 11:14:17 -06002544 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06002545 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002546 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 return err;
2548 }
Stephen Warren74b654c2011-06-01 11:14:18 -06002549 spdif->nid = cvt_nid;
Takashi Iwaia551d912015-02-26 12:34:49 +01002550 snd_hdac_regmap_read(&codec->core, cvt_nid,
2551 AC_VERB_GET_DIGI_CONVERT_1, &val);
2552 spdif->ctls = val;
Stephen Warren7c9359762011-06-01 11:14:17 -06002553 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 return 0;
2555}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002556EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557
Takashi Iwai95a962c2014-10-29 16:03:58 +01002558/**
2559 * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID
2560 * @codec: the HDA codec
2561 * @nid: widget NID
2562 *
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002563 * call within spdif_mutex lock
2564 */
Stephen Warren7c9359762011-06-01 11:14:17 -06002565struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
2566 hda_nid_t nid)
2567{
2568 int i;
2569 for (i = 0; i < codec->spdif_out.used; i++) {
2570 struct hda_spdif_out *spdif =
2571 snd_array_elem(&codec->spdif_out, i);
2572 if (spdif->nid == nid)
2573 return spdif;
2574 }
2575 return NULL;
2576}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002577EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid);
Stephen Warren7c9359762011-06-01 11:14:17 -06002578
Takashi Iwai95a962c2014-10-29 16:03:58 +01002579/**
2580 * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl
2581 * @codec: the HDA codec
2582 * @idx: the SPDIF ctl index
2583 *
2584 * Unassign the widget from the given SPDIF control.
2585 */
Stephen Warren74b654c2011-06-01 11:14:18 -06002586void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
2587{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002588 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06002589
2590 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002591 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06002592 spdif->nid = (u16)-1;
2593 mutex_unlock(&codec->spdif_mutex);
2594}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002595EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign);
Stephen Warren74b654c2011-06-01 11:14:18 -06002596
Takashi Iwai95a962c2014-10-29 16:03:58 +01002597/**
2598 * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID
2599 * @codec: the HDA codec
2600 * @idx: the SPDIF ctl idx
2601 * @nid: widget NID
2602 *
2603 * Assign the widget to the SPDIF control with the given index.
2604 */
Stephen Warren74b654c2011-06-01 11:14:18 -06002605void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
2606{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002607 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06002608 unsigned short val;
2609
2610 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002611 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06002612 if (spdif->nid != nid) {
2613 spdif->nid = nid;
2614 val = spdif->ctls;
2615 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
2616 }
2617 mutex_unlock(&codec->spdif_mutex);
2618}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002619EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign);
Stephen Warren74b654c2011-06-01 11:14:18 -06002620
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621/*
Takashi Iwai9a081602008-02-12 18:37:26 +01002622 * SPDIF sharing with analog output
2623 */
2624static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2625 struct snd_ctl_elem_value *ucontrol)
2626{
2627 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2628 ucontrol->value.integer.value[0] = mout->share_spdif;
2629 return 0;
2630}
2631
2632static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2633 struct snd_ctl_elem_value *ucontrol)
2634{
2635 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2636 mout->share_spdif = !!ucontrol->value.integer.value[0];
2637 return 0;
2638}
2639
2640static struct snd_kcontrol_new spdif_share_sw = {
2641 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2642 .name = "IEC958 Default PCM Playback Switch",
2643 .info = snd_ctl_boolean_mono_info,
2644 .get = spdif_share_sw_get,
2645 .put = spdif_share_sw_put,
2646};
2647
Takashi Iwaid5191e52009-11-16 14:58:17 +01002648/**
2649 * snd_hda_create_spdif_share_sw - create Default PCM switch
2650 * @codec: the HDA codec
2651 * @mout: multi-out instance
2652 */
Takashi Iwai9a081602008-02-12 18:37:26 +01002653int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2654 struct hda_multi_out *mout)
2655{
Mengdong Lin4c7a5482013-03-07 14:11:05 -05002656 struct snd_kcontrol *kctl;
2657
Takashi Iwai9a081602008-02-12 18:37:26 +01002658 if (!mout->dig_out_nid)
2659 return 0;
Mengdong Lin4c7a5482013-03-07 14:11:05 -05002660
2661 kctl = snd_ctl_new1(&spdif_share_sw, mout);
2662 if (!kctl)
2663 return -ENOMEM;
Takashi Iwai9a081602008-02-12 18:37:26 +01002664 /* ATTENTION: here mout is passed as private_data, instead of codec */
Mengdong Lin4c7a5482013-03-07 14:11:05 -05002665 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
Takashi Iwai9a081602008-02-12 18:37:26 +01002666}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002667EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01002668
2669/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 * SPDIF input
2671 */
2672
2673#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2674
Takashi Iwai0ba21762007-04-16 11:29:14 +02002675static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
2676 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677{
2678 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2679
2680 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
2681 return 0;
2682}
2683
Takashi Iwai0ba21762007-04-16 11:29:14 +02002684static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
2685 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686{
2687 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2688 hda_nid_t nid = kcontrol->private_value;
2689 unsigned int val = !!ucontrol->value.integer.value[0];
2690 int change;
2691
Ingo Molnar62932df2006-01-16 16:34:20 +01002692 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002694 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 codec->spdif_in_enable = val;
Takashi Iwaia551d912015-02-26 12:34:49 +01002696 snd_hdac_regmap_write(&codec->core, nid,
2697 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002699 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 return change;
2701}
2702
Takashi Iwai0ba21762007-04-16 11:29:14 +02002703static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
2704 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705{
2706 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2707 hda_nid_t nid = kcontrol->private_value;
Takashi Iwaia551d912015-02-26 12:34:49 +01002708 unsigned int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 unsigned int sbits;
2710
Takashi Iwaia551d912015-02-26 12:34:49 +01002711 snd_hdac_regmap_read(&codec->core, nid,
2712 AC_VERB_GET_DIGI_CONVERT_1, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 sbits = convert_to_spdif_status(val);
2714 ucontrol->value.iec958.status[0] = sbits;
2715 ucontrol->value.iec958.status[1] = sbits >> 8;
2716 ucontrol->value.iec958.status[2] = sbits >> 16;
2717 ucontrol->value.iec958.status[3] = sbits >> 24;
2718 return 0;
2719}
2720
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002721static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 {
2723 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002724 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 .info = snd_hda_spdif_in_switch_info,
2726 .get = snd_hda_spdif_in_switch_get,
2727 .put = snd_hda_spdif_in_switch_put,
2728 },
2729 {
2730 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2731 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002732 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 .info = snd_hda_spdif_mask_info,
2734 .get = snd_hda_spdif_in_status_get,
2735 },
2736 { } /* end */
2737};
2738
2739/**
2740 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
2741 * @codec: the HDA codec
2742 * @nid: audio in widget NID
2743 *
2744 * Creates controls related with the SPDIF input.
2745 * Called from each patch supporting the SPDIF in.
2746 *
2747 * Returns 0 if successful, or a negative error code.
2748 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002749int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750{
2751 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002752 struct snd_kcontrol *kctl;
2753 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01002754 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755
Takashi Iwaidcda5802012-10-12 17:24:51 +02002756 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01002757 if (idx < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002758 codec_err(codec, "too many IEC958 inputs\n");
Takashi Iwai09f99702008-02-04 12:31:13 +01002759 return -EBUSY;
2760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
2762 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01002763 if (!kctl)
2764 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002766 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002767 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 return err;
2769 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002770 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01002771 snd_hda_codec_read(codec, nid, 0,
2772 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02002773 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 return 0;
2775}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002776EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777
Takashi Iwai95a962c2014-10-29 16:03:58 +01002778/**
2779 * snd_hda_codec_set_power_to_all - Set the power state to all widgets
2780 * @codec: the HDA codec
2781 * @fg: function group (not used now)
2782 * @power_state: the power state to set (AC_PWRST_*)
2783 *
2784 * Set the given power state to all widgets that have the power control.
2785 * If the codec has power_filter set, it evaluates the power state and
2786 * filter out if it's unchanged as D3.
2787 */
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002788void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
Takashi Iwai9419ab62013-01-24 17:23:35 +01002789 unsigned int power_state)
Takashi Iwai54d17402005-11-21 16:33:22 +01002790{
Takashi Iwai7639a062015-03-03 10:07:24 +01002791 hda_nid_t nid;
Takashi Iwai54d17402005-11-21 16:33:22 +01002792
Takashi Iwai7639a062015-03-03 10:07:24 +01002793 for_each_hda_codec_node(nid, codec) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01002794 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9419ab62013-01-24 17:23:35 +01002795 unsigned int state = power_state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002796 if (!(wcaps & AC_WCAP_POWER))
2797 continue;
Takashi Iwai9419ab62013-01-24 17:23:35 +01002798 if (codec->power_filter) {
2799 state = codec->power_filter(codec, nid, power_state);
2800 if (state != power_state && power_state == AC_PWRST_D3)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002801 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02002802 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002803 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai9419ab62013-01-24 17:23:35 +01002804 state);
Takashi Iwai54d17402005-11-21 16:33:22 +01002805 }
Takashi Iwai54d17402005-11-21 16:33:22 +01002806}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002807EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002808
2809/*
Takashi Iwai432c6412012-08-28 09:59:20 -07002810 * wait until the state is reached, returns the current state
2811 */
2812static unsigned int hda_sync_power_state(struct hda_codec *codec,
2813 hda_nid_t fg,
2814 unsigned int power_state)
2815{
2816 unsigned long end_time = jiffies + msecs_to_jiffies(500);
2817 unsigned int state, actual_state;
2818
2819 for (;;) {
2820 state = snd_hda_codec_read(codec, fg, 0,
2821 AC_VERB_GET_POWER_STATE, 0);
2822 if (state & AC_PWRST_ERROR)
2823 break;
2824 actual_state = (state >> 4) & 0x0f;
2825 if (actual_state == power_state)
2826 break;
2827 if (time_after_eq(jiffies, end_time))
2828 break;
2829 /* wait until the codec reachs to the target state */
2830 msleep(1);
2831 }
2832 return state;
2833}
2834
Takashi Iwai95a962c2014-10-29 16:03:58 +01002835/**
2836 * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD
2837 * @codec: the HDA codec
2838 * @nid: widget NID
2839 * @power_state: power state to evalue
2840 *
2841 * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set.
2842 * This can be used a codec power_filter callback.
2843 */
Takashi Iwaiba615b82013-03-13 14:47:21 +01002844unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
2845 hda_nid_t nid,
2846 unsigned int power_state)
Takashi Iwai9419ab62013-01-24 17:23:35 +01002847{
Takashi Iwai7639a062015-03-03 10:07:24 +01002848 if (nid == codec->core.afg || nid == codec->core.mfg)
Takashi Iwaidfc6e462014-01-13 16:09:57 +01002849 return power_state;
Takashi Iwai9419ab62013-01-24 17:23:35 +01002850 if (power_state == AC_PWRST_D3 &&
2851 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
2852 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
2853 int eapd = snd_hda_codec_read(codec, nid, 0,
2854 AC_VERB_GET_EAPD_BTLENABLE, 0);
2855 if (eapd & 0x02)
2856 return AC_PWRST_D0;
2857 }
2858 return power_state;
2859}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002860EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter);
Takashi Iwai9419ab62013-01-24 17:23:35 +01002861
Takashi Iwai432c6412012-08-28 09:59:20 -07002862/*
Takashi Iwai08fa20a2012-08-31 07:46:56 -07002863 * set power state of the codec, and return the power state
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002864 */
Takashi Iwaid8193872012-08-31 07:54:38 -07002865static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwai08fa20a2012-08-31 07:46:56 -07002866 unsigned int power_state)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002867{
Takashi Iwai7639a062015-03-03 10:07:24 +01002868 hda_nid_t fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
Wang Xingchao09617ce2012-06-08 10:26:08 +08002869 int count;
2870 unsigned int state;
Takashi Iwai63e51fd2013-06-06 14:20:19 +02002871 int flags = 0;
Wang Xingchao09617ce2012-06-08 10:26:08 +08002872
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002873 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08002874 if (power_state == AC_PWRST_D3) {
Mengdong Lin7f132922013-11-29 01:48:45 -05002875 if (codec->depop_delay < 0)
Takashi Iwai7639a062015-03-03 10:07:24 +01002876 msleep(codec_has_epss(codec) ? 10 : 100);
Mengdong Lin7f132922013-11-29 01:48:45 -05002877 else if (codec->depop_delay > 0)
2878 msleep(codec->depop_delay);
Takashi Iwai63e51fd2013-06-06 14:20:19 +02002879 flags = HDA_RW_NO_RESPONSE_FALLBACK;
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08002880 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08002881
2882 /* repeat power states setting at most 10 times*/
2883 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07002884 if (codec->patch_ops.set_power_state)
2885 codec->patch_ops.set_power_state(codec, fg,
2886 power_state);
2887 else {
Takashi Iwaidfc6e462014-01-13 16:09:57 +01002888 state = power_state;
2889 if (codec->power_filter)
2890 state = codec->power_filter(codec, fg, state);
2891 if (state == power_state || power_state != AC_PWRST_D3)
2892 snd_hda_codec_read(codec, fg, flags,
2893 AC_VERB_SET_POWER_STATE,
2894 state);
Takashi Iwai9419ab62013-01-24 17:23:35 +01002895 snd_hda_codec_set_power_to_all(codec, fg, power_state);
Takashi Iwai432c6412012-08-28 09:59:20 -07002896 }
2897 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08002898 if (!(state & AC_PWRST_ERROR))
2899 break;
2900 }
Mengdong Linb8dfc4622012-08-23 17:32:30 +08002901
Takashi Iwai08fa20a2012-08-31 07:46:56 -07002902 return state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002903}
Takashi Iwai54d17402005-11-21 16:33:22 +01002904
Takashi Iwaib9c590b2013-01-24 17:27:32 +01002905/* sync power states of all widgets;
2906 * this is called at the end of codec parsing
2907 */
2908static void sync_power_up_states(struct hda_codec *codec)
2909{
Takashi Iwai7639a062015-03-03 10:07:24 +01002910 hda_nid_t nid;
Takashi Iwaib9c590b2013-01-24 17:27:32 +01002911
Takashi Iwaiba615b82013-03-13 14:47:21 +01002912 /* don't care if no filter is used */
2913 if (!codec->power_filter)
Takashi Iwaib9c590b2013-01-24 17:27:32 +01002914 return;
2915
Takashi Iwai7639a062015-03-03 10:07:24 +01002916 for_each_hda_codec_node(nid, codec) {
Takashi Iwaib9c590b2013-01-24 17:27:32 +01002917 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9040d102013-01-24 17:47:17 +01002918 unsigned int target;
Takashi Iwaib9c590b2013-01-24 17:27:32 +01002919 if (!(wcaps & AC_WCAP_POWER))
2920 continue;
2921 target = codec->power_filter(codec, nid, AC_PWRST_D0);
2922 if (target == AC_PWRST_D0)
2923 continue;
Takashi Iwai9040d102013-01-24 17:47:17 +01002924 if (!snd_hda_check_power_state(codec, nid, target))
Takashi Iwaib9c590b2013-01-24 17:27:32 +01002925 snd_hda_codec_write(codec, nid, 0,
2926 AC_VERB_SET_POWER_STATE, target);
2927 }
2928}
2929
Takashi Iwai648a8d22014-02-25 10:38:13 +01002930#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai11aeff02008-07-30 15:01:46 +02002931/* execute additional init verbs */
2932static void hda_exec_init_verbs(struct hda_codec *codec)
2933{
2934 if (codec->init_verbs.list)
2935 snd_hda_sequence_write(codec, codec->init_verbs.list);
2936}
2937#else
2938static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
2939#endif
2940
Takashi Iwai2a439522011-07-26 09:52:50 +02002941#ifdef CONFIG_PM
Takashi Iwaicc72da72015-02-19 16:00:22 +01002942/* update the power on/off account with the current jiffies */
2943static void update_power_acct(struct hda_codec *codec, bool on)
2944{
2945 unsigned long delta = jiffies - codec->power_jiffies;
2946
2947 if (on)
2948 codec->power_on_acct += delta;
2949 else
2950 codec->power_off_acct += delta;
2951 codec->power_jiffies += delta;
2952}
2953
2954void snd_hda_update_power_acct(struct hda_codec *codec)
2955{
2956 update_power_acct(codec, hda_codec_is_power_on(codec));
2957}
2958
Takashi Iwaicb53c622007-08-10 17:21:45 +02002959/*
2960 * call suspend and power-down; used both from PM and power-save
Takashi Iwai08fa20a2012-08-31 07:46:56 -07002961 * this function returns the power state in the end
Takashi Iwaicb53c622007-08-10 17:21:45 +02002962 */
Takashi Iwaicc72da72015-02-19 16:00:22 +01002963static unsigned int hda_call_codec_suspend(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02002964{
Takashi Iwai08fa20a2012-08-31 07:46:56 -07002965 unsigned int state;
2966
Takashi Iwai7639a062015-03-03 10:07:24 +01002967 atomic_inc(&codec->core.in_pm);
Takashi Iwai989c3182012-11-19 14:14:58 +01002968
Takashi Iwaicb53c622007-08-10 17:21:45 +02002969 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002970 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02002971 hda_cleanup_all_streams(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07002972 state = hda_set_power_state(codec, AC_PWRST_D3);
Takashi Iwaicc72da72015-02-19 16:00:22 +01002973 update_power_acct(codec, true);
Takashi Iwai7639a062015-03-03 10:07:24 +01002974 atomic_dec(&codec->core.in_pm);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07002975 return state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002976}
2977
2978/*
2979 * kick up codec; used both from PM and power-save
2980 */
2981static void hda_call_codec_resume(struct hda_codec *codec)
2982{
Takashi Iwai7639a062015-03-03 10:07:24 +01002983 atomic_inc(&codec->core.in_pm);
Takashi Iwai989c3182012-11-19 14:14:58 +01002984
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01002985 if (codec->core.regmap)
2986 regcache_mark_dirty(codec->core.regmap);
2987
Takashi Iwaicc72da72015-02-19 16:00:22 +01002988 codec->power_jiffies = jiffies;
Takashi Iwaicc72da72015-02-19 16:00:22 +01002989
Takashi Iwaid8193872012-08-31 07:54:38 -07002990 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02002991 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02002992 hda_exec_init_verbs(codec);
Takashi Iwai31614bb2013-01-23 15:58:40 +01002993 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002994 if (codec->patch_ops.resume)
2995 codec->patch_ops.resume(codec);
2996 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02002997 if (codec->patch_ops.init)
2998 codec->patch_ops.init(codec);
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01002999 if (codec->core.regmap)
3000 regcache_sync(codec->core.regmap);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003001 }
David Henningsson26a6cb62012-10-09 15:04:21 +02003002
3003 if (codec->jackpoll_interval)
3004 hda_jackpoll_work(&codec->jackpoll_work.work);
Takashi Iwai31614bb2013-01-23 15:58:40 +01003005 else
David Henningsson26a6cb62012-10-09 15:04:21 +02003006 snd_hda_jack_report_sync(codec);
Takashi Iwai7639a062015-03-03 10:07:24 +01003007 atomic_dec(&codec->core.in_pm);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003008}
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003009
Takashi Iwaicc72da72015-02-19 16:00:22 +01003010static int hda_codec_runtime_suspend(struct device *dev)
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003011{
3012 struct hda_codec *codec = dev_to_hda_codec(dev);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003013 struct hda_pcm *pcm;
Takashi Iwaicc72da72015-02-19 16:00:22 +01003014 unsigned int state;
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003015
3016 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003017 list_for_each_entry(pcm, &codec->pcm_list_head, list)
3018 snd_pcm_suspend_all(pcm->pcm);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003019 state = hda_call_codec_suspend(codec);
Takashi Iwai7639a062015-03-03 10:07:24 +01003020 if (codec_has_clkstop(codec) && codec_has_epss(codec) &&
3021 (state & AC_PWRST_CLK_STOP_OK))
3022 snd_hdac_codec_link_down(&codec->core);
Mengdong Lina5e7e072015-04-29 17:43:20 +08003023 snd_hdac_link_power(&codec->core, false);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003024 return 0;
3025}
3026
Takashi Iwaicc72da72015-02-19 16:00:22 +01003027static int hda_codec_runtime_resume(struct device *dev)
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003028{
Takashi Iwai55ed9cd2015-02-19 17:35:32 +01003029 struct hda_codec *codec = dev_to_hda_codec(dev);
3030
Mengdong Lina5e7e072015-04-29 17:43:20 +08003031 snd_hdac_link_power(&codec->core, true);
Takashi Iwai7639a062015-03-03 10:07:24 +01003032 snd_hdac_codec_link_up(&codec->core);
Takashi Iwai55ed9cd2015-02-19 17:35:32 +01003033 hda_call_codec_resume(codec);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003034 pm_runtime_mark_last_busy(dev);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003035 return 0;
3036}
Takashi Iwai2a439522011-07-26 09:52:50 +02003037#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003038
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003039/* referred in hda_bind.c */
3040const struct dev_pm_ops hda_codec_driver_pm = {
Takashi Iwaicc72da72015-02-19 16:00:22 +01003041 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
3042 pm_runtime_force_resume)
3043 SET_RUNTIME_PM_OPS(hda_codec_runtime_suspend, hda_codec_runtime_resume,
3044 NULL)
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003045};
Takashi Iwai54d17402005-11-21 16:33:22 +01003046
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003047/*
3048 * add standard channel maps if not specified
3049 */
3050static int add_std_chmaps(struct hda_codec *codec)
3051{
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003052 struct hda_pcm *pcm;
3053 int str, err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003054
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003055 list_for_each_entry(pcm, &codec->pcm_list_head, list) {
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003056 for (str = 0; str < 2; str++) {
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003057 struct hda_pcm_stream *hinfo = &pcm->stream[str];
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003058 struct snd_pcm_chmap *chmap;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003059 const struct snd_pcm_chmap_elem *elem;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003060
Takashi Iwaib25cf302015-08-26 14:21:14 +02003061 if (!pcm->pcm || pcm->own_chmap || !hinfo->substreams)
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003062 continue;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003063 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003064 err = snd_pcm_add_chmap_ctls(pcm->pcm, str, elem,
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003065 hinfo->channels_max,
3066 0, &chmap);
3067 if (err < 0)
3068 return err;
3069 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
3070 }
3071 }
3072 return 0;
3073}
3074
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003075/* default channel maps for 2.1 speakers;
3076 * since HD-audio supports only stereo, odd number channels are omitted
3077 */
3078const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
3079 { .channels = 2,
3080 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
3081 { .channels = 4,
3082 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
3083 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
3084 { }
3085};
3086EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
3087
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003088int snd_hda_codec_build_controls(struct hda_codec *codec)
3089{
3090 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003091 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003092 /* continue to initialize... */
3093 if (codec->patch_ops.init)
3094 err = codec->patch_ops.init(codec);
3095 if (!err && codec->patch_ops.build_controls)
3096 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003097 if (err < 0)
3098 return err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003099
3100 /* we create chmaps here instead of build_pcms */
3101 err = add_std_chmaps(codec);
3102 if (err < 0)
3103 return err;
3104
David Henningsson26a6cb62012-10-09 15:04:21 +02003105 if (codec->jackpoll_interval)
3106 hda_jackpoll_work(&codec->jackpoll_work.work);
3107 else
3108 snd_hda_jack_report_sync(codec); /* call at the last init point */
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003109 sync_power_up_states(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 return 0;
3111}
3112
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 * PCM stuff
3115 */
3116static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3117 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003118 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119{
3120 return 0;
3121}
3122
3123static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3124 struct hda_codec *codec,
3125 unsigned int stream_tag,
3126 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003127 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128{
3129 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3130 return 0;
3131}
3132
3133static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3134 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003135 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136{
Takashi Iwai888afa12008-03-18 09:57:50 +01003137 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 return 0;
3139}
3140
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003141static int set_pcm_default_values(struct hda_codec *codec,
3142 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003144 int err;
3145
Takashi Iwai0ba21762007-04-16 11:29:14 +02003146 /* query support PCM information from the given NID */
3147 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003148 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02003149 info->rates ? NULL : &info->rates,
3150 info->formats ? NULL : &info->formats,
3151 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003152 if (err < 0)
3153 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 }
3155 if (info->ops.open == NULL)
3156 info->ops.open = hda_pcm_default_open_close;
3157 if (info->ops.close == NULL)
3158 info->ops.close = hda_pcm_default_open_close;
3159 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003160 if (snd_BUG_ON(!info->nid))
3161 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 info->ops.prepare = hda_pcm_default_prepare;
3163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003165 if (snd_BUG_ON(!info->nid))
3166 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 info->ops.cleanup = hda_pcm_default_cleanup;
3168 }
3169 return 0;
3170}
3171
Takashi Iwaieb541332010-08-06 13:48:11 +02003172/*
3173 * codec prepare/cleanup entries
3174 */
Takashi Iwai95a962c2014-10-29 16:03:58 +01003175/**
3176 * snd_hda_codec_prepare - Prepare a stream
3177 * @codec: the HDA codec
3178 * @hinfo: PCM information
3179 * @stream: stream tag to assign
3180 * @format: format id to assign
3181 * @substream: PCM substream to assign
3182 *
3183 * Calls the prepare callback set by the codec with the given arguments.
3184 * Clean up the inactive streams when successful.
3185 */
Takashi Iwaieb541332010-08-06 13:48:11 +02003186int snd_hda_codec_prepare(struct hda_codec *codec,
3187 struct hda_pcm_stream *hinfo,
3188 unsigned int stream,
3189 unsigned int format,
3190 struct snd_pcm_substream *substream)
3191{
3192 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003193 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwai61ca4102015-02-27 17:57:55 +01003194 if (hinfo->ops.prepare)
3195 ret = hinfo->ops.prepare(hinfo, codec, stream, format,
3196 substream);
3197 else
3198 ret = -ENODEV;
Takashi Iwaieb541332010-08-06 13:48:11 +02003199 if (ret >= 0)
3200 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003201 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003202 return ret;
3203}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003204EXPORT_SYMBOL_GPL(snd_hda_codec_prepare);
Takashi Iwaieb541332010-08-06 13:48:11 +02003205
Takashi Iwai95a962c2014-10-29 16:03:58 +01003206/**
3207 * snd_hda_codec_cleanup - Prepare a stream
3208 * @codec: the HDA codec
3209 * @hinfo: PCM information
3210 * @substream: PCM substream
3211 *
3212 * Calls the cleanup callback set by the codec with the given arguments.
3213 */
Takashi Iwaieb541332010-08-06 13:48:11 +02003214void snd_hda_codec_cleanup(struct hda_codec *codec,
3215 struct hda_pcm_stream *hinfo,
3216 struct snd_pcm_substream *substream)
3217{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003218 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwai61ca4102015-02-27 17:57:55 +01003219 if (hinfo->ops.cleanup)
3220 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003221 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003222}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003223EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup);
Takashi Iwaieb541332010-08-06 13:48:11 +02003224
Takashi Iwaid5191e52009-11-16 14:58:17 +01003225/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01003226const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3227 "Audio", "SPDIF", "HDMI", "Modem"
3228};
3229
Takashi Iwai176d5332008-07-30 15:01:44 +02003230/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003231 * get the empty PCM device number to assign
3232 */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003233static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003234{
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003235 /* audio device indices; not linear to keep compatibility */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003236 /* assigned to static slots up to dev#10; if more needed, assign
3237 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
3238 */
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003239 static int audio_idx[HDA_PCM_NTYPES][5] = {
3240 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3241 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01003242 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003243 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003244 };
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003245 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003246
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003247 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003248 dev_err(bus->card->dev, "Invalid PCM type %d\n", type);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003249 return -EINVAL;
3250 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003251
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003252 for (i = 0; audio_idx[type][i] >= 0; i++) {
3253#ifndef CONFIG_SND_DYNAMIC_MINORS
3254 if (audio_idx[type][i] >= 8)
3255 break;
3256#endif
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003257 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3258 return audio_idx[type][i];
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003259 }
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003260
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003261#ifdef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai01b65bf2011-11-24 14:31:46 +01003262 /* non-fixed slots starting from 10 */
3263 for (i = 10; i < 32; i++) {
3264 if (!test_and_set_bit(i, bus->pcm_dev_bits))
3265 return i;
3266 }
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003267#endif
Takashi Iwai01b65bf2011-11-24 14:31:46 +01003268
Takashi Iwai4e76a882014-02-25 12:21:03 +01003269 dev_warn(bus->card->dev, "Too many %s devices\n",
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003270 snd_hda_pcm_type_name[type]);
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003271#ifndef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai4e76a882014-02-25 12:21:03 +01003272 dev_warn(bus->card->dev,
3273 "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003274#endif
Wu Fengguangf5d6def2009-10-30 11:38:26 +01003275 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003276}
3277
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003278/* call build_pcms ops of the given codec and set up the default parameters */
3279int snd_hda_codec_parse_pcms(struct hda_codec *codec)
Takashi Iwai176d5332008-07-30 15:01:44 +02003280{
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003281 struct hda_pcm *cpcm;
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003282 int err;
Takashi Iwai176d5332008-07-30 15:01:44 +02003283
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003284 if (!list_empty(&codec->pcm_list_head))
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003285 return 0; /* already parsed */
3286
3287 if (!codec->patch_ops.build_pcms)
3288 return 0;
3289
3290 err = codec->patch_ops.build_pcms(codec);
3291 if (err < 0) {
3292 codec_err(codec, "cannot build PCMs for #%d (error %d)\n",
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003293 codec->core.addr, err);
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003294 return err;
3295 }
3296
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003297 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003298 int stream;
3299
3300 for (stream = 0; stream < 2; stream++) {
3301 struct hda_pcm_stream *info = &cpcm->stream[stream];
3302
3303 if (!info->substreams)
3304 continue;
Takashi Iwai176d5332008-07-30 15:01:44 +02003305 err = set_pcm_default_values(codec, info);
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003306 if (err < 0) {
3307 codec_warn(codec,
3308 "fail to setup default for PCM %s\n",
3309 cpcm->name);
Takashi Iwai176d5332008-07-30 15:01:44 +02003310 return err;
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003311 }
Takashi Iwai176d5332008-07-30 15:01:44 +02003312 }
3313 }
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003314
3315 return 0;
Takashi Iwai176d5332008-07-30 15:01:44 +02003316}
3317
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003318/* assign all PCMs of the given codec */
3319int snd_hda_codec_build_pcms(struct hda_codec *codec)
3320{
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003321 struct hda_bus *bus = codec->bus;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003322 struct hda_pcm *cpcm;
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003323 int dev, err;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003324
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003325 err = snd_hda_codec_parse_pcms(codec);
Takashi Iwaid2896192015-10-20 16:23:55 +02003326 if (err < 0)
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003327 return err;
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003328
3329 /* attach a new PCM streams */
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003330 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003331 if (cpcm->pcm)
3332 continue; /* already attached */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003333 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01003334 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003335
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003336 dev = get_empty_pcm_device(bus, cpcm->pcm_type);
3337 if (dev < 0)
3338 continue; /* no fatal error */
3339 cpcm->device = dev;
Takashi Iwai0a505752015-04-16 23:25:02 +02003340 err = snd_hda_attach_pcm_stream(bus, codec, cpcm);
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003341 if (err < 0) {
3342 codec_err(codec,
3343 "cannot attach PCM stream %d for codec #%d\n",
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003344 dev, codec->core.addr);
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003345 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003346 }
3347 }
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003348
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003349 return 0;
3350}
3351
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 * snd_hda_add_new_ctls - create controls from the array
3354 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003355 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 *
3357 * This helper function creates and add new controls in the given array.
3358 * The array must be terminated with an empty entry as terminator.
3359 *
3360 * Returns 0 if successful, or a negative error code.
3361 */
Takashi Iwai031024e2011-05-02 11:29:30 +02003362int snd_hda_add_new_ctls(struct hda_codec *codec,
3363 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01003365 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366
3367 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01003368 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01003369 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003370 if (knew->iface == -1) /* skip this codec private value */
3371 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01003372 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01003373 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003374 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01003375 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01003376 if (addr > 0)
3377 kctl->id.device = addr;
3378 if (idx > 0)
3379 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003380 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003381 if (!err)
3382 break;
3383 /* try first with another device index corresponding to
3384 * the codec addr; if it still fails (or it's the
3385 * primary codec), then try another control index
3386 */
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003387 if (!addr && codec->core.addr)
3388 addr = codec->core.addr;
Takashi Iwai1afe2062010-12-23 10:17:52 +01003389 else if (!idx && !knew->index) {
3390 idx = find_empty_mixer_ctl_idx(codec,
Takashi Iwaidcda5802012-10-12 17:24:51 +02003391 knew->name, 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003392 if (idx <= 0)
3393 return err;
3394 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01003395 return err;
3396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 }
3398 return 0;
3399}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003400EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401
Takashi Iwai83012a72012-08-24 18:38:08 +02003402#ifdef CONFIG_PM
Takashi Iwaibb573922015-02-20 09:26:04 +01003403static void codec_set_power_save(struct hda_codec *codec, int delay)
Takashi Iwaicc72da72015-02-19 16:00:22 +01003404{
3405 struct device *dev = hda_codec_dev(codec);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003406
Lu, Han2377c3c2015-06-09 16:50:38 +08003407 if (delay == 0 && codec->auto_runtime_pm)
3408 delay = 3000;
3409
Takashi Iwaicc72da72015-02-19 16:00:22 +01003410 if (delay > 0) {
3411 pm_runtime_set_autosuspend_delay(dev, delay);
3412 pm_runtime_use_autosuspend(dev);
3413 pm_runtime_allow(dev);
3414 if (!pm_runtime_suspended(dev))
3415 pm_runtime_mark_last_busy(dev);
3416 } else {
3417 pm_runtime_dont_use_autosuspend(dev);
3418 pm_runtime_forbid(dev);
3419 }
3420}
Takashi Iwaibb573922015-02-20 09:26:04 +01003421
3422/**
3423 * snd_hda_set_power_save - reprogram autosuspend for the given delay
3424 * @bus: HD-audio bus
3425 * @delay: autosuspend delay in msec, 0 = off
3426 *
3427 * Synchronize the runtime PM autosuspend state from the power_save option.
3428 */
3429void snd_hda_set_power_save(struct hda_bus *bus, int delay)
3430{
3431 struct hda_codec *c;
3432
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003433 list_for_each_codec(c, bus)
Takashi Iwaibb573922015-02-20 09:26:04 +01003434 codec_set_power_save(c, delay);
3435}
3436EXPORT_SYMBOL_GPL(snd_hda_set_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003437
Takashi Iwaid5191e52009-11-16 14:58:17 +01003438/**
3439 * snd_hda_check_amp_list_power - Check the amp list and update the power
3440 * @codec: HD-audio codec
3441 * @check: the object containing an AMP list and the status
3442 * @nid: NID to check / update
3443 *
3444 * Check whether the given NID is in the amp list. If it's in the list,
3445 * check the current AMP status, and update the the power-status according
3446 * to the mute status.
3447 *
3448 * This function is supposed to be set or called from the check_power_status
3449 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003450 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003451int snd_hda_check_amp_list_power(struct hda_codec *codec,
3452 struct hda_loopback_check *check,
3453 hda_nid_t nid)
3454{
Takashi Iwai031024e2011-05-02 11:29:30 +02003455 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003456 int ch, v;
3457
3458 if (!check->amplist)
3459 return 0;
3460 for (p = check->amplist; p->nid; p++) {
3461 if (p->nid == nid)
3462 break;
3463 }
3464 if (!p->nid)
3465 return 0; /* nothing changed */
3466
3467 for (p = check->amplist; p->nid; p++) {
3468 for (ch = 0; ch < 2; ch++) {
3469 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
3470 p->idx);
3471 if (!(v & HDA_AMP_MUTE) && v > 0) {
3472 if (!check->power_on) {
3473 check->power_on = 1;
Takashi Iwai664c7152015-04-08 11:43:14 +02003474 snd_hda_power_up_pm(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003475 }
3476 return 1;
3477 }
3478 }
3479 }
3480 if (check->power_on) {
3481 check->power_on = 0;
Takashi Iwai664c7152015-04-08 11:43:14 +02003482 snd_hda_power_down_pm(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003483 }
3484 return 0;
3485}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003486EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003487#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003489/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 * input MUX helper
3491 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01003492
3493/**
3494 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003495 * @imux: imux helper object
3496 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003497 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003498int snd_hda_input_mux_info(const struct hda_input_mux *imux,
3499 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500{
3501 unsigned int index;
3502
3503 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3504 uinfo->count = 1;
3505 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02003506 if (!imux->num_items)
3507 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 index = uinfo->value.enumerated.item;
3509 if (index >= imux->num_items)
3510 index = imux->num_items - 1;
3511 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
3512 return 0;
3513}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003514EXPORT_SYMBOL_GPL(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515
Takashi Iwaid5191e52009-11-16 14:58:17 +01003516/**
3517 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003518 * @codec: the HDA codec
3519 * @imux: imux helper object
3520 * @ucontrol: pointer to get/store the data
3521 * @nid: input mux NID
3522 * @cur_val: pointer to get/store the current imux value
Takashi Iwaid5191e52009-11-16 14:58:17 +01003523 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003524int snd_hda_input_mux_put(struct hda_codec *codec,
3525 const struct hda_input_mux *imux,
3526 struct snd_ctl_elem_value *ucontrol,
3527 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 unsigned int *cur_val)
3529{
3530 unsigned int idx;
3531
Takashi Iwai5513b0c2007-10-09 11:58:41 +02003532 if (!imux->num_items)
3533 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 idx = ucontrol->value.enumerated.item[0];
3535 if (idx >= imux->num_items)
3536 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003537 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003539 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
3540 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 *cur_val = idx;
3542 return 1;
3543}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003544EXPORT_SYMBOL_GPL(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545
3546
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003547/**
3548 * snd_hda_enum_helper_info - Helper for simple enum ctls
3549 * @kcontrol: ctl element
3550 * @uinfo: pointer to get/store the data
3551 * @num_items: number of enum items
3552 * @texts: enum item string array
3553 *
Takashi Iwaidda415d2012-11-30 18:34:38 +01003554 * process kcontrol info callback of a simple string enum array
3555 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
3556 */
3557int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
3558 struct snd_ctl_elem_info *uinfo,
3559 int num_items, const char * const *texts)
3560{
3561 static const char * const texts_default[] = {
3562 "Disabled", "Enabled"
3563 };
3564
3565 if (!texts || !num_items) {
3566 num_items = 2;
3567 texts = texts_default;
3568 }
3569
Takashi Iwai3ff72212014-10-20 18:17:28 +02003570 return snd_ctl_enum_info(uinfo, 1, num_items, texts);
Takashi Iwaidda415d2012-11-30 18:34:38 +01003571}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003572EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info);
Takashi Iwaidda415d2012-11-30 18:34:38 +01003573
3574/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 * Multi-channel / digital-out PCM helper functions
3576 */
3577
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003578/* setup SPDIF output stream */
3579static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
3580 unsigned int stream_tag, unsigned int format)
3581{
Laurence Darby3bef1c32012-11-03 17:00:06 +00003582 struct hda_spdif_out *spdif;
3583 unsigned int curr_fmt;
3584 bool reset;
Stephen Warren7c9359762011-06-01 11:14:17 -06003585
Laurence Darby3bef1c32012-11-03 17:00:06 +00003586 spdif = snd_hda_spdif_out_of_nid(codec, nid);
3587 curr_fmt = snd_hda_codec_read(codec, nid, 0,
3588 AC_VERB_GET_STREAM_FORMAT, 0);
3589 reset = codec->spdif_status_reset &&
3590 (spdif->ctls & AC_DIG1_ENABLE) &&
3591 curr_fmt != format;
3592
3593 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
3594 updated */
3595 if (reset)
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003596 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06003597 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02003598 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003599 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02003600 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02003601 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02003602 for (d = codec->slave_dig_outs; *d; d++)
3603 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
3604 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04003605 }
Takashi Iwai2f728532008-09-25 16:32:41 +02003606 /* turn on again (if needed) */
Laurence Darby3bef1c32012-11-03 17:00:06 +00003607 if (reset)
Takashi Iwai2f728532008-09-25 16:32:41 +02003608 set_dig_out_convert(codec, nid,
Stephen Warren7c9359762011-06-01 11:14:17 -06003609 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02003610}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04003611
Takashi Iwai2f728532008-09-25 16:32:41 +02003612static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
3613{
3614 snd_hda_codec_cleanup_stream(codec, nid);
3615 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02003616 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02003617 for (d = codec->slave_dig_outs; *d; d++)
3618 snd_hda_codec_cleanup_stream(codec, *d);
3619 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003620}
3621
Takashi Iwaid5191e52009-11-16 14:58:17 +01003622/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003623 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003624 * @codec: the HDA codec
3625 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003627int snd_hda_multi_out_dig_open(struct hda_codec *codec,
3628 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629{
Ingo Molnar62932df2006-01-16 16:34:20 +01003630 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02003631 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
3632 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02003633 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01003635 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 return 0;
3637}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003638EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639
Takashi Iwaid5191e52009-11-16 14:58:17 +01003640/**
3641 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003642 * @codec: the HDA codec
3643 * @mout: hda_multi_out object
3644 * @stream_tag: stream tag to assign
3645 * @format: format id to assign
3646 * @substream: PCM substream to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01003647 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003648int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
3649 struct hda_multi_out *mout,
3650 unsigned int stream_tag,
3651 unsigned int format,
3652 struct snd_pcm_substream *substream)
3653{
3654 mutex_lock(&codec->spdif_mutex);
3655 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
3656 mutex_unlock(&codec->spdif_mutex);
3657 return 0;
3658}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003659EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003660
Takashi Iwaid5191e52009-11-16 14:58:17 +01003661/**
3662 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003663 * @codec: the HDA codec
3664 * @mout: hda_multi_out object
Takashi Iwaid5191e52009-11-16 14:58:17 +01003665 */
Takashi Iwai9411e212009-02-13 11:32:28 +01003666int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
3667 struct hda_multi_out *mout)
3668{
3669 mutex_lock(&codec->spdif_mutex);
3670 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3671 mutex_unlock(&codec->spdif_mutex);
3672 return 0;
3673}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003674EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup);
Takashi Iwai9411e212009-02-13 11:32:28 +01003675
Takashi Iwaid5191e52009-11-16 14:58:17 +01003676/**
3677 * snd_hda_multi_out_dig_close - release the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003678 * @codec: the HDA codec
3679 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003681int snd_hda_multi_out_dig_close(struct hda_codec *codec,
3682 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683{
Ingo Molnar62932df2006-01-16 16:34:20 +01003684 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01003686 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 return 0;
3688}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003689EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690
Takashi Iwaid5191e52009-11-16 14:58:17 +01003691/**
3692 * snd_hda_multi_out_analog_open - open analog outputs
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003693 * @codec: the HDA codec
3694 * @mout: hda_multi_out object
3695 * @substream: PCM substream to assign
3696 * @hinfo: PCM information to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01003697 *
3698 * Open analog outputs and set up the hw-constraints.
3699 * If the digital outputs can be opened as slave, open the digital
3700 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003702int snd_hda_multi_out_analog_open(struct hda_codec *codec,
3703 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01003704 struct snd_pcm_substream *substream,
3705 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706{
Takashi Iwai9a081602008-02-12 18:37:26 +01003707 struct snd_pcm_runtime *runtime = substream->runtime;
3708 runtime->hw.channels_max = mout->max_channels;
3709 if (mout->dig_out_nid) {
3710 if (!mout->analog_rates) {
3711 mout->analog_rates = hinfo->rates;
3712 mout->analog_formats = hinfo->formats;
3713 mout->analog_maxbps = hinfo->maxbps;
3714 } else {
3715 runtime->hw.rates = mout->analog_rates;
3716 runtime->hw.formats = mout->analog_formats;
3717 hinfo->maxbps = mout->analog_maxbps;
3718 }
3719 if (!mout->spdif_rates) {
3720 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
3721 &mout->spdif_rates,
3722 &mout->spdif_formats,
3723 &mout->spdif_maxbps);
3724 }
3725 mutex_lock(&codec->spdif_mutex);
3726 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02003727 if ((runtime->hw.rates & mout->spdif_rates) &&
3728 (runtime->hw.formats & mout->spdif_formats)) {
3729 runtime->hw.rates &= mout->spdif_rates;
3730 runtime->hw.formats &= mout->spdif_formats;
3731 if (mout->spdif_maxbps < hinfo->maxbps)
3732 hinfo->maxbps = mout->spdif_maxbps;
3733 } else {
3734 mout->share_spdif = 0;
3735 /* FIXME: need notify? */
3736 }
Takashi Iwai9a081602008-02-12 18:37:26 +01003737 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02003738 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01003739 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 return snd_pcm_hw_constraint_step(substream->runtime, 0,
3741 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
3742}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003743EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744
Takashi Iwaid5191e52009-11-16 14:58:17 +01003745/**
3746 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003747 * @codec: the HDA codec
3748 * @mout: hda_multi_out object
3749 * @stream_tag: stream tag to assign
3750 * @format: format id to assign
3751 * @substream: PCM substream to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01003752 *
3753 * Set up the i/o for analog out.
3754 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003756int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
3757 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758 unsigned int stream_tag,
3759 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003760 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761{
Takashi Iwaidda14412011-05-02 11:29:30 +02003762 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003764 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 int i;
3766
Ingo Molnar62932df2006-01-16 16:34:20 +01003767 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003768 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01003769 if (mout->dig_out_nid && mout->share_spdif &&
3770 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02003772 snd_hda_is_supported_format(codec, mout->dig_out_nid,
3773 format) &&
Stephen Warren7c9359762011-06-01 11:14:17 -06003774 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003776 setup_dig_out_stream(codec, mout->dig_out_nid,
3777 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 } else {
3779 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02003780 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 }
3782 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003783 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784
3785 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003786 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
3787 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02003788 if (!mout->no_share_stream &&
3789 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003791 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
3792 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01003793 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02003794 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
3795 if (!mout->no_share_stream && mout->hp_out_nid[i])
3796 snd_hda_codec_setup_stream(codec,
3797 mout->hp_out_nid[i],
3798 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01003799
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 /* surrounds */
3801 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02003802 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003803 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3804 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02003805 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003806 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3807 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 }
David Henningssoncd4035e2013-10-10 09:01:25 +02003809
3810 /* extra surrounds */
3811 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
3812 int ch = 0;
3813 if (!mout->extra_out_nid[i])
3814 break;
3815 if (chs >= (i + 1) * 2)
3816 ch = i * 2;
3817 else if (!mout->no_share_stream)
3818 break;
3819 snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
3820 stream_tag, ch, format);
3821 }
3822
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823 return 0;
3824}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003825EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826
Takashi Iwaid5191e52009-11-16 14:58:17 +01003827/**
3828 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003829 * @codec: the HDA codec
3830 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003832int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
3833 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834{
Takashi Iwaidda14412011-05-02 11:29:30 +02003835 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 int i;
3837
3838 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01003839 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01003841 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02003842 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
3843 if (mout->hp_out_nid[i])
3844 snd_hda_codec_cleanup_stream(codec,
3845 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01003846 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
3847 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01003848 snd_hda_codec_cleanup_stream(codec,
3849 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01003850 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02003852 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 mout->dig_out_used = 0;
3854 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003855 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856 return 0;
3857}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003858EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859
Takashi Iwai47408602012-04-20 13:06:53 +02003860/**
3861 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003862 * @codec: the HDA codec
3863 * @pin: referred pin NID
Takashi Iwai47408602012-04-20 13:06:53 +02003864 *
3865 * Guess the suitable VREF pin bits to be set as the pin-control value.
3866 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
3867 */
3868unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
3869{
3870 unsigned int pincap;
3871 unsigned int oldval;
3872 oldval = snd_hda_codec_read(codec, pin, 0,
3873 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3874 pincap = snd_hda_query_pin_caps(codec, pin);
3875 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
3876 /* Exception: if the default pin setup is vref50, we give it priority */
3877 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
3878 return AC_PINCTL_VREF_80;
3879 else if (pincap & AC_PINCAP_VREF_50)
3880 return AC_PINCTL_VREF_50;
3881 else if (pincap & AC_PINCAP_VREF_100)
3882 return AC_PINCTL_VREF_100;
3883 else if (pincap & AC_PINCAP_VREF_GRD)
3884 return AC_PINCTL_VREF_GRD;
3885 return AC_PINCTL_VREF_HIZ;
3886}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003887EXPORT_SYMBOL_GPL(snd_hda_get_default_vref);
Takashi Iwai47408602012-04-20 13:06:53 +02003888
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003889/**
3890 * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap
3891 * @codec: the HDA codec
3892 * @pin: referred pin NID
3893 * @val: pin ctl value to audit
3894 */
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01003895unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
3896 hda_nid_t pin, unsigned int val)
3897{
3898 static unsigned int cap_lists[][2] = {
3899 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
3900 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
3901 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
3902 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
3903 };
3904 unsigned int cap;
3905
3906 if (!val)
3907 return 0;
3908 cap = snd_hda_query_pin_caps(codec, pin);
3909 if (!cap)
3910 return val; /* don't know what to do... */
3911
3912 if (val & AC_PINCTL_OUT_EN) {
3913 if (!(cap & AC_PINCAP_OUT))
3914 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
3915 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
3916 val &= ~AC_PINCTL_HP_EN;
3917 }
3918
3919 if (val & AC_PINCTL_IN_EN) {
3920 if (!(cap & AC_PINCAP_IN))
3921 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
3922 else {
3923 unsigned int vcap, vref;
3924 int i;
3925 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
3926 vref = val & AC_PINCTL_VREFEN;
3927 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
3928 if (vref == cap_lists[i][0] &&
3929 !(vcap & cap_lists[i][1])) {
3930 if (i == ARRAY_SIZE(cap_lists) - 1)
3931 vref = AC_PINCTL_VREF_HIZ;
3932 else
3933 vref = cap_lists[i + 1][0];
3934 }
3935 }
3936 val &= ~AC_PINCTL_VREFEN;
3937 val |= vref;
3938 }
3939 }
3940
3941 return val;
3942}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003943EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl);
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01003944
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003945/**
3946 * _snd_hda_pin_ctl - Helper to set pin ctl value
3947 * @codec: the HDA codec
3948 * @pin: referred pin NID
3949 * @val: pin control value to set
3950 * @cached: access over codec pinctl cache or direct write
3951 *
3952 * This function is a helper to set a pin ctl value more safely.
3953 * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the
3954 * value in pin target array via snd_hda_codec_set_pin_target(), then
3955 * actually writes the value via either snd_hda_codec_update_cache() or
3956 * snd_hda_codec_write() depending on @cached flag.
3957 */
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02003958int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
3959 unsigned int val, bool cached)
3960{
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01003961 val = snd_hda_correct_pin_ctl(codec, pin, val);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01003962 snd_hda_codec_set_pin_target(codec, pin, val);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02003963 if (cached)
3964 return snd_hda_codec_update_cache(codec, pin, 0,
3965 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
3966 else
3967 return snd_hda_codec_write(codec, pin, 0,
3968 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
3969}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003970EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02003971
Takashi Iwai990061c2010-09-09 22:08:44 +02003972/**
3973 * snd_hda_add_imux_item - Add an item to input_mux
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003974 * @codec: the HDA codec
3975 * @imux: imux helper object
3976 * @label: the name of imux item to assign
3977 * @index: index number of imux item to assign
3978 * @type_idx: pointer to store the resultant label index
Takashi Iwai990061c2010-09-09 22:08:44 +02003979 *
3980 * When the same label is used already in the existing items, the number
3981 * suffix is appended to the label. This label index number is stored
3982 * to type_idx when non-NULL pointer is given.
3983 */
Takashi Iwai6194b992014-06-06 18:12:16 +02003984int snd_hda_add_imux_item(struct hda_codec *codec,
3985 struct hda_input_mux *imux, const char *label,
Takashi Iwai10a20af2010-09-09 16:28:02 +02003986 int index, int *type_idx)
3987{
3988 int i, label_idx = 0;
3989 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
Takashi Iwai6194b992014-06-06 18:12:16 +02003990 codec_err(codec, "hda_codec: Too many imux items!\n");
Takashi Iwai10a20af2010-09-09 16:28:02 +02003991 return -EINVAL;
3992 }
3993 for (i = 0; i < imux->num_items; i++) {
3994 if (!strncmp(label, imux->items[i].label, strlen(label)))
3995 label_idx++;
3996 }
3997 if (type_idx)
3998 *type_idx = label_idx;
3999 if (label_idx > 0)
4000 snprintf(imux->items[imux->num_items].label,
4001 sizeof(imux->items[imux->num_items].label),
4002 "%s %d", label, label_idx);
4003 else
4004 strlcpy(imux->items[imux->num_items].label, label,
4005 sizeof(imux->items[imux->num_items].label));
4006 imux->items[imux->num_items].index = index;
4007 imux->num_items++;
4008 return 0;
4009}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004010EXPORT_SYMBOL_GPL(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02004011
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012/**
Takashi Iwai0a505752015-04-16 23:25:02 +02004013 * snd_hda_bus_reset_codecs - Reset the bus
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004014 * @bus: HD-audio bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 */
Takashi Iwai0a505752015-04-16 23:25:02 +02004016void snd_hda_bus_reset_codecs(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004018 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019
Takashi Iwaid068ebc2015-03-02 23:22:59 +01004020 list_for_each_codec(codec, bus) {
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004021 /* FIXME: maybe a better way needed for forced reset */
David Henningsson26a6cb62012-10-09 15:04:21 +02004022 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004023#ifdef CONFIG_PM
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05004024 if (hda_codec_is_power_on(codec)) {
Takashi Iwaicc72da72015-02-19 16:00:22 +01004025 hda_call_codec_suspend(codec);
Mengdong Lin12edb892013-11-26 23:32:23 -05004026 hda_call_codec_resume(codec);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004027 }
4028#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030}
Takashi Iwaib2e18592008-07-30 15:01:44 +02004031
Takashi Iwaid5191e52009-11-16 14:58:17 +01004032/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004033 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
4034 * @pcm: PCM caps bits
4035 * @buf: the string buffer to write
4036 * @buflen: the max buffer length
4037 *
4038 * used by hda_proc.c and hda_eld.c
4039 */
Takashi Iwaib2022262008-11-21 21:24:03 +01004040void snd_print_pcm_bits(int pcm, char *buf, int buflen)
4041{
4042 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
4043 int i, j;
4044
4045 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
4046 if (pcm & (AC_SUPPCM_BITS_8 << i))
4047 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
4048
4049 buf[j] = '\0'; /* necessary when j == 0 */
4050}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004051EXPORT_SYMBOL_GPL(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004052
4053MODULE_DESCRIPTION("HDA codec core");
4054MODULE_LICENSE("GPL");