blob: 3db26c451837cf0e2893d1071c1a699465d89e1b [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/init.h>
23#include <linux/delay.h>
24#include <linux/slab.h>
Ingo Molnar62932df2006-01-16 16:34:20 +010025#include <linux/mutex.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040026#include <linux/module.h>
Takashi Iwaicc72da72015-02-19 16:00:22 +010027#include <linux/pm.h>
28#include <linux/pm_runtime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <sound/core.h>
30#include "hda_codec.h"
31#include <sound/asoundef.h>
Jaroslav Kysela302e9c52006-07-05 17:39:49 +020032#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <sound/initval.h>
Takashi Iwaicd372fb2011-03-03 14:40:14 +010034#include <sound/jack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "hda_local.h"
Jaroslav Kysela123c07a2009-10-21 14:48:23 +020036#include "hda_beep.h"
Takashi Iwai1835a0f2011-10-27 22:12:46 +020037#include "hda_jack.h"
Takashi Iwai28073142007-07-27 18:58:06 +020038#include <sound/hda_hwdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Takashi Iwai83012a72012-08-24 18:38:08 +020040#ifdef CONFIG_PM
Takashi Iwai7639a062015-03-03 10:07:24 +010041#define codec_in_pm(codec) atomic_read(&(codec)->core.in_pm)
Takashi Iwaicc72da72015-02-19 16:00:22 +010042#define hda_codec_is_power_on(codec) \
43 (!pm_runtime_suspended(hda_codec_dev(codec)))
Takashi Iwaicb53c622007-08-10 17:21:45 +020044#else
Takashi Iwaid846b172012-11-24 11:58:24 +010045#define codec_in_pm(codec) 0
Takashi Iwaie581f3d2011-07-26 10:19:20 +020046#define hda_codec_is_power_on(codec) 1
Takashi Iwaicb53c622007-08-10 17:21:45 +020047#endif
48
Takashi Iwai7639a062015-03-03 10:07:24 +010049#define codec_has_epss(codec) \
50 ((codec)->core.power_caps & AC_PWRST_EPSS)
51#define codec_has_clkstop(codec) \
52 ((codec)->core.power_caps & AC_PWRST_CLKSTOP)
53
Takashi Iwai33fa35e2008-11-06 16:50:40 +010054/*
Takashi Iwai058524482015-03-03 15:40:08 +010055 * Send and receive a verb - passed to exec_verb override for hdac_device
Takashi Iwaiaa2936f2009-05-26 16:07:57 +020056 */
Takashi Iwai058524482015-03-03 15:40:08 +010057static int codec_exec_verb(struct hdac_device *dev, unsigned int cmd,
58 unsigned int flags, unsigned int *res)
Takashi Iwaiaa2936f2009-05-26 16:07:57 +020059{
Takashi Iwai058524482015-03-03 15:40:08 +010060 struct hda_codec *codec = container_of(dev, struct hda_codec, core);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +020061 struct hda_bus *bus = codec->bus;
Takashi Iwai8dd78332009-06-02 01:16:07 +020062 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +020063
Wu Fengguang6430aee2009-07-17 16:49:19 +080064 if (cmd == ~0)
65 return -1;
66
Takashi Iwai8dd78332009-06-02 01:16:07 +020067 again:
Takashi Iwai664c7152015-04-08 11:43:14 +020068 snd_hda_power_up_pm(codec);
Takashi Iwaid068ebc2015-03-02 23:22:59 +010069 mutex_lock(&bus->core.cmd_mutex);
Takashi Iwai63e51fd72013-06-06 14:20:19 +020070 if (flags & HDA_RW_NO_RESPONSE_FALLBACK)
71 bus->no_response_fallback = 1;
Takashi Iwaid068ebc2015-03-02 23:22:59 +010072 err = snd_hdac_bus_exec_verb_unlocked(&bus->core, codec->core.addr,
73 cmd, res);
Takashi Iwai63e51fd72013-06-06 14:20:19 +020074 bus->no_response_fallback = 0;
Takashi Iwaid068ebc2015-03-02 23:22:59 +010075 mutex_unlock(&bus->core.cmd_mutex);
Takashi Iwai664c7152015-04-08 11:43:14 +020076 snd_hda_power_down_pm(codec);
Takashi Iwaicad372f2015-03-25 17:57:00 +010077 if (!codec_in_pm(codec) && res && err == -EAGAIN) {
Takashi Iwai8dd78332009-06-02 01:16:07 +020078 if (bus->response_reset) {
Takashi Iwai4e76a882014-02-25 12:21:03 +010079 codec_dbg(codec,
80 "resetting BUS due to fatal communication error\n");
Takashi Iwai0a505752015-04-16 23:25:02 +020081 snd_hda_bus_reset(bus);
Takashi Iwai8dd78332009-06-02 01:16:07 +020082 }
83 goto again;
84 }
85 /* clear reset-flag when the communication gets recovered */
Takashi Iwaid846b172012-11-24 11:58:24 +010086 if (!err || codec_in_pm(codec))
Takashi Iwai8dd78332009-06-02 01:16:07 +020087 bus->response_reset = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +020088 return err;
89}
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 * snd_hda_sequence_write - sequence writes
93 * @codec: the HDA codec
94 * @seq: VERB array to send
95 *
96 * Send the commands sequentially from the given array.
97 * The array must be terminated with NID=0.
98 */
99void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
100{
101 for (; seq->nid; seq++)
102 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
103}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100104EXPORT_SYMBOL_GPL(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100106/* connection list element */
107struct hda_conn_list {
108 struct list_head list;
109 int len;
110 hda_nid_t nid;
111 hda_nid_t conns[0];
112};
113
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200114/* look up the cached results */
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100115static struct hda_conn_list *
116lookup_conn_list(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200117{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100118 struct hda_conn_list *p;
119 list_for_each_entry(p, &codec->conn_list, list) {
120 if (p->nid == nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200121 return p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200122 }
123 return NULL;
124}
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200125
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100126static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
127 const hda_nid_t *list)
128{
129 struct hda_conn_list *p;
130
131 p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL);
132 if (!p)
133 return -ENOMEM;
134 p->len = len;
135 p->nid = nid;
136 memcpy(p->conns, list, len * sizeof(hda_nid_t));
137 list_add(&p->list, &codec->conn_list);
138 return 0;
139}
140
141static void remove_conn_list(struct hda_codec *codec)
142{
143 while (!list_empty(&codec->conn_list)) {
144 struct hda_conn_list *p;
145 p = list_first_entry(&codec->conn_list, typeof(*p), list);
146 list_del(&p->list);
147 kfree(p);
148 }
149}
150
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200151/* read the connection and add to the cache */
152static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200153{
Takashi Iwai4eea3092013-02-07 18:18:19 +0100154 hda_nid_t list[32];
155 hda_nid_t *result = list;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200156 int len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200157
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200158 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
Takashi Iwai4eea3092013-02-07 18:18:19 +0100159 if (len == -ENOSPC) {
160 len = snd_hda_get_num_raw_conns(codec, nid);
161 result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL);
162 if (!result)
163 return -ENOMEM;
164 len = snd_hda_get_raw_connections(codec, nid, result, len);
165 }
166 if (len >= 0)
167 len = snd_hda_override_conn_list(codec, nid, len, result);
168 if (result != list)
169 kfree(result);
170 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200171}
Takashi Iwaidce20792011-06-24 14:10:28 +0200172
173/**
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100174 * snd_hda_get_conn_list - get connection list
175 * @codec: the HDA codec
176 * @nid: NID to parse
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100177 * @listp: the pointer to store NID list
178 *
179 * Parses the connection list of the given widget and stores the pointer
180 * to the list of NIDs.
181 *
182 * Returns the number of connections, or a negative error code.
183 *
184 * Note that the returned pointer isn't protected against the list
185 * modification. If snd_hda_override_conn_list() might be called
186 * concurrently, protect with a mutex appropriately.
187 */
188int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
189 const hda_nid_t **listp)
190{
191 bool added = false;
192
193 for (;;) {
194 int err;
195 const struct hda_conn_list *p;
196
197 /* if the connection-list is already cached, read it */
198 p = lookup_conn_list(codec, nid);
199 if (p) {
200 if (listp)
201 *listp = p->conns;
202 return p->len;
203 }
204 if (snd_BUG_ON(added))
205 return -EINVAL;
206
207 err = read_and_add_raw_conns(codec, nid);
208 if (err < 0)
209 return err;
210 added = true;
211 }
212}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100213EXPORT_SYMBOL_GPL(snd_hda_get_conn_list);
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100214
215/**
Takashi Iwaidce20792011-06-24 14:10:28 +0200216 * snd_hda_get_connections - copy connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 * @codec: the HDA codec
218 * @nid: NID to parse
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200219 * @conn_list: connection list array; when NULL, checks only the size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 * @max_conns: max. number of connections to store
221 *
222 * Parses the connection list of the given widget and stores the list
223 * of NIDs.
224 *
225 * Returns the number of connections, or a negative error code.
226 */
227int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200228 hda_nid_t *conn_list, int max_conns)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200229{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100230 const hda_nid_t *list;
231 int len = snd_hda_get_conn_list(codec, nid, &list);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200232
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100233 if (len > 0 && conn_list) {
234 if (len > max_conns) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100235 codec_err(codec, "Too many connections %d for NID 0x%x\n",
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200236 len, nid);
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200237 return -EINVAL;
238 }
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100239 memcpy(conn_list, list, len * sizeof(hda_nid_t));
Takashi Iwaidce20792011-06-24 14:10:28 +0200240 }
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200241
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100242 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200243}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100244EXPORT_SYMBOL_GPL(snd_hda_get_connections);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200247 * snd_hda_override_conn_list - add/modify the connection-list to cache
248 * @codec: the HDA codec
249 * @nid: NID to parse
250 * @len: number of connection list entries
251 * @list: the list of connection entries
252 *
253 * Add or modify the given connection-list to the cache. If the corresponding
254 * cache already exists, invalidate it and append a new one.
255 *
256 * Returns zero or a negative error code.
257 */
258int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
259 const hda_nid_t *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100261 struct hda_conn_list *p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200262
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100263 p = lookup_conn_list(codec, nid);
264 if (p) {
265 list_del(&p->list);
266 kfree(p);
267 }
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200268
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100269 return add_conn_list(codec, nid, len, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100271EXPORT_SYMBOL_GPL(snd_hda_override_conn_list);
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200272
273/**
Takashi Iwai8d087c72011-06-28 12:45:47 +0200274 * snd_hda_get_conn_index - get the connection index of the given NID
275 * @codec: the HDA codec
276 * @mux: NID containing the list
277 * @nid: NID to select
278 * @recursive: 1 when searching NID recursively, otherwise 0
279 *
280 * Parses the connection list of the widget @mux and checks whether the
281 * widget @nid is present. If it is, return the connection index.
282 * Otherwise it returns -1.
283 */
284int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
285 hda_nid_t nid, int recursive)
286{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100287 const hda_nid_t *conn;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200288 int i, nums;
289
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100290 nums = snd_hda_get_conn_list(codec, mux, &conn);
Takashi Iwai8d087c72011-06-28 12:45:47 +0200291 for (i = 0; i < nums; i++)
292 if (conn[i] == nid)
293 return i;
294 if (!recursive)
295 return -1;
Takashi Iwaid94ddd82012-12-20 14:42:42 +0100296 if (recursive > 10) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100297 codec_dbg(codec, "too deep connection for 0x%x\n", nid);
Takashi Iwai8d087c72011-06-28 12:45:47 +0200298 return -1;
299 }
300 recursive++;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200301 for (i = 0; i < nums; i++) {
302 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
303 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
304 continue;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200305 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
306 return i;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200307 }
Takashi Iwai8d087c72011-06-28 12:45:47 +0200308 return -1;
309}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100310EXPORT_SYMBOL_GPL(snd_hda_get_conn_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Libin Yang13800f32017-01-12 16:04:52 +0800312/**
313 * snd_hda_get_num_devices - get DEVLIST_LEN parameter of the given widget
314 * @codec: the HDA codec
315 * @nid: NID of the pin to parse
316 *
317 * Get the device entry number on the given widget. This is a feature of
318 * DP MST audio. Each pin can have several device entries in it.
319 */
320unsigned int snd_hda_get_num_devices(struct hda_codec *codec, hda_nid_t nid)
Mengdong Linf1aa0682013-08-26 21:35:21 -0400321{
322 unsigned int wcaps = get_wcaps(codec, nid);
323 unsigned int parm;
324
325 if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) ||
326 get_wcaps_type(wcaps) != AC_WID_PIN)
327 return 0;
328
Dave Airlie132bd962015-06-09 13:39:31 +1000329 parm = snd_hdac_read_parm_uncached(&codec->core, nid, AC_PAR_DEVLIST_LEN);
Takashi Iwai86548442015-06-09 07:22:26 +0200330 if (parm == -1)
Mengdong Linf1aa0682013-08-26 21:35:21 -0400331 parm = 0;
332 return parm & AC_DEV_LIST_LEN_MASK;
333}
Libin Yang13800f32017-01-12 16:04:52 +0800334EXPORT_SYMBOL_GPL(snd_hda_get_num_devices);
Mengdong Linf1aa0682013-08-26 21:35:21 -0400335
336/**
337 * snd_hda_get_devices - copy device list without cache
338 * @codec: the HDA codec
339 * @nid: NID of the pin to parse
340 * @dev_list: device list array
341 * @max_devices: max. number of devices to store
342 *
343 * Copy the device list. This info is dynamic and so not cached.
344 * Currently called only from hda_proc.c, so not exported.
345 */
346int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid,
347 u8 *dev_list, int max_devices)
348{
349 unsigned int parm;
350 int i, dev_len, devices;
351
Libin Yang13800f32017-01-12 16:04:52 +0800352 parm = snd_hda_get_num_devices(codec, nid);
Mengdong Linf1aa0682013-08-26 21:35:21 -0400353 if (!parm) /* not multi-stream capable */
354 return 0;
355
356 dev_len = parm + 1;
357 dev_len = dev_len < max_devices ? dev_len : max_devices;
358
359 devices = 0;
360 while (devices < dev_len) {
Takashi Iwaicad372f2015-03-25 17:57:00 +0100361 if (snd_hdac_read(&codec->core, nid,
362 AC_VERB_GET_DEVICE_LIST, devices, &parm))
363 break; /* error */
Mengdong Linf1aa0682013-08-26 21:35:21 -0400364
365 for (i = 0; i < 8; i++) {
366 dev_list[devices] = (u8)parm;
367 parm >>= 4;
368 devices++;
369 if (devices >= dev_len)
370 break;
371 }
372 }
373 return devices;
374}
375
Libin Yang13800f32017-01-12 16:04:52 +0800376/**
377 * snd_hda_get_dev_select - get device entry select on the pin
378 * @codec: the HDA codec
379 * @nid: NID of the pin to get device entry select
380 *
381 * Get the devcie entry select on the pin. Return the device entry
382 * id selected on the pin. Return 0 means the first device entry
383 * is selected or MST is not supported.
384 */
385int snd_hda_get_dev_select(struct hda_codec *codec, hda_nid_t nid)
386{
387 /* not support dp_mst will always return 0, using first dev_entry */
388 if (!codec->dp_mst)
389 return 0;
390
391 return snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DEVICE_SEL, 0);
392}
393EXPORT_SYMBOL_GPL(snd_hda_get_dev_select);
394
395/**
396 * snd_hda_set_dev_select - set device entry select on the pin
397 * @codec: the HDA codec
398 * @nid: NID of the pin to set device entry select
399 * @dev_id: device entry id to be set
400 *
401 * Set the device entry select on the pin nid.
402 */
403int snd_hda_set_dev_select(struct hda_codec *codec, hda_nid_t nid, int dev_id)
404{
405 int ret, num_devices;
406
407 /* not support dp_mst will always return 0, using first dev_entry */
408 if (!codec->dp_mst)
409 return 0;
410
411 /* AC_PAR_DEVLIST_LEN is 0 based. */
412 num_devices = snd_hda_get_num_devices(codec, nid) + 1;
413 /* If Device List Length is 0 (num_device = 1),
414 * the pin is not multi stream capable.
415 * Do nothing in this case.
416 */
417 if (num_devices == 1)
418 return 0;
419
420 /* Behavior of setting index being equal to or greater than
421 * Device List Length is not predictable
422 */
423 if (num_devices <= dev_id)
424 return -EINVAL;
425
426 ret = snd_hda_codec_write(codec, nid, 0,
427 AC_VERB_SET_DEVICE_SEL, dev_id);
428
429 return ret;
430}
431EXPORT_SYMBOL_GPL(snd_hda_set_dev_select);
432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100434 * read widget caps for each widget and store in cache
435 */
436static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
437{
438 int i;
439 hda_nid_t nid;
440
Takashi Iwai7639a062015-03-03 10:07:24 +0100441 codec->wcaps = kmalloc(codec->core.num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200442 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100443 return -ENOMEM;
Takashi Iwai7639a062015-03-03 10:07:24 +0100444 nid = codec->core.start_nid;
445 for (i = 0; i < codec->core.num_nodes; i++, nid++)
Takashi Iwai9ba17b42015-03-03 23:29:47 +0100446 codec->wcaps[i] = snd_hdac_read_parm_uncached(&codec->core,
447 nid, AC_PAR_AUDIO_WIDGET_CAP);
Takashi Iwai54d17402005-11-21 16:33:22 +0100448 return 0;
449}
450
Takashi Iwai3be14142009-02-20 14:11:16 +0100451/* read all pin default configurations and save codec->init_pins */
452static int read_pin_defaults(struct hda_codec *codec)
453{
Takashi Iwai7639a062015-03-03 10:07:24 +0100454 hda_nid_t nid;
Takashi Iwai3be14142009-02-20 14:11:16 +0100455
Takashi Iwai7639a062015-03-03 10:07:24 +0100456 for_each_hda_codec_node(nid, codec) {
Takashi Iwai3be14142009-02-20 14:11:16 +0100457 struct hda_pincfg *pin;
458 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +0200459 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +0100460 if (wid_type != AC_WID_PIN)
461 continue;
462 pin = snd_array_new(&codec->init_pins);
463 if (!pin)
464 return -ENOMEM;
465 pin->nid = nid;
466 pin->cfg = snd_hda_codec_read(codec, nid, 0,
467 AC_VERB_GET_CONFIG_DEFAULT, 0);
Libin Yang91520852017-01-12 16:04:53 +0800468 /*
469 * all device entries are the same widget control so far
470 * fixme: if any codec is different, need fix here
471 */
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200472 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
473 AC_VERB_GET_PIN_WIDGET_CONTROL,
474 0);
Takashi Iwai3be14142009-02-20 14:11:16 +0100475 }
476 return 0;
477}
478
479/* look up the given pin config list and return the item matching with NID */
480static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
481 struct snd_array *array,
482 hda_nid_t nid)
483{
484 int i;
485 for (i = 0; i < array->used; i++) {
486 struct hda_pincfg *pin = snd_array_elem(array, i);
487 if (pin->nid == nid)
488 return pin;
489 }
490 return NULL;
491}
492
Takashi Iwai3be14142009-02-20 14:11:16 +0100493/* set the current pin config value for the given NID.
494 * the value is cached, and read via snd_hda_codec_get_pincfg()
495 */
496int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
497 hda_nid_t nid, unsigned int cfg)
498{
499 struct hda_pincfg *pin;
500
Takashi Iwaid5657ec2013-04-18 09:59:28 +0200501 /* the check below may be invalid when pins are added by a fixup
502 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
503 * for now
504 */
505 /*
Takashi Iwaib82855a2009-12-27 11:24:56 +0100506 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
507 return -EINVAL;
Takashi Iwaid5657ec2013-04-18 09:59:28 +0200508 */
Takashi Iwaib82855a2009-12-27 11:24:56 +0100509
Takashi Iwai3be14142009-02-20 14:11:16 +0100510 pin = look_up_pincfg(codec, list, nid);
511 if (!pin) {
512 pin = snd_array_new(list);
513 if (!pin)
514 return -ENOMEM;
515 pin->nid = nid;
516 }
517 pin->cfg = cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100518 return 0;
519}
520
Takashi Iwaid5191e52009-11-16 14:58:17 +0100521/**
522 * snd_hda_codec_set_pincfg - Override a pin default configuration
523 * @codec: the HDA codec
524 * @nid: NID to set the pin config
525 * @cfg: the pin default config value
526 *
527 * Override a pin default configuration value in the cache.
528 * This value can be read by snd_hda_codec_get_pincfg() in a higher
529 * priority than the real hardware value.
530 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100531int snd_hda_codec_set_pincfg(struct hda_codec *codec,
532 hda_nid_t nid, unsigned int cfg)
533{
Takashi Iwai346ff702009-02-23 09:42:57 +0100534 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100535}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100536EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100537
Takashi Iwaid5191e52009-11-16 14:58:17 +0100538/**
539 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
540 * @codec: the HDA codec
541 * @nid: NID to get the pin config
542 *
543 * Get the current pin config value of the given pin NID.
544 * If the pincfg value is cached or overridden via sysfs or driver,
545 * returns the cached value.
546 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100547unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
548{
549 struct hda_pincfg *pin;
550
Takashi Iwai648a8d22014-02-25 10:38:13 +0100551#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai09b70e82013-01-10 18:21:56 +0100552 {
553 unsigned int cfg = 0;
554 mutex_lock(&codec->user_mutex);
555 pin = look_up_pincfg(codec, &codec->user_pins, nid);
556 if (pin)
557 cfg = pin->cfg;
558 mutex_unlock(&codec->user_mutex);
559 if (cfg)
560 return cfg;
561 }
Takashi Iwai3be14142009-02-20 14:11:16 +0100562#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100563 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
564 if (pin)
565 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100566 pin = look_up_pincfg(codec, &codec->init_pins, nid);
567 if (pin)
568 return pin->cfg;
569 return 0;
570}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100571EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100572
Takashi Iwai95a962c2014-10-29 16:03:58 +0100573/**
574 * snd_hda_codec_set_pin_target - remember the current pinctl target value
575 * @codec: the HDA codec
576 * @nid: pin NID
577 * @val: assigned pinctl value
578 *
579 * This function stores the given value to a pinctl target value in the
580 * pincfg table. This isn't always as same as the actually written value
581 * but can be referred at any time via snd_hda_codec_get_pin_target().
582 */
Takashi Iwaid7fdc002013-01-10 08:38:04 +0100583int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
584 unsigned int val)
585{
586 struct hda_pincfg *pin;
587
588 pin = look_up_pincfg(codec, &codec->init_pins, nid);
589 if (!pin)
590 return -EINVAL;
591 pin->target = val;
592 return 0;
593}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100594EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target);
Takashi Iwaid7fdc002013-01-10 08:38:04 +0100595
Takashi Iwai95a962c2014-10-29 16:03:58 +0100596/**
597 * snd_hda_codec_get_pin_target - return the current pinctl target value
598 * @codec: the HDA codec
599 * @nid: pin NID
600 */
Takashi Iwaid7fdc002013-01-10 08:38:04 +0100601int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
602{
603 struct hda_pincfg *pin;
604
605 pin = look_up_pincfg(codec, &codec->init_pins, nid);
606 if (!pin)
607 return 0;
608 return pin->target;
609}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100610EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target);
Takashi Iwaid7fdc002013-01-10 08:38:04 +0100611
Takashi Iwai92ee6162009-12-27 11:18:59 +0100612/**
613 * snd_hda_shutup_pins - Shut up all pins
614 * @codec: the HDA codec
615 *
616 * Clear all pin controls to shup up before suspend for avoiding click noise.
617 * The controls aren't cached so that they can be resumed properly.
618 */
619void snd_hda_shutup_pins(struct hda_codec *codec)
620{
621 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200622 /* don't shut up pins when unloading the driver; otherwise it breaks
623 * the default pin setup at the next load of the driver
624 */
625 if (codec->bus->shutdown)
626 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +0100627 for (i = 0; i < codec->init_pins.used; i++) {
628 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
629 /* use read here for syncing after issuing each verb */
630 snd_hda_codec_read(codec, pin->nid, 0,
631 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
632 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200633 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +0100634}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100635EXPORT_SYMBOL_GPL(snd_hda_shutup_pins);
Takashi Iwai92ee6162009-12-27 11:18:59 +0100636
Takashi Iwai2a439522011-07-26 09:52:50 +0200637#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200638/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
639static void restore_shutup_pins(struct hda_codec *codec)
640{
641 int i;
642 if (!codec->pins_shutup)
643 return;
644 if (codec->bus->shutdown)
645 return;
646 for (i = 0; i < codec->init_pins.used; i++) {
647 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
648 snd_hda_codec_write(codec, pin->nid, 0,
649 AC_VERB_SET_PIN_WIDGET_CONTROL,
650 pin->ctrl);
651 }
652 codec->pins_shutup = 0;
653}
Mike Waychison1c7276c2011-04-20 12:04:36 -0700654#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200655
David Henningsson26a6cb62012-10-09 15:04:21 +0200656static void hda_jackpoll_work(struct work_struct *work)
657{
658 struct hda_codec *codec =
659 container_of(work, struct hda_codec, jackpoll_work.work);
David Henningsson26a6cb62012-10-09 15:04:21 +0200660
661 snd_hda_jack_set_dirty_all(codec);
662 snd_hda_jack_poll_all(codec);
Wang Xingchao18e60622013-07-25 23:34:45 -0400663
664 if (!codec->jackpoll_interval)
665 return;
666
Takashi Iwai2f35c632015-02-27 22:43:26 +0100667 schedule_delayed_work(&codec->jackpoll_work,
668 codec->jackpoll_interval);
David Henningsson26a6cb62012-10-09 15:04:21 +0200669}
670
Takashi Iwai3fdf1462012-11-22 08:16:31 +0100671/* release all pincfg lists */
672static void free_init_pincfgs(struct hda_codec *codec)
Takashi Iwai3be14142009-02-20 14:11:16 +0100673{
Takashi Iwai346ff702009-02-23 09:42:57 +0100674 snd_array_free(&codec->driver_pins);
Takashi Iwai648a8d22014-02-25 10:38:13 +0100675#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai346ff702009-02-23 09:42:57 +0100676 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +0100677#endif
Takashi Iwai3be14142009-02-20 14:11:16 +0100678 snd_array_free(&codec->init_pins);
679}
680
Takashi Iwai54d17402005-11-21 16:33:22 +0100681/*
Takashi Iwaieb541332010-08-06 13:48:11 +0200682 * audio-converter setup caches
683 */
684struct hda_cvt_setup {
685 hda_nid_t nid;
686 u8 stream_tag;
687 u8 channel_id;
688 u16 format_id;
689 unsigned char active; /* cvt is currently used */
690 unsigned char dirty; /* setups should be cleared */
691};
692
693/* get or create a cache entry for the given audio converter NID */
694static struct hda_cvt_setup *
695get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
696{
697 struct hda_cvt_setup *p;
698 int i;
699
700 for (i = 0; i < codec->cvt_setups.used; i++) {
701 p = snd_array_elem(&codec->cvt_setups, i);
702 if (p->nid == nid)
703 return p;
704 }
705 p = snd_array_new(&codec->cvt_setups);
706 if (p)
707 p->nid = nid;
708 return p;
709}
710
711/*
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100712 * PCM device
713 */
714static void release_pcm(struct kref *kref)
715{
716 struct hda_pcm *pcm = container_of(kref, struct hda_pcm, kref);
717
718 if (pcm->pcm)
719 snd_device_free(pcm->codec->card, pcm->pcm);
720 clear_bit(pcm->device, pcm->codec->bus->pcm_dev_bits);
721 kfree(pcm->name);
722 kfree(pcm);
723}
724
725void snd_hda_codec_pcm_put(struct hda_pcm *pcm)
726{
727 kref_put(&pcm->kref, release_pcm);
728}
729EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_put);
730
731struct hda_pcm *snd_hda_codec_pcm_new(struct hda_codec *codec,
732 const char *fmt, ...)
733{
734 struct hda_pcm *pcm;
735 va_list args;
736
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100737 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
738 if (!pcm)
739 return NULL;
740
741 pcm->codec = codec;
742 kref_init(&pcm->kref);
Takashi Iwai30e5f002015-04-27 16:39:19 +0200743 va_start(args, fmt);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100744 pcm->name = kvasprintf(GFP_KERNEL, fmt, args);
Takashi Iwai30e5f002015-04-27 16:39:19 +0200745 va_end(args);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100746 if (!pcm->name) {
747 kfree(pcm);
748 return NULL;
749 }
750
751 list_add_tail(&pcm->list, &codec->pcm_list_head);
752 return pcm;
753}
754EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_new);
755
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100756/*
757 * codec destructor
758 */
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100759static void codec_release_pcms(struct hda_codec *codec)
760{
761 struct hda_pcm *pcm, *n;
762
763 list_for_each_entry_safe(pcm, n, &codec->pcm_list_head, list) {
764 list_del_init(&pcm->list);
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100765 if (pcm->pcm)
766 snd_device_disconnect(codec->card, pcm->pcm);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100767 snd_hda_codec_pcm_put(pcm);
768 }
769}
770
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100771void snd_hda_codec_cleanup_for_unbind(struct hda_codec *codec)
772{
Takashi Iwaic4c25332015-03-03 17:22:12 +0100773 if (codec->registered) {
774 /* pm_runtime_put() is called in snd_hdac_device_exit() */
775 pm_runtime_get_noresume(hda_codec_dev(codec));
776 pm_runtime_disable(hda_codec_dev(codec));
777 codec->registered = 0;
778 }
779
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100780 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100781 if (!codec->in_freeing)
782 snd_hda_ctls_clear(codec);
783 codec_release_pcms(codec);
784 snd_hda_detach_beep_device(codec);
785 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
786 snd_hda_jack_tbl_clear(codec);
787 codec->proc_widget_hook = NULL;
788 codec->spec = NULL;
789
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100790 /* free only driver_pins so that init_pins + user_pins are restored */
791 snd_array_free(&codec->driver_pins);
792 snd_array_free(&codec->cvt_setups);
793 snd_array_free(&codec->spdif_out);
794 snd_array_free(&codec->verbs);
795 codec->preset = NULL;
796 codec->slave_dig_outs = NULL;
797 codec->spdif_status_reset = 0;
798 snd_array_free(&codec->mixers);
799 snd_array_free(&codec->nids);
800 remove_conn_list(codec);
Takashi Iwai4d75faa02015-02-25 14:42:38 +0100801 snd_hdac_regmap_exit(&codec->core);
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100802}
803
Takashi Iwaid8193872012-08-31 07:54:38 -0700804static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwaibb6ac722009-03-13 09:02:42 +0100805 unsigned int power_state);
806
Takashi Iwaic4c25332015-03-03 17:22:12 +0100807/* also called from hda_bind.c */
808void snd_hda_codec_register(struct hda_codec *codec)
809{
810 if (codec->registered)
811 return;
812 if (device_is_registered(hda_codec_dev(codec))) {
813 snd_hda_register_beep_device(codec);
Mengdong Lina5e7e072015-04-29 17:43:20 +0800814 snd_hdac_link_power(&codec->core, true);
Takashi Iwaic4c25332015-03-03 17:22:12 +0100815 pm_runtime_enable(hda_codec_dev(codec));
816 /* it was powered up in snd_hda_codec_new(), now all done */
817 snd_hda_power_down(codec);
818 codec->registered = 1;
819 }
820}
821
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100822static int snd_hda_codec_dev_register(struct snd_device *device)
823{
Takashi Iwaic4c25332015-03-03 17:22:12 +0100824 snd_hda_codec_register(device->device_data);
Takashi Iwaid604b392014-02-28 13:42:09 +0100825 return 0;
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100826}
827
828static int snd_hda_codec_dev_disconnect(struct snd_device *device)
829{
830 struct hda_codec *codec = device->device_data;
831
Takashi Iwaid604b392014-02-28 13:42:09 +0100832 snd_hda_detach_beep_device(codec);
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100833 return 0;
834}
835
Takashi Iwai2565c892014-02-19 11:41:09 +0100836static int snd_hda_codec_dev_free(struct snd_device *device)
837{
Takashi Iwaid56db742015-02-27 23:25:35 +0100838 struct hda_codec *codec = device->device_data;
839
840 codec->in_freeing = 1;
Takashi Iwai3256be62015-02-24 14:59:42 +0100841 snd_hdac_device_unregister(&codec->core);
Takashi Iwaia4577822015-06-11 14:02:49 +0200842 snd_hdac_link_power(&codec->core, false);
Takashi Iwaid56db742015-02-27 23:25:35 +0100843 put_device(hda_codec_dev(codec));
Takashi Iwai2565c892014-02-19 11:41:09 +0100844 return 0;
845}
846
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100847static void snd_hda_codec_dev_release(struct device *dev)
848{
Takashi Iwaid56db742015-02-27 23:25:35 +0100849 struct hda_codec *codec = dev_to_hda_codec(dev);
850
851 free_init_pincfgs(codec);
Takashi Iwai7639a062015-03-03 10:07:24 +0100852 snd_hdac_device_exit(&codec->core);
Takashi Iwaid56db742015-02-27 23:25:35 +0100853 snd_hda_sysfs_clear(codec);
Takashi Iwaid56db742015-02-27 23:25:35 +0100854 kfree(codec->modelname);
855 kfree(codec->wcaps);
Takashi Iwaid56db742015-02-27 23:25:35 +0100856 kfree(codec);
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100857}
858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859/**
860 * snd_hda_codec_new - create a HDA codec
861 * @bus: the bus to assign
862 * @codec_addr: the codec address
863 * @codecp: the pointer to store the generated codec
864 *
865 * Returns 0 if successful, or a negative error code.
866 */
Takashi Iwai6efdd852015-02-27 16:09:22 +0100867int snd_hda_codec_new(struct hda_bus *bus, struct snd_card *card,
868 unsigned int codec_addr, struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
870 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +0200871 char component[31];
Takashi Iwaid8193872012-08-31 07:54:38 -0700872 hda_nid_t fg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 int err;
Takashi Iwai2565c892014-02-19 11:41:09 +0100874 static struct snd_device_ops dev_ops = {
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100875 .dev_register = snd_hda_codec_dev_register,
876 .dev_disconnect = snd_hda_codec_dev_disconnect,
Takashi Iwai2565c892014-02-19 11:41:09 +0100877 .dev_free = snd_hda_codec_dev_free,
878 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Takashi Iwaida3cec32008-08-08 17:12:14 +0200880 if (snd_BUG_ON(!bus))
881 return -EINVAL;
882 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
883 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200885 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Takashi Iwaif4de8fe2015-02-27 16:17:18 +0100886 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Takashi Iwai7639a062015-03-03 10:07:24 +0100889 sprintf(component, "hdaudioC%dD%d", card->number, codec_addr);
890 err = snd_hdac_device_init(&codec->core, &bus->core, component,
891 codec_addr);
892 if (err < 0) {
893 kfree(codec);
894 return err;
895 }
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100896
Takashi Iwai7639a062015-03-03 10:07:24 +0100897 codec->core.dev.release = snd_hda_codec_dev_release;
898 codec->core.type = HDA_DEV_LEGACY;
Takashi Iwai058524482015-03-03 15:40:08 +0100899 codec->core.exec_verb = codec_exec_verb;
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 codec->bus = bus;
Takashi Iwai6efdd852015-02-27 16:09:22 +0100902 codec->card = card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +0100904 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +0800905 mutex_init(&codec->control_mutex);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +0100906 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
907 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +0100908 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +0100909 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +0200910 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Stephen Warren7c935972011-06-01 11:14:17 -0600911 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai361dab32012-05-09 14:35:27 +0200912 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100913 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100914 INIT_LIST_HEAD(&codec->conn_list);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100915 INIT_LIST_HEAD(&codec->pcm_list_head);
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100916
David Henningsson26a6cb62012-10-09 15:04:21 +0200917 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
Mengdong Lin7f132922013-11-29 01:48:45 -0500918 codec->depop_delay = -1;
David Henningssonf5662e12014-07-22 14:09:34 +0200919 codec->fixup_id = HDA_FIXUP_ID_NOT_SET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Takashi Iwai83012a72012-08-24 18:38:08 +0200921#ifdef CONFIG_PM
Takashi Iwaicc72da72015-02-19 16:00:22 +0100922 codec->power_jiffies = jiffies;
Takashi Iwaicb53c622007-08-10 17:21:45 +0200923#endif
924
Takashi Iwai648a8d22014-02-25 10:38:13 +0100925 snd_hda_sysfs_init(codec);
926
Takashi Iwaic382a9f2012-05-07 15:01:02 +0200927 if (codec->bus->modelname) {
928 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
929 if (!codec->modelname) {
Heloise NH6986a0e2015-07-17 09:42:06 +0800930 err = -ENOMEM;
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100931 goto error;
Takashi Iwaic382a9f2012-05-07 15:01:02 +0200932 }
933 }
934
Takashi Iwai7639a062015-03-03 10:07:24 +0100935 fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
Takashi Iwaid8193872012-08-31 07:54:38 -0700936 err = read_widget_caps(codec, fg);
Takashi Iwaif4de8fe2015-02-27 16:17:18 +0100937 if (err < 0)
Takashi Iwai3be14142009-02-20 14:11:16 +0100938 goto error;
Takashi Iwai3be14142009-02-20 14:11:16 +0100939 err = read_pin_defaults(codec);
940 if (err < 0)
941 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +0100942
Takashi Iwaibb6ac722009-03-13 09:02:42 +0100943 /* power-up all before initialization */
Takashi Iwaid8193872012-08-31 07:54:38 -0700944 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaibb6ac722009-03-13 09:02:42 +0100945
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200946 snd_hda_codec_proc_new(codec);
947
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200948 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200949
Takashi Iwai7639a062015-03-03 10:07:24 +0100950 sprintf(component, "HDA:%08x,%08x,%08x", codec->core.vendor_id,
951 codec->core.subsystem_id, codec->core.revision_id);
Takashi Iwai6efdd852015-02-27 16:09:22 +0100952 snd_component_add(card, component);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200953
Takashi Iwai6efdd852015-02-27 16:09:22 +0100954 err = snd_device_new(card, SNDRV_DEV_CODEC, codec, &dev_ops);
Takashi Iwai2565c892014-02-19 11:41:09 +0100955 if (err < 0)
956 goto error;
957
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200958 if (codecp)
959 *codecp = codec;
960 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +0100961
962 error:
Takashi Iwaid56db742015-02-27 23:25:35 +0100963 put_device(hda_codec_dev(codec));
Takashi Iwai3be14142009-02-20 14:11:16 +0100964 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200965}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100966EXPORT_SYMBOL_GPL(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200967
Takashi Iwai95a962c2014-10-29 16:03:58 +0100968/**
969 * snd_hda_codec_update_widgets - Refresh widget caps and pin defaults
970 * @codec: the HDA codec
971 *
972 * Forcibly refresh the all widget caps and the init pin configurations of
973 * the given codec.
974 */
Mengdong Lina15d05d2013-02-08 17:09:31 -0500975int snd_hda_codec_update_widgets(struct hda_codec *codec)
976{
977 hda_nid_t fg;
978 int err;
979
Takashi Iwai9dcc1442015-08-25 08:52:55 +0200980 err = snd_hdac_refresh_widget_sysfs(&codec->core);
Takashi Iwai7639a062015-03-03 10:07:24 +0100981 if (err < 0)
982 return err;
983
Mengdong Lina15d05d2013-02-08 17:09:31 -0500984 /* Assume the function group node does not change,
985 * only the widget nodes may change.
986 */
987 kfree(codec->wcaps);
Takashi Iwai7639a062015-03-03 10:07:24 +0100988 fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
Mengdong Lina15d05d2013-02-08 17:09:31 -0500989 err = read_widget_caps(codec, fg);
Takashi Iwaif4de8fe2015-02-27 16:17:18 +0100990 if (err < 0)
Mengdong Lina15d05d2013-02-08 17:09:31 -0500991 return err;
Mengdong Lina15d05d2013-02-08 17:09:31 -0500992
993 snd_array_free(&codec->init_pins);
994 err = read_pin_defaults(codec);
995
996 return err;
997}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100998EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets);
Mengdong Lina15d05d2013-02-08 17:09:31 -0500999
Takashi Iwaied360812012-08-08 17:12:52 +02001000/* update the stream-id if changed */
1001static void update_pcm_stream_id(struct hda_codec *codec,
1002 struct hda_cvt_setup *p, hda_nid_t nid,
1003 u32 stream_tag, int channel_id)
1004{
1005 unsigned int oldval, newval;
1006
1007 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1008 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1009 newval = (stream_tag << 4) | channel_id;
1010 if (oldval != newval)
1011 snd_hda_codec_write(codec, nid, 0,
1012 AC_VERB_SET_CHANNEL_STREAMID,
1013 newval);
1014 p->stream_tag = stream_tag;
1015 p->channel_id = channel_id;
1016 }
1017}
1018
1019/* update the format-id if changed */
1020static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1021 hda_nid_t nid, int format)
1022{
1023 unsigned int oldval;
1024
1025 if (p->format_id != format) {
1026 oldval = snd_hda_codec_read(codec, nid, 0,
1027 AC_VERB_GET_STREAM_FORMAT, 0);
1028 if (oldval != format) {
1029 msleep(1);
1030 snd_hda_codec_write(codec, nid, 0,
1031 AC_VERB_SET_STREAM_FORMAT,
1032 format);
1033 }
1034 p->format_id = format;
1035 }
1036}
1037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038/**
1039 * snd_hda_codec_setup_stream - set up the codec for streaming
1040 * @codec: the CODEC to set up
1041 * @nid: the NID to set up
1042 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1043 * @channel_id: channel id to pass, zero based.
1044 * @format: stream format.
1045 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001046void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1047 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 int channel_id, int format)
1049{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001050 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001051 struct hda_cvt_setup *p;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001052 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001053 int i;
1054
Takashi Iwai0ba21762007-04-16 11:29:14 +02001055 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001056 return;
1057
Takashi Iwai4e76a882014-02-25 12:21:03 +01001058 codec_dbg(codec,
1059 "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1060 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001061 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001062 if (!p)
Takashi Iwaieb541332010-08-06 13:48:11 +02001063 return;
Takashi Iwaied360812012-08-08 17:12:52 +02001064
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01001065 if (codec->patch_ops.stream_pm)
1066 codec->patch_ops.stream_pm(codec, nid, true);
Takashi Iwaied360812012-08-08 17:12:52 +02001067 if (codec->pcm_format_first)
1068 update_pcm_format(codec, p, nid, format);
1069 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1070 if (!codec->pcm_format_first)
1071 update_pcm_format(codec, p, nid, format);
1072
Takashi Iwaieb541332010-08-06 13:48:11 +02001073 p->active = 1;
1074 p->dirty = 0;
1075
1076 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001077 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwaid068ebc2015-03-02 23:22:59 +01001078 list_for_each_codec(c, codec->bus) {
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001079 for (i = 0; i < c->cvt_setups.used; i++) {
1080 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001081 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001082 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001083 p->dirty = 1;
1084 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001087EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Takashi Iwaif0cea792010-08-13 11:56:53 +02001089static void really_cleanup_stream(struct hda_codec *codec,
1090 struct hda_cvt_setup *q);
1091
Takashi Iwaid5191e52009-11-16 14:58:17 +01001092/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001093 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001094 * @codec: the CODEC to clean up
1095 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001096 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001097 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001098void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1099 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001100{
Takashi Iwaieb541332010-08-06 13:48:11 +02001101 struct hda_cvt_setup *p;
1102
Takashi Iwai888afa12008-03-18 09:57:50 +01001103 if (!nid)
1104 return;
1105
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001106 if (codec->no_sticky_stream)
1107 do_now = 1;
1108
Takashi Iwai4e76a882014-02-25 12:21:03 +01001109 codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001110 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001111 if (p) {
Takashi Iwaif0cea792010-08-13 11:56:53 +02001112 /* here we just clear the active flag when do_now isn't set;
1113 * actual clean-ups will be done later in
1114 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1115 */
1116 if (do_now)
1117 really_cleanup_stream(codec, p);
1118 else
1119 p->active = 0;
1120 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001121}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001122EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001123
Takashi Iwaieb541332010-08-06 13:48:11 +02001124static void really_cleanup_stream(struct hda_codec *codec,
1125 struct hda_cvt_setup *q)
1126{
1127 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001128 if (q->stream_tag || q->channel_id)
1129 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1130 if (q->format_id)
1131 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1132);
Takashi Iwaieb541332010-08-06 13:48:11 +02001133 memset(q, 0, sizeof(*q));
1134 q->nid = nid;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01001135 if (codec->patch_ops.stream_pm)
1136 codec->patch_ops.stream_pm(codec, nid, false);
Takashi Iwaieb541332010-08-06 13:48:11 +02001137}
1138
1139/* clean up the all conflicting obsolete streams */
1140static void purify_inactive_streams(struct hda_codec *codec)
1141{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001142 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001143 int i;
1144
Takashi Iwaid068ebc2015-03-02 23:22:59 +01001145 list_for_each_codec(c, codec->bus) {
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001146 for (i = 0; i < c->cvt_setups.used; i++) {
1147 struct hda_cvt_setup *p;
1148 p = snd_array_elem(&c->cvt_setups, i);
1149 if (p->dirty)
1150 really_cleanup_stream(c, p);
1151 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001152 }
1153}
1154
Takashi Iwai2a439522011-07-26 09:52:50 +02001155#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001156/* clean up all streams; called from suspend */
1157static void hda_cleanup_all_streams(struct hda_codec *codec)
1158{
1159 int i;
1160
1161 for (i = 0; i < codec->cvt_setups.used; i++) {
1162 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1163 if (p->stream_tag)
1164 really_cleanup_stream(codec, p);
1165 }
1166}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001167#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169/*
1170 * amp access functions
1171 */
1172
Takashi Iwaid5191e52009-11-16 14:58:17 +01001173/**
1174 * query_amp_caps - query AMP capabilities
1175 * @codec: the HD-auio codec
1176 * @nid: the NID to query
1177 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1178 *
1179 * Query AMP capabilities for the given widget and direction.
1180 * Returns the obtained capability bits.
1181 *
1182 * When cap bits have been already read, this doesn't read again but
1183 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001185u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186{
Takashi Iwaifaa75f82015-02-26 08:54:56 +01001187 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1188 nid = codec->core.afg;
1189 return snd_hda_param_read(codec, nid,
1190 direction == HDA_OUTPUT ?
1191 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001193EXPORT_SYMBOL_GPL(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Takashi Iwaid5191e52009-11-16 14:58:17 +01001195/**
David Henningsson861a04e2014-09-23 10:38:17 +02001196 * snd_hda_check_amp_caps - query AMP capabilities
1197 * @codec: the HD-audio codec
1198 * @nid: the NID to query
1199 * @dir: either #HDA_INPUT or #HDA_OUTPUT
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001200 * @bits: bit mask to check the result
David Henningsson861a04e2014-09-23 10:38:17 +02001201 *
1202 * Check whether the widget has the given amp capability for the direction.
1203 */
1204bool snd_hda_check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
1205 int dir, unsigned int bits)
1206{
1207 if (!nid)
1208 return false;
1209 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
1210 if (query_amp_caps(codec, nid, dir) & bits)
1211 return true;
1212 return false;
1213}
1214EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps);
1215
1216/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01001217 * snd_hda_override_amp_caps - Override the AMP capabilities
1218 * @codec: the CODEC to clean up
1219 * @nid: the NID to clean up
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001220 * @dir: either #HDA_INPUT or #HDA_OUTPUT
Takashi Iwaid5191e52009-11-16 14:58:17 +01001221 * @caps: the capability bits to set
1222 *
1223 * Override the cached AMP caps bits value by the given one.
1224 * This function is useful if the driver needs to adjust the AMP ranges,
1225 * e.g. limit to 0dB, etc.
1226 *
1227 * Returns zero if successful or a negative error code.
1228 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001229int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1230 unsigned int caps)
1231{
Takashi Iwaifaa75f82015-02-26 08:54:56 +01001232 unsigned int parm;
1233
1234 snd_hda_override_wcaps(codec, nid,
1235 get_wcaps(codec, nid) | AC_WCAP_AMP_OVRD);
1236 parm = dir == HDA_OUTPUT ? AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP;
1237 return snd_hdac_override_parm(&codec->core, nid, parm, caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001238}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001239EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001240
Takashi Iwaid5191e52009-11-16 14:58:17 +01001241/**
Takashi Iwaia686ec42015-06-11 10:51:28 +02001242 * snd_hda_codec_amp_update - update the AMP mono value
1243 * @codec: HD-audio codec
1244 * @nid: NID to read the AMP value
1245 * @ch: channel to update (0 or 1)
1246 * @dir: #HDA_INPUT or #HDA_OUTPUT
1247 * @idx: the index value (only for input direction)
1248 * @mask: bit mask to set
1249 * @val: the bits value to set
1250 *
1251 * Update the AMP values for the given channel, direction and index.
1252 */
1253int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid,
1254 int ch, int dir, int idx, int mask, int val)
1255{
1256 unsigned int cmd = snd_hdac_regmap_encode_amp(nid, ch, dir, idx);
1257
1258 /* enable fake mute if no h/w mute but min=mute */
1259 if ((query_amp_caps(codec, nid, dir) &
1260 (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) == AC_AMPCAP_MIN_MUTE)
1261 cmd |= AC_AMP_FAKE_MUTE;
1262 return snd_hdac_regmap_update_raw(&codec->core, cmd, mask, val);
1263}
1264EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update);
1265
1266/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01001267 * snd_hda_codec_amp_stereo - update the AMP stereo values
1268 * @codec: HD-audio codec
1269 * @nid: NID to read the AMP value
1270 * @direction: #HDA_INPUT or #HDA_OUTPUT
1271 * @idx: the index value (only for input direction)
1272 * @mask: bit mask to set
1273 * @val: the bits value to set
1274 *
1275 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1276 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001277 */
1278int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1279 int direction, int idx, int mask, int val)
1280{
1281 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001282
1283 if (snd_BUG_ON(mask & ~0xff))
1284 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001285 for (ch = 0; ch < 2; ch++)
1286 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1287 idx, mask, val);
1288 return ret;
1289}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001290EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001291
Takashi Iwai95a962c2014-10-29 16:03:58 +01001292/**
1293 * snd_hda_codec_amp_init - initialize the AMP value
1294 * @codec: the HDA codec
1295 * @nid: NID to read the AMP value
1296 * @ch: channel (left=0 or right=1)
1297 * @dir: #HDA_INPUT or #HDA_OUTPUT
1298 * @idx: the index value (only for input direction)
1299 * @mask: bit mask to set
1300 * @val: the bits value to set
1301 *
1302 * Works like snd_hda_codec_amp_update() but it writes the value only at
Takashi Iwai280e57d2012-12-14 10:32:21 +01001303 * the first access. If the amp was already initialized / updated beforehand,
1304 * this does nothing.
1305 */
1306int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
1307 int dir, int idx, int mask, int val)
1308{
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01001309 int orig;
1310
1311 if (!codec->core.regmap)
1312 return -EINVAL;
1313 regcache_cache_only(codec->core.regmap, true);
1314 orig = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1315 regcache_cache_only(codec->core.regmap, false);
1316 if (orig >= 0)
1317 return 0;
1318 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx, mask, val);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001319}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001320EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001321
Takashi Iwai95a962c2014-10-29 16:03:58 +01001322/**
1323 * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value
1324 * @codec: the HDA codec
1325 * @nid: NID to read the AMP value
1326 * @dir: #HDA_INPUT or #HDA_OUTPUT
1327 * @idx: the index value (only for input direction)
1328 * @mask: bit mask to set
1329 * @val: the bits value to set
1330 *
1331 * Call snd_hda_codec_amp_init() for both stereo channels.
1332 */
Takashi Iwai280e57d2012-12-14 10:32:21 +01001333int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
1334 int dir, int idx, int mask, int val)
1335{
1336 int ch, ret = 0;
1337
1338 if (snd_BUG_ON(mask & ~0xff))
1339 mask &= 0xff;
1340 for (ch = 0; ch < 2; ch++)
1341 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
1342 idx, mask, val);
1343 return ret;
1344}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001345EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001346
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001347static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1348 unsigned int ofs)
1349{
1350 u32 caps = query_amp_caps(codec, nid, dir);
1351 /* get num steps */
1352 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1353 if (ofs < caps)
1354 caps -= ofs;
1355 return caps;
1356}
1357
Takashi Iwaid5191e52009-11-16 14:58:17 +01001358/**
1359 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001360 * @kcontrol: referred ctl element
1361 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01001362 *
1363 * The control element is supposed to have the private_value field
1364 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1365 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001366int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1367 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368{
1369 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1370 u16 nid = get_amp_nid(kcontrol);
1371 u8 chs = get_amp_channels(kcontrol);
1372 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001373 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001375 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1376 uinfo->count = chs == 3 ? 2 : 1;
1377 uinfo->value.integer.min = 0;
1378 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1379 if (!uinfo->value.integer.max) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001380 codec_warn(codec,
1381 "num_steps = 0 for NID=0x%x (ctl = %s)\n",
1382 nid, kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 return -EINVAL;
1384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 return 0;
1386}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001387EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001389
1390static inline unsigned int
1391read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1392 int ch, int dir, int idx, unsigned int ofs)
1393{
1394 unsigned int val;
1395 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1396 val &= HDA_AMP_VOLMASK;
1397 if (val >= ofs)
1398 val -= ofs;
1399 else
1400 val = 0;
1401 return val;
1402}
1403
1404static inline int
1405update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1406 int ch, int dir, int idx, unsigned int ofs,
1407 unsigned int val)
1408{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001409 unsigned int maxval;
1410
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001411 if (val > 0)
1412 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02001413 /* ofs = 0: raw max value */
1414 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001415 if (val > maxval)
1416 val = maxval;
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01001417 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1418 HDA_AMP_VOLMASK, val);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001419}
1420
Takashi Iwaid5191e52009-11-16 14:58:17 +01001421/**
1422 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001423 * @kcontrol: ctl element
1424 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01001425 *
1426 * The control element is supposed to have the private_value field
1427 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1428 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001429int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1430 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431{
1432 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1433 hda_nid_t nid = get_amp_nid(kcontrol);
1434 int chs = get_amp_channels(kcontrol);
1435 int dir = get_amp_direction(kcontrol);
1436 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001437 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 long *valp = ucontrol->value.integer.value;
1439
1440 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001441 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001443 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 return 0;
1445}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001446EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Takashi Iwaid5191e52009-11-16 14:58:17 +01001448/**
1449 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001450 * @kcontrol: ctl element
1451 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01001452 *
1453 * The control element is supposed to have the private_value field
1454 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1455 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001456int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1457 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458{
1459 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1460 hda_nid_t nid = get_amp_nid(kcontrol);
1461 int chs = get_amp_channels(kcontrol);
1462 int dir = get_amp_direction(kcontrol);
1463 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001464 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 long *valp = ucontrol->value.integer.value;
1466 int change = 0;
1467
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001468 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001469 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001470 valp++;
1471 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001472 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001473 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 return change;
1475}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001476EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Takashi Iwai99b5c5b2017-05-10 12:29:37 +02001478/* inquiry the amp caps and convert to TLV */
1479static void get_ctl_amp_tlv(struct snd_kcontrol *kcontrol, unsigned int *tlv)
1480{
1481 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1482 hda_nid_t nid = get_amp_nid(kcontrol);
1483 int dir = get_amp_direction(kcontrol);
1484 unsigned int ofs = get_amp_offset(kcontrol);
1485 bool min_mute = get_amp_min_mute(kcontrol);
1486 u32 caps, val1, val2;
1487
1488 caps = query_amp_caps(codec, nid, dir);
1489 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1490 val2 = (val2 + 1) * 25;
1491 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
1492 val1 += ofs;
1493 val1 = ((int)val1) * ((int)val2);
1494 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
1495 val2 |= TLV_DB_SCALE_MUTE;
1496 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1497 tlv[1] = 2 * sizeof(unsigned int);
1498 tlv[2] = val1;
1499 tlv[3] = val2;
1500}
1501
Takashi Iwaid5191e52009-11-16 14:58:17 +01001502/**
Takashi Iwaif0049e12017-05-10 12:34:09 +02001503 * snd_hda_mixer_amp_tlv - TLV callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001504 * @kcontrol: ctl element
1505 * @op_flag: operation flag
1506 * @size: byte size of input TLV
1507 * @_tlv: TLV data
Takashi Iwaid5191e52009-11-16 14:58:17 +01001508 *
1509 * The control element is supposed to have the private_value field
1510 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1511 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001512int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1513 unsigned int size, unsigned int __user *_tlv)
1514{
Takashi Iwai99b5c5b2017-05-10 12:29:37 +02001515 unsigned int tlv[4];
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001516
1517 if (size < 4 * sizeof(unsigned int))
1518 return -ENOMEM;
Takashi Iwai99b5c5b2017-05-10 12:29:37 +02001519 get_ctl_amp_tlv(kcontrol, tlv);
1520 if (copy_to_user(_tlv, tlv, sizeof(tlv)))
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001521 return -EFAULT;
1522 return 0;
1523}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001524EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001525
Takashi Iwaid5191e52009-11-16 14:58:17 +01001526/**
1527 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
1528 * @codec: HD-audio codec
1529 * @nid: NID of a reference widget
1530 * @dir: #HDA_INPUT or #HDA_OUTPUT
1531 * @tlv: TLV data to be stored, at least 4 elements
1532 *
1533 * Set (static) TLV data for a virtual master volume using the AMP caps
1534 * obtained from the reference NID.
1535 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01001536 */
1537void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1538 unsigned int *tlv)
1539{
1540 u32 caps;
1541 int nums, step;
1542
1543 caps = query_amp_caps(codec, nid, dir);
1544 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1545 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1546 step = (step + 1) * 25;
1547 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1548 tlv[1] = 2 * sizeof(unsigned int);
1549 tlv[2] = -nums * step;
1550 tlv[3] = step;
1551}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001552EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001553
1554/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01001555static struct snd_kcontrol *
Takashi Iwaidcda5802012-10-12 17:24:51 +02001556find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01001557{
1558 struct snd_ctl_elem_id id;
1559 memset(&id, 0, sizeof(id));
1560 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwaidcda5802012-10-12 17:24:51 +02001561 id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01001562 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02001563 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
1564 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01001565 strcpy(id.name, name);
Takashi Iwai6efdd852015-02-27 16:09:22 +01001566 return snd_ctl_find_id(codec->card, &id);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001567}
1568
Takashi Iwaid5191e52009-11-16 14:58:17 +01001569/**
1570 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
1571 * @codec: HD-audio codec
1572 * @name: ctl id name string
1573 *
1574 * Get the control element with the given id string and IFACE_MIXER.
1575 */
Takashi Iwai09f99702008-02-04 12:31:13 +01001576struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1577 const char *name)
1578{
Takashi Iwaidcda5802012-10-12 17:24:51 +02001579 return find_mixer_ctl(codec, name, 0, 0);
Takashi Iwai09f99702008-02-04 12:31:13 +01001580}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001581EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01001582
Takashi Iwaidcda5802012-10-12 17:24:51 +02001583static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01001584 int start_idx)
Takashi Iwai1afe2062010-12-23 10:17:52 +01001585{
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01001586 int i, idx;
1587 /* 16 ctlrs should be large enough */
1588 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
1589 if (!find_mixer_ctl(codec, name, 0, idx))
Takashi Iwai1afe2062010-12-23 10:17:52 +01001590 return idx;
1591 }
1592 return -EBUSY;
1593}
1594
Takashi Iwaid5191e52009-11-16 14:58:17 +01001595/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001596 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01001597 * @codec: HD-audio codec
1598 * @nid: corresponding NID (optional)
1599 * @kctl: the control element to assign
1600 *
1601 * Add the given control element to an array inside the codec instance.
1602 * All control elements belonging to a codec are supposed to be added
1603 * by this function so that a proper clean-up works at the free or
1604 * reconfiguration time.
1605 *
1606 * If non-zero @nid is passed, the NID is assigned to the control element.
1607 * The assignment is shown in the codec proc file.
1608 *
1609 * snd_hda_ctl_add() checks the control subdev id field whether
1610 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001611 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
1612 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01001613 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001614int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
1615 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02001616{
1617 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001618 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001619 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001620
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001621 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001622 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001623 if (nid == 0)
1624 nid = get_amp_nid_(kctl->private_value);
1625 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001626 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
1627 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001628 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01001629 kctl->id.subdevice = 0;
Takashi Iwai6efdd852015-02-27 16:09:22 +01001630 err = snd_ctl_add(codec->card, kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001631 if (err < 0)
1632 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001633 item = snd_array_new(&codec->mixers);
1634 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02001635 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001636 item->kctl = kctl;
1637 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001638 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001639 return 0;
1640}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001641EXPORT_SYMBOL_GPL(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001642
Takashi Iwaid5191e52009-11-16 14:58:17 +01001643/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001644 * snd_hda_add_nid - Assign a NID to a control element
1645 * @codec: HD-audio codec
1646 * @nid: corresponding NID (optional)
1647 * @kctl: the control element to assign
1648 * @index: index to kctl
1649 *
1650 * Add the given control element to an array inside the codec instance.
1651 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
1652 * NID:KCTL mapping - for example "Capture Source" selector.
1653 */
1654int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
1655 unsigned int index, hda_nid_t nid)
1656{
1657 struct hda_nid_item *item;
1658
1659 if (nid > 0) {
1660 item = snd_array_new(&codec->nids);
1661 if (!item)
1662 return -ENOMEM;
1663 item->kctl = kctl;
1664 item->index = index;
1665 item->nid = nid;
1666 return 0;
1667 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01001668 codec_err(codec, "no NID for mapping control %s:%d:%d\n",
1669 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001670 return -EINVAL;
1671}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001672EXPORT_SYMBOL_GPL(snd_hda_add_nid);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001673
1674/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01001675 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
1676 * @codec: HD-audio codec
1677 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02001678void snd_hda_ctls_clear(struct hda_codec *codec)
1679{
1680 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001681 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001682 for (i = 0; i < codec->mixers.used; i++)
Takashi Iwai6efdd852015-02-27 16:09:22 +01001683 snd_ctl_remove(codec->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001684 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001685 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001686}
1687
Takashi Iwai95a962c2014-10-29 16:03:58 +01001688/**
1689 * snd_hda_lock_devices - pseudo device locking
1690 * @bus: the BUS
1691 *
Takashi Iwaia65d6292009-02-23 16:57:04 +01001692 * toggle card->shutdown to allow/disallow the device access (as a hack)
1693 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001694int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001695{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001696 struct snd_card *card = bus->card;
1697 struct hda_codec *codec;
1698
Takashi Iwaia65d6292009-02-23 16:57:04 +01001699 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001700 if (card->shutdown)
1701 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01001702 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001703 if (!list_empty(&card->ctl_files))
1704 goto err_clear;
1705
Takashi Iwaid068ebc2015-03-02 23:22:59 +01001706 list_for_each_codec(codec, bus) {
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01001707 struct hda_pcm *cpcm;
1708 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001709 if (!cpcm->pcm)
1710 continue;
1711 if (cpcm->pcm->streams[0].substream_opened ||
1712 cpcm->pcm->streams[1].substream_opened)
1713 goto err_clear;
1714 }
1715 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01001716 spin_unlock(&card->files_lock);
1717 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01001718
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001719 err_clear:
1720 card->shutdown = 0;
1721 err_unlock:
1722 spin_unlock(&card->files_lock);
1723 return -EINVAL;
1724}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001725EXPORT_SYMBOL_GPL(snd_hda_lock_devices);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001726
Takashi Iwai95a962c2014-10-29 16:03:58 +01001727/**
1728 * snd_hda_unlock_devices - pseudo device unlocking
1729 * @bus: the BUS
1730 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001731void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01001732{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001733 struct snd_card *card = bus->card;
1734
Takashi Iwaia65d6292009-02-23 16:57:04 +01001735 spin_lock(&card->files_lock);
1736 card->shutdown = 0;
1737 spin_unlock(&card->files_lock);
1738}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001739EXPORT_SYMBOL_GPL(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01001740
Takashi Iwaid5191e52009-11-16 14:58:17 +01001741/**
1742 * snd_hda_codec_reset - Clear all objects assigned to the codec
1743 * @codec: HD-audio codec
1744 *
1745 * This frees the all PCM and control elements assigned to the codec, and
1746 * clears the caches and restores the pin default configurations.
1747 *
1748 * When a device is being used, it returns -EBSY. If successfully freed,
1749 * returns zero.
1750 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01001751int snd_hda_codec_reset(struct hda_codec *codec)
1752{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001753 struct hda_bus *bus = codec->bus;
Takashi Iwaia65d6292009-02-23 16:57:04 +01001754
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001755 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01001756 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01001757
1758 /* OK, let it free */
Takashi Iwai3256be62015-02-24 14:59:42 +01001759 snd_hdac_device_unregister(&codec->core);
Takashi Iwaid8a766a2015-02-17 15:25:37 +01001760
Takashi Iwaia65d6292009-02-23 16:57:04 +01001761 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001762 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01001763 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001764}
1765
Takashi Iwai6194b992014-06-06 18:12:16 +02001766typedef int (*map_slave_func_t)(struct hda_codec *, void *, struct snd_kcontrol *);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01001767
1768/* apply the function to all matching slave ctls in the mixer list */
1769static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01001770 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01001771{
1772 struct hda_nid_item *items;
1773 const char * const *s;
1774 int i, err;
1775
1776 items = codec->mixers.list;
1777 for (i = 0; i < codec->mixers.used; i++) {
1778 struct snd_kcontrol *sctl = items[i].kctl;
Takashi Iwaica16ec02013-10-28 12:00:35 +01001779 if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01001780 continue;
1781 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01001782 char tmpname[sizeof(sctl->id.name)];
1783 const char *name = *s;
1784 if (suffix) {
1785 snprintf(tmpname, sizeof(tmpname), "%s %s",
1786 name, suffix);
1787 name = tmpname;
1788 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01001789 if (!strcmp(sctl->id.name, name)) {
Takashi Iwai6194b992014-06-06 18:12:16 +02001790 err = func(codec, data, sctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01001791 if (err)
1792 return err;
1793 break;
1794 }
1795 }
1796 }
1797 return 0;
1798}
1799
Takashi Iwai6194b992014-06-06 18:12:16 +02001800static int check_slave_present(struct hda_codec *codec,
1801 void *data, struct snd_kcontrol *sctl)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01001802{
1803 return 1;
1804}
1805
Takashi Iwai18478e82012-03-09 17:51:10 +01001806/* guess the value corresponding to 0dB */
Takashi Iwai6194b992014-06-06 18:12:16 +02001807static int get_kctl_0dB_offset(struct hda_codec *codec,
1808 struct snd_kcontrol *kctl, int *step_to_check)
Takashi Iwai18478e82012-03-09 17:51:10 +01001809{
1810 int _tlv[4];
1811 const int *tlv = NULL;
1812 int val = -1;
1813
Takashi Iwai99b5c5b2017-05-10 12:29:37 +02001814 if ((kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) &&
1815 kctl->tlv.c == snd_hda_mixer_amp_tlv) {
1816 get_ctl_amp_tlv(kctl, _tlv);
1817 tlv = _tlv;
Takashi Iwai18478e82012-03-09 17:51:10 +01001818 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
1819 tlv = kctl->tlv.p;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01001820 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) {
1821 int step = tlv[3];
1822 step &= ~TLV_DB_SCALE_MUTE;
1823 if (!step)
1824 return -1;
Takashi Iwai485e3e0c2013-11-04 15:51:00 +01001825 if (*step_to_check && *step_to_check != step) {
Takashi Iwai6194b992014-06-06 18:12:16 +02001826 codec_err(codec, "Mismatching dB step for vmaster slave (%d!=%d)\n",
Takashi Iwai4e76a882014-02-25 12:21:03 +01001827- *step_to_check, step);
Takashi Iwai485e3e0c2013-11-04 15:51:00 +01001828 return -1;
1829 }
1830 *step_to_check = step;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01001831 val = -tlv[2] / step;
1832 }
Takashi Iwai18478e82012-03-09 17:51:10 +01001833 return val;
1834}
1835
1836/* call kctl->put with the given value(s) */
1837static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
1838{
1839 struct snd_ctl_elem_value *ucontrol;
1840 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
1841 if (!ucontrol)
1842 return -ENOMEM;
1843 ucontrol->value.integer.value[0] = val;
1844 ucontrol->value.integer.value[1] = val;
1845 kctl->put(kctl, ucontrol);
1846 kfree(ucontrol);
1847 return 0;
1848}
1849
1850/* initialize the slave volume with 0dB */
Takashi Iwai6194b992014-06-06 18:12:16 +02001851static int init_slave_0dB(struct hda_codec *codec,
1852 void *data, struct snd_kcontrol *slave)
Takashi Iwai18478e82012-03-09 17:51:10 +01001853{
Takashi Iwai6194b992014-06-06 18:12:16 +02001854 int offset = get_kctl_0dB_offset(codec, slave, data);
Takashi Iwai18478e82012-03-09 17:51:10 +01001855 if (offset > 0)
1856 put_kctl_with_value(slave, offset);
1857 return 0;
1858}
1859
1860/* unmute the slave */
Takashi Iwai6194b992014-06-06 18:12:16 +02001861static int init_slave_unmute(struct hda_codec *codec,
1862 void *data, struct snd_kcontrol *slave)
Takashi Iwai18478e82012-03-09 17:51:10 +01001863{
1864 return put_kctl_with_value(slave, 1);
1865}
1866
Takashi Iwaie8750942014-06-30 14:02:39 +02001867static int add_slave(struct hda_codec *codec,
1868 void *data, struct snd_kcontrol *slave)
1869{
1870 return snd_ctl_add_slave(data, slave);
1871}
1872
Takashi Iwaid5191e52009-11-16 14:58:17 +01001873/**
Takashi Iwai95a962c2014-10-29 16:03:58 +01001874 * __snd_hda_add_vmaster - create a virtual master control and add slaves
Takashi Iwaid5191e52009-11-16 14:58:17 +01001875 * @codec: HD-audio codec
1876 * @name: vmaster control name
1877 * @tlv: TLV data (optional)
1878 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01001879 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01001880 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01001881 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01001882 *
1883 * Create a virtual master control with the given name. The TLV data
1884 * must be either NULL or a valid data.
1885 *
1886 * @slaves is a NULL-terminated array of strings, each of which is a
1887 * slave control name. All controls with these names are assigned to
1888 * the new virtual master control.
1889 *
1890 * This function returns zero if successful or a negative error code.
1891 */
Takashi Iwai18478e82012-03-09 17:51:10 +01001892int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01001893 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01001894 const char *suffix, bool init_slave_vol,
1895 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01001896{
1897 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01001898 int err;
1899
Takashi Iwai29e58532012-03-12 12:25:03 +01001900 if (ctl_ret)
1901 *ctl_ret = NULL;
1902
Takashi Iwai9322ca52012-02-03 14:28:01 +01001903 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01001904 if (err != 1) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001905 codec_dbg(codec, "No slave found for %s\n", name);
Takashi Iwai2f085542008-02-22 18:43:50 +01001906 return 0;
1907 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01001908 kctl = snd_ctl_make_virtual_master(name, tlv);
1909 if (!kctl)
1910 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001911 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001912 if (err < 0)
1913 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001914
Takashi Iwaie8750942014-06-30 14:02:39 +02001915 err = map_slaves(codec, slaves, suffix, add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01001916 if (err < 0)
1917 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01001918
1919 /* init with master mute & zero volume */
1920 put_kctl_with_value(kctl, 0);
Takashi Iwai485e3e0c2013-11-04 15:51:00 +01001921 if (init_slave_vol) {
1922 int step = 0;
Takashi Iwai18478e82012-03-09 17:51:10 +01001923 map_slaves(codec, slaves, suffix,
Takashi Iwai485e3e0c2013-11-04 15:51:00 +01001924 tlv ? init_slave_0dB : init_slave_unmute, &step);
1925 }
Takashi Iwai18478e82012-03-09 17:51:10 +01001926
Takashi Iwai29e58532012-03-12 12:25:03 +01001927 if (ctl_ret)
1928 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01001929 return 0;
1930}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001931EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001932
Takashi Iwaid2f344b2012-03-12 16:59:58 +01001933/*
1934 * mute-LED control using vmaster
1935 */
1936static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
1937 struct snd_ctl_elem_info *uinfo)
1938{
1939 static const char * const texts[] = {
David Henningssonc86c2d42013-01-03 14:12:29 +01001940 "On", "Off", "Follow Master"
Takashi Iwaid2f344b2012-03-12 16:59:58 +01001941 };
Takashi Iwaid2f344b2012-03-12 16:59:58 +01001942
Takashi Iwai3ff72212014-10-20 18:17:28 +02001943 return snd_ctl_enum_info(uinfo, 1, 3, texts);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01001944}
1945
1946static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
1947 struct snd_ctl_elem_value *ucontrol)
1948{
1949 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
1950 ucontrol->value.enumerated.item[0] = hook->mute_mode;
1951 return 0;
1952}
1953
1954static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
1955 struct snd_ctl_elem_value *ucontrol)
1956{
1957 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
1958 unsigned int old_mode = hook->mute_mode;
1959
1960 hook->mute_mode = ucontrol->value.enumerated.item[0];
1961 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
1962 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
1963 if (old_mode == hook->mute_mode)
1964 return 0;
1965 snd_hda_sync_vmaster_hook(hook);
1966 return 1;
1967}
1968
Bhumika Goyalf3b827e2017-02-20 00:18:09 +05301969static const struct snd_kcontrol_new vmaster_mute_mode = {
Takashi Iwaid2f344b2012-03-12 16:59:58 +01001970 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1971 .name = "Mute-LED Mode",
1972 .info = vmaster_mute_mode_info,
1973 .get = vmaster_mute_mode_get,
1974 .put = vmaster_mute_mode_put,
1975};
1976
Takashi Iwaiee52e562015-04-27 10:36:11 +02001977/* meta hook to call each driver's vmaster hook */
1978static void vmaster_hook(void *private_data, int enabled)
1979{
1980 struct hda_vmaster_mute_hook *hook = private_data;
1981
1982 if (hook->mute_mode != HDA_VMUTE_FOLLOW_MASTER)
1983 enabled = hook->mute_mode;
1984 hook->hook(hook->codec, enabled);
1985}
1986
Takashi Iwai95a962c2014-10-29 16:03:58 +01001987/**
1988 * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED
1989 * @codec: the HDA codec
1990 * @hook: the vmaster hook object
1991 * @expose_enum_ctl: flag to create an enum ctl
1992 *
1993 * Add a mute-LED hook with the given vmaster switch kctl.
1994 * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically
1995 * created and associated with the given hook.
Takashi Iwaid2f344b2012-03-12 16:59:58 +01001996 */
1997int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735cb2012-03-13 07:55:10 +01001998 struct hda_vmaster_mute_hook *hook,
1999 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002000{
2001 struct snd_kcontrol *kctl;
2002
2003 if (!hook->hook || !hook->sw_kctl)
2004 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002005 hook->codec = codec;
2006 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaiee52e562015-04-27 10:36:11 +02002007 snd_ctl_add_vmaster_hook(hook->sw_kctl, vmaster_hook, hook);
Takashi Iwaif29735cb2012-03-13 07:55:10 +01002008 if (!expose_enum_ctl)
2009 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002010 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2011 if (!kctl)
2012 return -ENOMEM;
2013 return snd_hda_ctl_add(codec, 0, kctl);
2014}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002015EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002016
Takashi Iwai95a962c2014-10-29 16:03:58 +01002017/**
2018 * snd_hda_sync_vmaster_hook - Sync vmaster hook
2019 * @hook: the vmaster hook
2020 *
2021 * Call the hook with the current value for synchronization.
2022 * Should be called in init callback.
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002023 */
2024void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2025{
2026 if (!hook->hook || !hook->codec)
2027 return;
Takashi Iwai594813f2013-04-17 18:16:05 +02002028 /* don't call vmaster hook in the destructor since it might have
2029 * been already destroyed
2030 */
2031 if (hook->codec->bus->shutdown)
2032 return;
Takashi Iwaiee52e562015-04-27 10:36:11 +02002033 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002034}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002035EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002036
2037
Takashi Iwaid5191e52009-11-16 14:58:17 +01002038/**
2039 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002040 * @kcontrol: referred ctl element
2041 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002042 *
2043 * The control element is supposed to have the private_value field
2044 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2045 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002046int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2047 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048{
2049 int chs = get_amp_channels(kcontrol);
2050
2051 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2052 uinfo->count = chs == 3 ? 2 : 1;
2053 uinfo->value.integer.min = 0;
2054 uinfo->value.integer.max = 1;
2055 return 0;
2056}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002057EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
Takashi Iwaid5191e52009-11-16 14:58:17 +01002059/**
2060 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002061 * @kcontrol: ctl element
2062 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002063 *
2064 * The control element is supposed to have the private_value field
2065 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2066 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002067int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2068 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069{
2070 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2071 hda_nid_t nid = get_amp_nid(kcontrol);
2072 int chs = get_amp_channels(kcontrol);
2073 int dir = get_amp_direction(kcontrol);
2074 int idx = get_amp_index(kcontrol);
2075 long *valp = ucontrol->value.integer.value;
2076
2077 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002078 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002079 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002081 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002082 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 return 0;
2084}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002085EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
Takashi Iwaid5191e52009-11-16 14:58:17 +01002087/**
2088 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002089 * @kcontrol: ctl element
2090 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002091 *
2092 * The control element is supposed to have the private_value field
2093 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2094 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002095int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2096 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097{
2098 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2099 hda_nid_t nid = get_amp_nid(kcontrol);
2100 int chs = get_amp_channels(kcontrol);
2101 int dir = get_amp_direction(kcontrol);
2102 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 long *valp = ucontrol->value.integer.value;
2104 int change = 0;
2105
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002106 if (chs & 1) {
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01002107 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
2108 HDA_AMP_MUTE,
2109 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002110 valp++;
2111 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002112 if (chs & 2)
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01002113 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
2114 HDA_AMP_MUTE,
2115 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002116 hda_call_check_power_status(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 return change;
2118}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002119EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
Takashi Iwai532d5382007-07-27 19:02:40 +02002121/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 * SPDIF out controls
2123 */
2124
Takashi Iwai0ba21762007-04-16 11:29:14 +02002125static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2126 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127{
2128 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2129 uinfo->count = 1;
2130 return 0;
2131}
2132
Takashi Iwai0ba21762007-04-16 11:29:14 +02002133static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2134 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135{
2136 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2137 IEC958_AES0_NONAUDIO |
2138 IEC958_AES0_CON_EMPHASIS_5015 |
2139 IEC958_AES0_CON_NOT_COPYRIGHT;
2140 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2141 IEC958_AES1_CON_ORIGINAL;
2142 return 0;
2143}
2144
Takashi Iwai0ba21762007-04-16 11:29:14 +02002145static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2146 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147{
2148 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2149 IEC958_AES0_NONAUDIO |
2150 IEC958_AES0_PRO_EMPHASIS_5015;
2151 return 0;
2152}
2153
Takashi Iwai0ba21762007-04-16 11:29:14 +02002154static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2155 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156{
2157 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06002158 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002159 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002161 mutex_lock(&codec->spdif_mutex);
2162 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06002163 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2164 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2165 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2166 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002167 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168
2169 return 0;
2170}
2171
2172/* convert from SPDIF status bits to HDA SPDIF bits
2173 * bit 0 (DigEn) is always set zero (to be filled later)
2174 */
2175static unsigned short convert_from_spdif_status(unsigned int sbits)
2176{
2177 unsigned short val = 0;
2178
2179 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002180 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002182 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002184 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2185 IEC958_AES0_PRO_EMPHASIS_5015)
2186 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002188 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2189 IEC958_AES0_CON_EMPHASIS_5015)
2190 val |= AC_DIG1_EMPHASIS;
2191 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2192 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002194 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2196 }
2197 return val;
2198}
2199
2200/* convert to SPDIF status bits from HDA SPDIF bits
2201 */
2202static unsigned int convert_to_spdif_status(unsigned short val)
2203{
2204 unsigned int sbits = 0;
2205
Takashi Iwai0ba21762007-04-16 11:29:14 +02002206 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002208 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 sbits |= IEC958_AES0_PROFESSIONAL;
2210 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwaia686fd12013-03-20 15:42:00 +01002211 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2213 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002214 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002216 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002218 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2220 sbits |= val & (0x7f << 8);
2221 }
2222 return sbits;
2223}
2224
Takashi Iwai2f728532008-09-25 16:32:41 +02002225/* set digital convert verbs both for the given NID and its slaves */
2226static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaia551d912015-02-26 12:34:49 +01002227 int mask, int val)
Takashi Iwai2f728532008-09-25 16:32:41 +02002228{
Takashi Iwaidda14412011-05-02 11:29:30 +02002229 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02002230
Takashi Iwaia551d912015-02-26 12:34:49 +01002231 snd_hdac_regmap_update(&codec->core, nid, AC_VERB_SET_DIGI_CONVERT_1,
2232 mask, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002233 d = codec->slave_dig_outs;
2234 if (!d)
2235 return;
2236 for (; *d; d++)
Takashi Iwai7d4b5e92015-04-16 15:14:53 +02002237 snd_hdac_regmap_update(&codec->core, *d,
Takashi Iwaia551d912015-02-26 12:34:49 +01002238 AC_VERB_SET_DIGI_CONVERT_1, mask, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002239}
2240
2241static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2242 int dig1, int dig2)
2243{
Takashi Iwaia551d912015-02-26 12:34:49 +01002244 unsigned int mask = 0;
2245 unsigned int val = 0;
2246
2247 if (dig1 != -1) {
2248 mask |= 0xff;
2249 val = dig1;
2250 }
2251 if (dig2 != -1) {
2252 mask |= 0xff00;
2253 val |= dig2 << 8;
2254 }
2255 set_dig_out(codec, nid, mask, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002256}
2257
Takashi Iwai0ba21762007-04-16 11:29:14 +02002258static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2259 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260{
2261 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06002262 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002263 struct hda_spdif_out *spdif;
2264 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 unsigned short val;
2266 int change;
2267
Ingo Molnar62932df2006-01-16 16:34:20 +01002268 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002269 spdif = snd_array_elem(&codec->spdif_out, idx);
2270 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06002271 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2273 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2274 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c935972011-06-01 11:14:17 -06002275 val = convert_from_spdif_status(spdif->status);
2276 val |= spdif->ctls & 1;
2277 change = spdif->ctls != val;
2278 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06002279 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02002280 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01002281 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 return change;
2283}
2284
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002285#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286
Takashi Iwai0ba21762007-04-16 11:29:14 +02002287static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2288 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289{
2290 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06002291 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002292 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002294 mutex_lock(&codec->spdif_mutex);
2295 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06002296 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002297 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 return 0;
2299}
2300
Stephen Warren74b654c2011-06-01 11:14:18 -06002301static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
2302 int dig1, int dig2)
2303{
2304 set_dig_out_convert(codec, nid, dig1, dig2);
2305 /* unmute amp switch (if any) */
2306 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
2307 (dig1 & AC_DIG1_ENABLE))
2308 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2309 HDA_AMP_MUTE, 0);
2310}
2311
Takashi Iwai0ba21762007-04-16 11:29:14 +02002312static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2313 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314{
2315 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06002316 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002317 struct hda_spdif_out *spdif;
2318 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 unsigned short val;
2320 int change;
2321
Ingo Molnar62932df2006-01-16 16:34:20 +01002322 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002323 spdif = snd_array_elem(&codec->spdif_out, idx);
2324 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06002325 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02002327 val |= AC_DIG1_ENABLE;
Stephen Warren7c935972011-06-01 11:14:17 -06002328 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06002329 spdif->ctls = val;
2330 if (change && nid != (u16)-1)
2331 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01002332 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 return change;
2334}
2335
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002336static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 {
2338 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2339 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002340 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 .info = snd_hda_spdif_mask_info,
2342 .get = snd_hda_spdif_cmask_get,
2343 },
2344 {
2345 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2346 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002347 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 .info = snd_hda_spdif_mask_info,
2349 .get = snd_hda_spdif_pmask_get,
2350 },
2351 {
2352 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002353 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 .info = snd_hda_spdif_mask_info,
2355 .get = snd_hda_spdif_default_get,
2356 .put = snd_hda_spdif_default_put,
2357 },
2358 {
2359 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002360 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 .info = snd_hda_spdif_out_switch_info,
2362 .get = snd_hda_spdif_out_switch_get,
2363 .put = snd_hda_spdif_out_switch_put,
2364 },
2365 { } /* end */
2366};
2367
2368/**
Takashi Iwaidcda5802012-10-12 17:24:51 +02002369 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 * @codec: the HDA codec
Takashi Iwaidcda5802012-10-12 17:24:51 +02002371 * @associated_nid: NID that new ctls associated with
2372 * @cvt_nid: converter NID
2373 * @type: HDA_PCM_TYPE_*
2374 * Creates controls related with the digital output.
2375 * Called from each patch supporting the digital out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 *
2377 * Returns 0 if successful, or a negative error code.
2378 */
Takashi Iwaidcda5802012-10-12 17:24:51 +02002379int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
2380 hda_nid_t associated_nid,
2381 hda_nid_t cvt_nid,
2382 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383{
2384 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002385 struct snd_kcontrol *kctl;
2386 struct snd_kcontrol_new *dig_mix;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002387 int idx = 0;
Takashi Iwaia551d912015-02-26 12:34:49 +01002388 int val = 0;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002389 const int spdif_index = 16;
Stephen Warren7c935972011-06-01 11:14:17 -06002390 struct hda_spdif_out *spdif;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002391 struct hda_bus *bus = codec->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002393 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02002394 type == HDA_PCM_TYPE_SPDIF) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002395 idx = spdif_index;
2396 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02002397 type == HDA_PCM_TYPE_HDMI) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002398 /* suppose a single SPDIF device */
2399 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2400 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
2401 if (!kctl)
2402 break;
2403 kctl->id.index = spdif_index;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002404 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002405 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002406 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002407 if (!bus->primary_dig_out_type)
2408 bus->primary_dig_out_type = type;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002409
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002410 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
Takashi Iwai1afe2062010-12-23 10:17:52 +01002411 if (idx < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002412 codec_err(codec, "too many IEC958 outputs\n");
Takashi Iwai09f99702008-02-04 12:31:13 +01002413 return -EBUSY;
2414 }
Stephen Warren7c935972011-06-01 11:14:17 -06002415 spdif = snd_array_new(&codec->spdif_out);
Mengdong Lin25336e82013-03-07 14:10:25 -05002416 if (!spdif)
2417 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2419 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01002420 if (!kctl)
2421 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01002422 kctl->id.index = idx;
Stephen Warren7c935972011-06-01 11:14:17 -06002423 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06002424 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002425 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 return err;
2427 }
Stephen Warren74b654c2011-06-01 11:14:18 -06002428 spdif->nid = cvt_nid;
Takashi Iwaia551d912015-02-26 12:34:49 +01002429 snd_hdac_regmap_read(&codec->core, cvt_nid,
2430 AC_VERB_GET_DIGI_CONVERT_1, &val);
2431 spdif->ctls = val;
Stephen Warren7c935972011-06-01 11:14:17 -06002432 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 return 0;
2434}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002435EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436
Takashi Iwai95a962c2014-10-29 16:03:58 +01002437/**
2438 * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID
2439 * @codec: the HDA codec
2440 * @nid: widget NID
2441 *
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002442 * call within spdif_mutex lock
2443 */
Stephen Warren7c935972011-06-01 11:14:17 -06002444struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
2445 hda_nid_t nid)
2446{
2447 int i;
2448 for (i = 0; i < codec->spdif_out.used; i++) {
2449 struct hda_spdif_out *spdif =
2450 snd_array_elem(&codec->spdif_out, i);
2451 if (spdif->nid == nid)
2452 return spdif;
2453 }
2454 return NULL;
2455}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002456EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid);
Stephen Warren7c935972011-06-01 11:14:17 -06002457
Takashi Iwai95a962c2014-10-29 16:03:58 +01002458/**
2459 * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl
2460 * @codec: the HDA codec
2461 * @idx: the SPDIF ctl index
2462 *
2463 * Unassign the widget from the given SPDIF control.
2464 */
Stephen Warren74b654c2011-06-01 11:14:18 -06002465void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
2466{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002467 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06002468
2469 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002470 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06002471 spdif->nid = (u16)-1;
2472 mutex_unlock(&codec->spdif_mutex);
2473}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002474EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign);
Stephen Warren74b654c2011-06-01 11:14:18 -06002475
Takashi Iwai95a962c2014-10-29 16:03:58 +01002476/**
2477 * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID
2478 * @codec: the HDA codec
2479 * @idx: the SPDIF ctl idx
2480 * @nid: widget NID
2481 *
2482 * Assign the widget to the SPDIF control with the given index.
2483 */
Stephen Warren74b654c2011-06-01 11:14:18 -06002484void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
2485{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002486 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06002487 unsigned short val;
2488
2489 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002490 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06002491 if (spdif->nid != nid) {
2492 spdif->nid = nid;
2493 val = spdif->ctls;
2494 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
2495 }
2496 mutex_unlock(&codec->spdif_mutex);
2497}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002498EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign);
Stephen Warren74b654c2011-06-01 11:14:18 -06002499
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500/*
Takashi Iwai9a081602008-02-12 18:37:26 +01002501 * SPDIF sharing with analog output
2502 */
2503static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2504 struct snd_ctl_elem_value *ucontrol)
2505{
2506 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2507 ucontrol->value.integer.value[0] = mout->share_spdif;
2508 return 0;
2509}
2510
2511static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2512 struct snd_ctl_elem_value *ucontrol)
2513{
2514 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2515 mout->share_spdif = !!ucontrol->value.integer.value[0];
2516 return 0;
2517}
2518
Bhumika Goyalf3b827e2017-02-20 00:18:09 +05302519static const struct snd_kcontrol_new spdif_share_sw = {
Takashi Iwai9a081602008-02-12 18:37:26 +01002520 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2521 .name = "IEC958 Default PCM Playback Switch",
2522 .info = snd_ctl_boolean_mono_info,
2523 .get = spdif_share_sw_get,
2524 .put = spdif_share_sw_put,
2525};
2526
Takashi Iwaid5191e52009-11-16 14:58:17 +01002527/**
2528 * snd_hda_create_spdif_share_sw - create Default PCM switch
2529 * @codec: the HDA codec
2530 * @mout: multi-out instance
2531 */
Takashi Iwai9a081602008-02-12 18:37:26 +01002532int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2533 struct hda_multi_out *mout)
2534{
Mengdong Lin4c7a5482013-03-07 14:11:05 -05002535 struct snd_kcontrol *kctl;
2536
Takashi Iwai9a081602008-02-12 18:37:26 +01002537 if (!mout->dig_out_nid)
2538 return 0;
Mengdong Lin4c7a5482013-03-07 14:11:05 -05002539
2540 kctl = snd_ctl_new1(&spdif_share_sw, mout);
2541 if (!kctl)
2542 return -ENOMEM;
Takashi Iwai9a081602008-02-12 18:37:26 +01002543 /* ATTENTION: here mout is passed as private_data, instead of codec */
Mengdong Lin4c7a5482013-03-07 14:11:05 -05002544 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
Takashi Iwai9a081602008-02-12 18:37:26 +01002545}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002546EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01002547
2548/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 * SPDIF input
2550 */
2551
2552#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2553
Takashi Iwai0ba21762007-04-16 11:29:14 +02002554static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
2555 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556{
2557 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2558
2559 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
2560 return 0;
2561}
2562
Takashi Iwai0ba21762007-04-16 11:29:14 +02002563static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
2564 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565{
2566 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2567 hda_nid_t nid = kcontrol->private_value;
2568 unsigned int val = !!ucontrol->value.integer.value[0];
2569 int change;
2570
Ingo Molnar62932df2006-01-16 16:34:20 +01002571 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002573 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 codec->spdif_in_enable = val;
Takashi Iwaia551d912015-02-26 12:34:49 +01002575 snd_hdac_regmap_write(&codec->core, nid,
2576 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002578 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 return change;
2580}
2581
Takashi Iwai0ba21762007-04-16 11:29:14 +02002582static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
2583 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584{
2585 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2586 hda_nid_t nid = kcontrol->private_value;
Takashi Iwaia551d912015-02-26 12:34:49 +01002587 unsigned int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 unsigned int sbits;
2589
Takashi Iwaia551d912015-02-26 12:34:49 +01002590 snd_hdac_regmap_read(&codec->core, nid,
2591 AC_VERB_GET_DIGI_CONVERT_1, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 sbits = convert_to_spdif_status(val);
2593 ucontrol->value.iec958.status[0] = sbits;
2594 ucontrol->value.iec958.status[1] = sbits >> 8;
2595 ucontrol->value.iec958.status[2] = sbits >> 16;
2596 ucontrol->value.iec958.status[3] = sbits >> 24;
2597 return 0;
2598}
2599
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002600static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 {
2602 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002603 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 .info = snd_hda_spdif_in_switch_info,
2605 .get = snd_hda_spdif_in_switch_get,
2606 .put = snd_hda_spdif_in_switch_put,
2607 },
2608 {
2609 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2610 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002611 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 .info = snd_hda_spdif_mask_info,
2613 .get = snd_hda_spdif_in_status_get,
2614 },
2615 { } /* end */
2616};
2617
2618/**
2619 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
2620 * @codec: the HDA codec
2621 * @nid: audio in widget NID
2622 *
2623 * Creates controls related with the SPDIF input.
2624 * Called from each patch supporting the SPDIF in.
2625 *
2626 * Returns 0 if successful, or a negative error code.
2627 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002628int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629{
2630 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002631 struct snd_kcontrol *kctl;
2632 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01002633 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634
Takashi Iwaidcda5802012-10-12 17:24:51 +02002635 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01002636 if (idx < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002637 codec_err(codec, "too many IEC958 inputs\n");
Takashi Iwai09f99702008-02-04 12:31:13 +01002638 return -EBUSY;
2639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
2641 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01002642 if (!kctl)
2643 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002645 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002646 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 return err;
2648 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002649 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01002650 snd_hda_codec_read(codec, nid, 0,
2651 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02002652 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 return 0;
2654}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002655EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656
Takashi Iwai95a962c2014-10-29 16:03:58 +01002657/**
2658 * snd_hda_codec_set_power_to_all - Set the power state to all widgets
2659 * @codec: the HDA codec
2660 * @fg: function group (not used now)
2661 * @power_state: the power state to set (AC_PWRST_*)
2662 *
2663 * Set the given power state to all widgets that have the power control.
2664 * If the codec has power_filter set, it evaluates the power state and
2665 * filter out if it's unchanged as D3.
2666 */
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002667void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
Takashi Iwai9419ab62013-01-24 17:23:35 +01002668 unsigned int power_state)
Takashi Iwai54d17402005-11-21 16:33:22 +01002669{
Takashi Iwai7639a062015-03-03 10:07:24 +01002670 hda_nid_t nid;
Takashi Iwai54d17402005-11-21 16:33:22 +01002671
Takashi Iwai7639a062015-03-03 10:07:24 +01002672 for_each_hda_codec_node(nid, codec) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01002673 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9419ab62013-01-24 17:23:35 +01002674 unsigned int state = power_state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002675 if (!(wcaps & AC_WCAP_POWER))
2676 continue;
Takashi Iwai9419ab62013-01-24 17:23:35 +01002677 if (codec->power_filter) {
2678 state = codec->power_filter(codec, nid, power_state);
2679 if (state != power_state && power_state == AC_PWRST_D3)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002680 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02002681 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002682 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai9419ab62013-01-24 17:23:35 +01002683 state);
Takashi Iwai54d17402005-11-21 16:33:22 +01002684 }
Takashi Iwai54d17402005-11-21 16:33:22 +01002685}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002686EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002687
2688/*
Takashi Iwai432c6412012-08-28 09:59:20 -07002689 * wait until the state is reached, returns the current state
2690 */
2691static unsigned int hda_sync_power_state(struct hda_codec *codec,
2692 hda_nid_t fg,
2693 unsigned int power_state)
2694{
2695 unsigned long end_time = jiffies + msecs_to_jiffies(500);
2696 unsigned int state, actual_state;
2697
2698 for (;;) {
2699 state = snd_hda_codec_read(codec, fg, 0,
2700 AC_VERB_GET_POWER_STATE, 0);
2701 if (state & AC_PWRST_ERROR)
2702 break;
2703 actual_state = (state >> 4) & 0x0f;
2704 if (actual_state == power_state)
2705 break;
2706 if (time_after_eq(jiffies, end_time))
2707 break;
2708 /* wait until the codec reachs to the target state */
2709 msleep(1);
2710 }
2711 return state;
2712}
2713
Takashi Iwai95a962c2014-10-29 16:03:58 +01002714/**
2715 * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD
2716 * @codec: the HDA codec
2717 * @nid: widget NID
2718 * @power_state: power state to evalue
2719 *
2720 * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set.
2721 * This can be used a codec power_filter callback.
2722 */
Takashi Iwaiba615b82013-03-13 14:47:21 +01002723unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
2724 hda_nid_t nid,
2725 unsigned int power_state)
Takashi Iwai9419ab62013-01-24 17:23:35 +01002726{
Takashi Iwai7639a062015-03-03 10:07:24 +01002727 if (nid == codec->core.afg || nid == codec->core.mfg)
Takashi Iwaidfc6e462014-01-13 16:09:57 +01002728 return power_state;
Takashi Iwai9419ab62013-01-24 17:23:35 +01002729 if (power_state == AC_PWRST_D3 &&
2730 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
2731 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
2732 int eapd = snd_hda_codec_read(codec, nid, 0,
2733 AC_VERB_GET_EAPD_BTLENABLE, 0);
2734 if (eapd & 0x02)
2735 return AC_PWRST_D0;
2736 }
2737 return power_state;
2738}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002739EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter);
Takashi Iwai9419ab62013-01-24 17:23:35 +01002740
Takashi Iwai432c6412012-08-28 09:59:20 -07002741/*
Takashi Iwai08fa20a2012-08-31 07:46:56 -07002742 * set power state of the codec, and return the power state
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002743 */
Takashi Iwaid8193872012-08-31 07:54:38 -07002744static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwai08fa20a2012-08-31 07:46:56 -07002745 unsigned int power_state)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002746{
Takashi Iwai7639a062015-03-03 10:07:24 +01002747 hda_nid_t fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
Wang Xingchao09617ce2012-06-08 10:26:08 +08002748 int count;
2749 unsigned int state;
Takashi Iwai63e51fd72013-06-06 14:20:19 +02002750 int flags = 0;
Wang Xingchao09617ce2012-06-08 10:26:08 +08002751
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002752 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08002753 if (power_state == AC_PWRST_D3) {
Mengdong Lin7f132922013-11-29 01:48:45 -05002754 if (codec->depop_delay < 0)
Takashi Iwai7639a062015-03-03 10:07:24 +01002755 msleep(codec_has_epss(codec) ? 10 : 100);
Mengdong Lin7f132922013-11-29 01:48:45 -05002756 else if (codec->depop_delay > 0)
2757 msleep(codec->depop_delay);
Takashi Iwai63e51fd72013-06-06 14:20:19 +02002758 flags = HDA_RW_NO_RESPONSE_FALLBACK;
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08002759 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08002760
2761 /* repeat power states setting at most 10 times*/
2762 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07002763 if (codec->patch_ops.set_power_state)
2764 codec->patch_ops.set_power_state(codec, fg,
2765 power_state);
2766 else {
Takashi Iwaidfc6e462014-01-13 16:09:57 +01002767 state = power_state;
2768 if (codec->power_filter)
2769 state = codec->power_filter(codec, fg, state);
2770 if (state == power_state || power_state != AC_PWRST_D3)
2771 snd_hda_codec_read(codec, fg, flags,
2772 AC_VERB_SET_POWER_STATE,
2773 state);
Takashi Iwai9419ab62013-01-24 17:23:35 +01002774 snd_hda_codec_set_power_to_all(codec, fg, power_state);
Takashi Iwai432c6412012-08-28 09:59:20 -07002775 }
2776 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08002777 if (!(state & AC_PWRST_ERROR))
2778 break;
2779 }
Mengdong Linb8dfc4622012-08-23 17:32:30 +08002780
Takashi Iwai08fa20a2012-08-31 07:46:56 -07002781 return state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002782}
Takashi Iwai54d17402005-11-21 16:33:22 +01002783
Takashi Iwaib9c590b2013-01-24 17:27:32 +01002784/* sync power states of all widgets;
2785 * this is called at the end of codec parsing
2786 */
2787static void sync_power_up_states(struct hda_codec *codec)
2788{
Takashi Iwai7639a062015-03-03 10:07:24 +01002789 hda_nid_t nid;
Takashi Iwaib9c590b2013-01-24 17:27:32 +01002790
Takashi Iwaiba615b82013-03-13 14:47:21 +01002791 /* don't care if no filter is used */
2792 if (!codec->power_filter)
Takashi Iwaib9c590b2013-01-24 17:27:32 +01002793 return;
2794
Takashi Iwai7639a062015-03-03 10:07:24 +01002795 for_each_hda_codec_node(nid, codec) {
Takashi Iwaib9c590b2013-01-24 17:27:32 +01002796 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9040d102013-01-24 17:47:17 +01002797 unsigned int target;
Takashi Iwaib9c590b2013-01-24 17:27:32 +01002798 if (!(wcaps & AC_WCAP_POWER))
2799 continue;
2800 target = codec->power_filter(codec, nid, AC_PWRST_D0);
2801 if (target == AC_PWRST_D0)
2802 continue;
Takashi Iwai9040d102013-01-24 17:47:17 +01002803 if (!snd_hda_check_power_state(codec, nid, target))
Takashi Iwaib9c590b2013-01-24 17:27:32 +01002804 snd_hda_codec_write(codec, nid, 0,
2805 AC_VERB_SET_POWER_STATE, target);
2806 }
2807}
2808
Takashi Iwai648a8d22014-02-25 10:38:13 +01002809#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai11aeff02008-07-30 15:01:46 +02002810/* execute additional init verbs */
2811static void hda_exec_init_verbs(struct hda_codec *codec)
2812{
2813 if (codec->init_verbs.list)
2814 snd_hda_sequence_write(codec, codec->init_verbs.list);
2815}
2816#else
2817static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
2818#endif
2819
Takashi Iwai2a439522011-07-26 09:52:50 +02002820#ifdef CONFIG_PM
Takashi Iwaicc72da72015-02-19 16:00:22 +01002821/* update the power on/off account with the current jiffies */
2822static void update_power_acct(struct hda_codec *codec, bool on)
2823{
2824 unsigned long delta = jiffies - codec->power_jiffies;
2825
2826 if (on)
2827 codec->power_on_acct += delta;
2828 else
2829 codec->power_off_acct += delta;
2830 codec->power_jiffies += delta;
2831}
2832
2833void snd_hda_update_power_acct(struct hda_codec *codec)
2834{
2835 update_power_acct(codec, hda_codec_is_power_on(codec));
2836}
2837
Takashi Iwaicb53c622007-08-10 17:21:45 +02002838/*
2839 * call suspend and power-down; used both from PM and power-save
Takashi Iwai08fa20a2012-08-31 07:46:56 -07002840 * this function returns the power state in the end
Takashi Iwaicb53c622007-08-10 17:21:45 +02002841 */
Takashi Iwaicc72da72015-02-19 16:00:22 +01002842static unsigned int hda_call_codec_suspend(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02002843{
Takashi Iwai08fa20a2012-08-31 07:46:56 -07002844 unsigned int state;
2845
Takashi Iwai7639a062015-03-03 10:07:24 +01002846 atomic_inc(&codec->core.in_pm);
Takashi Iwai989c3182012-11-19 14:14:58 +01002847
Takashi Iwaicb53c622007-08-10 17:21:45 +02002848 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002849 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02002850 hda_cleanup_all_streams(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07002851 state = hda_set_power_state(codec, AC_PWRST_D3);
Takashi Iwaicc72da72015-02-19 16:00:22 +01002852 update_power_acct(codec, true);
Takashi Iwai7639a062015-03-03 10:07:24 +01002853 atomic_dec(&codec->core.in_pm);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07002854 return state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002855}
2856
2857/*
2858 * kick up codec; used both from PM and power-save
2859 */
2860static void hda_call_codec_resume(struct hda_codec *codec)
2861{
Takashi Iwai7639a062015-03-03 10:07:24 +01002862 atomic_inc(&codec->core.in_pm);
Takashi Iwai989c3182012-11-19 14:14:58 +01002863
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01002864 if (codec->core.regmap)
2865 regcache_mark_dirty(codec->core.regmap);
2866
Takashi Iwaicc72da72015-02-19 16:00:22 +01002867 codec->power_jiffies = jiffies;
Takashi Iwaicc72da72015-02-19 16:00:22 +01002868
Takashi Iwaid8193872012-08-31 07:54:38 -07002869 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02002870 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02002871 hda_exec_init_verbs(codec);
Takashi Iwai31614bb2013-01-23 15:58:40 +01002872 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002873 if (codec->patch_ops.resume)
2874 codec->patch_ops.resume(codec);
2875 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02002876 if (codec->patch_ops.init)
2877 codec->patch_ops.init(codec);
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01002878 if (codec->core.regmap)
2879 regcache_sync(codec->core.regmap);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002880 }
David Henningsson26a6cb62012-10-09 15:04:21 +02002881
2882 if (codec->jackpoll_interval)
2883 hda_jackpoll_work(&codec->jackpoll_work.work);
Takashi Iwai31614bb2013-01-23 15:58:40 +01002884 else
David Henningsson26a6cb62012-10-09 15:04:21 +02002885 snd_hda_jack_report_sync(codec);
Takashi Iwai7639a062015-03-03 10:07:24 +01002886 atomic_dec(&codec->core.in_pm);
Takashi Iwaicb53c622007-08-10 17:21:45 +02002887}
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01002888
Takashi Iwaicc72da72015-02-19 16:00:22 +01002889static int hda_codec_runtime_suspend(struct device *dev)
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01002890{
2891 struct hda_codec *codec = dev_to_hda_codec(dev);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002892 struct hda_pcm *pcm;
Takashi Iwaicc72da72015-02-19 16:00:22 +01002893 unsigned int state;
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01002894
2895 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002896 list_for_each_entry(pcm, &codec->pcm_list_head, list)
2897 snd_pcm_suspend_all(pcm->pcm);
Takashi Iwaicc72da72015-02-19 16:00:22 +01002898 state = hda_call_codec_suspend(codec);
Takashi Iwai7639a062015-03-03 10:07:24 +01002899 if (codec_has_clkstop(codec) && codec_has_epss(codec) &&
2900 (state & AC_PWRST_CLK_STOP_OK))
2901 snd_hdac_codec_link_down(&codec->core);
Mengdong Lina5e7e072015-04-29 17:43:20 +08002902 snd_hdac_link_power(&codec->core, false);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01002903 return 0;
2904}
2905
Takashi Iwaicc72da72015-02-19 16:00:22 +01002906static int hda_codec_runtime_resume(struct device *dev)
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01002907{
Takashi Iwai55ed9cd2015-02-19 17:35:32 +01002908 struct hda_codec *codec = dev_to_hda_codec(dev);
2909
Mengdong Lina5e7e072015-04-29 17:43:20 +08002910 snd_hdac_link_power(&codec->core, true);
Takashi Iwai7639a062015-03-03 10:07:24 +01002911 snd_hdac_codec_link_up(&codec->core);
Takashi Iwai55ed9cd2015-02-19 17:35:32 +01002912 hda_call_codec_resume(codec);
Takashi Iwaicc72da72015-02-19 16:00:22 +01002913 pm_runtime_mark_last_busy(dev);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01002914 return 0;
2915}
Takashi Iwai2a439522011-07-26 09:52:50 +02002916#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002917
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01002918/* referred in hda_bind.c */
2919const struct dev_pm_ops hda_codec_driver_pm = {
Takashi Iwaicc72da72015-02-19 16:00:22 +01002920 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
2921 pm_runtime_force_resume)
2922 SET_RUNTIME_PM_OPS(hda_codec_runtime_suspend, hda_codec_runtime_resume,
2923 NULL)
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01002924};
Takashi Iwai54d17402005-11-21 16:33:22 +01002925
Takashi Iwai9c9a5172012-07-31 11:35:35 +02002926/*
2927 * add standard channel maps if not specified
2928 */
2929static int add_std_chmaps(struct hda_codec *codec)
2930{
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002931 struct hda_pcm *pcm;
2932 int str, err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02002933
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002934 list_for_each_entry(pcm, &codec->pcm_list_head, list) {
Takashi Iwai9c9a5172012-07-31 11:35:35 +02002935 for (str = 0; str < 2; str++) {
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002936 struct hda_pcm_stream *hinfo = &pcm->stream[str];
Takashi Iwai9c9a5172012-07-31 11:35:35 +02002937 struct snd_pcm_chmap *chmap;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01002938 const struct snd_pcm_chmap_elem *elem;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02002939
Takashi Iwaib25cf302015-08-26 14:21:14 +02002940 if (!pcm->pcm || pcm->own_chmap || !hinfo->substreams)
Takashi Iwai9c9a5172012-07-31 11:35:35 +02002941 continue;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01002942 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002943 err = snd_pcm_add_chmap_ctls(pcm->pcm, str, elem,
Takashi Iwai9c9a5172012-07-31 11:35:35 +02002944 hinfo->channels_max,
2945 0, &chmap);
2946 if (err < 0)
2947 return err;
2948 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
2949 }
2950 }
2951 return 0;
2952}
2953
Takashi Iwaiee81abb2012-11-08 17:12:10 +01002954/* default channel maps for 2.1 speakers;
2955 * since HD-audio supports only stereo, odd number channels are omitted
2956 */
2957const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
2958 { .channels = 2,
2959 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
2960 { .channels = 4,
2961 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2962 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
2963 { }
2964};
2965EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
2966
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002967int snd_hda_codec_build_controls(struct hda_codec *codec)
2968{
2969 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02002970 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002971 /* continue to initialize... */
2972 if (codec->patch_ops.init)
2973 err = codec->patch_ops.init(codec);
2974 if (!err && codec->patch_ops.build_controls)
2975 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02002976 if (err < 0)
2977 return err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02002978
2979 /* we create chmaps here instead of build_pcms */
2980 err = add_std_chmaps(codec);
2981 if (err < 0)
2982 return err;
2983
David Henningsson26a6cb62012-10-09 15:04:21 +02002984 if (codec->jackpoll_interval)
2985 hda_jackpoll_work(&codec->jackpoll_work.work);
2986 else
2987 snd_hda_jack_report_sync(codec); /* call at the last init point */
Takashi Iwaib9c590b2013-01-24 17:27:32 +01002988 sync_power_up_states(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 return 0;
2990}
2991
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 * PCM stuff
2994 */
2995static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
2996 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002997 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998{
2999 return 0;
3000}
3001
3002static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3003 struct hda_codec *codec,
3004 unsigned int stream_tag,
3005 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003006 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007{
3008 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3009 return 0;
3010}
3011
3012static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3013 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003014 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015{
Takashi Iwai888afa12008-03-18 09:57:50 +01003016 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 return 0;
3018}
3019
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003020static int set_pcm_default_values(struct hda_codec *codec,
3021 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003023 int err;
3024
Takashi Iwai0ba21762007-04-16 11:29:14 +02003025 /* query support PCM information from the given NID */
3026 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003027 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02003028 info->rates ? NULL : &info->rates,
3029 info->formats ? NULL : &info->formats,
3030 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003031 if (err < 0)
3032 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 }
3034 if (info->ops.open == NULL)
3035 info->ops.open = hda_pcm_default_open_close;
3036 if (info->ops.close == NULL)
3037 info->ops.close = hda_pcm_default_open_close;
3038 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003039 if (snd_BUG_ON(!info->nid))
3040 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 info->ops.prepare = hda_pcm_default_prepare;
3042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003044 if (snd_BUG_ON(!info->nid))
3045 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 info->ops.cleanup = hda_pcm_default_cleanup;
3047 }
3048 return 0;
3049}
3050
Takashi Iwaieb541332010-08-06 13:48:11 +02003051/*
3052 * codec prepare/cleanup entries
3053 */
Takashi Iwai95a962c2014-10-29 16:03:58 +01003054/**
3055 * snd_hda_codec_prepare - Prepare a stream
3056 * @codec: the HDA codec
3057 * @hinfo: PCM information
3058 * @stream: stream tag to assign
3059 * @format: format id to assign
3060 * @substream: PCM substream to assign
3061 *
3062 * Calls the prepare callback set by the codec with the given arguments.
3063 * Clean up the inactive streams when successful.
3064 */
Takashi Iwaieb541332010-08-06 13:48:11 +02003065int snd_hda_codec_prepare(struct hda_codec *codec,
3066 struct hda_pcm_stream *hinfo,
3067 unsigned int stream,
3068 unsigned int format,
3069 struct snd_pcm_substream *substream)
3070{
3071 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003072 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwai61ca4102015-02-27 17:57:55 +01003073 if (hinfo->ops.prepare)
3074 ret = hinfo->ops.prepare(hinfo, codec, stream, format,
3075 substream);
3076 else
3077 ret = -ENODEV;
Takashi Iwaieb541332010-08-06 13:48:11 +02003078 if (ret >= 0)
3079 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003080 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003081 return ret;
3082}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003083EXPORT_SYMBOL_GPL(snd_hda_codec_prepare);
Takashi Iwaieb541332010-08-06 13:48:11 +02003084
Takashi Iwai95a962c2014-10-29 16:03:58 +01003085/**
3086 * snd_hda_codec_cleanup - Prepare a stream
3087 * @codec: the HDA codec
3088 * @hinfo: PCM information
3089 * @substream: PCM substream
3090 *
3091 * Calls the cleanup callback set by the codec with the given arguments.
3092 */
Takashi Iwaieb541332010-08-06 13:48:11 +02003093void snd_hda_codec_cleanup(struct hda_codec *codec,
3094 struct hda_pcm_stream *hinfo,
3095 struct snd_pcm_substream *substream)
3096{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003097 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwai61ca4102015-02-27 17:57:55 +01003098 if (hinfo->ops.cleanup)
3099 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003100 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003101}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003102EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup);
Takashi Iwaieb541332010-08-06 13:48:11 +02003103
Takashi Iwaid5191e52009-11-16 14:58:17 +01003104/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01003105const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3106 "Audio", "SPDIF", "HDMI", "Modem"
3107};
3108
Takashi Iwai176d5332008-07-30 15:01:44 +02003109/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003110 * get the empty PCM device number to assign
3111 */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003112static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003113{
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003114 /* audio device indices; not linear to keep compatibility */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003115 /* assigned to static slots up to dev#10; if more needed, assign
3116 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
3117 */
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003118 static int audio_idx[HDA_PCM_NTYPES][5] = {
3119 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3120 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01003121 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003122 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003123 };
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003124 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003125
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003126 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003127 dev_err(bus->card->dev, "Invalid PCM type %d\n", type);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003128 return -EINVAL;
3129 }
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003130
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003131 for (i = 0; audio_idx[type][i] >= 0; i++) {
3132#ifndef CONFIG_SND_DYNAMIC_MINORS
3133 if (audio_idx[type][i] >= 8)
3134 break;
3135#endif
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003136 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3137 return audio_idx[type][i];
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003138 }
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003139
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003140#ifdef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai01b65bf2011-11-24 14:31:46 +01003141 /* non-fixed slots starting from 10 */
3142 for (i = 10; i < 32; i++) {
3143 if (!test_and_set_bit(i, bus->pcm_dev_bits))
3144 return i;
3145 }
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003146#endif
Takashi Iwai01b65bf2011-11-24 14:31:46 +01003147
Takashi Iwai4e76a882014-02-25 12:21:03 +01003148 dev_warn(bus->card->dev, "Too many %s devices\n",
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003149 snd_hda_pcm_type_name[type]);
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003150#ifndef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai4e76a882014-02-25 12:21:03 +01003151 dev_warn(bus->card->dev,
3152 "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003153#endif
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003154 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003155}
3156
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003157/* call build_pcms ops of the given codec and set up the default parameters */
3158int snd_hda_codec_parse_pcms(struct hda_codec *codec)
Takashi Iwai176d5332008-07-30 15:01:44 +02003159{
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003160 struct hda_pcm *cpcm;
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003161 int err;
Takashi Iwai176d5332008-07-30 15:01:44 +02003162
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003163 if (!list_empty(&codec->pcm_list_head))
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003164 return 0; /* already parsed */
3165
3166 if (!codec->patch_ops.build_pcms)
3167 return 0;
3168
3169 err = codec->patch_ops.build_pcms(codec);
3170 if (err < 0) {
3171 codec_err(codec, "cannot build PCMs for #%d (error %d)\n",
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003172 codec->core.addr, err);
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003173 return err;
3174 }
3175
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003176 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003177 int stream;
3178
3179 for (stream = 0; stream < 2; stream++) {
3180 struct hda_pcm_stream *info = &cpcm->stream[stream];
3181
3182 if (!info->substreams)
3183 continue;
Takashi Iwai176d5332008-07-30 15:01:44 +02003184 err = set_pcm_default_values(codec, info);
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003185 if (err < 0) {
3186 codec_warn(codec,
3187 "fail to setup default for PCM %s\n",
3188 cpcm->name);
Takashi Iwai176d5332008-07-30 15:01:44 +02003189 return err;
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003190 }
Takashi Iwai176d5332008-07-30 15:01:44 +02003191 }
3192 }
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003193
3194 return 0;
Takashi Iwai176d5332008-07-30 15:01:44 +02003195}
3196
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003197/* assign all PCMs of the given codec */
3198int snd_hda_codec_build_pcms(struct hda_codec *codec)
3199{
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003200 struct hda_bus *bus = codec->bus;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003201 struct hda_pcm *cpcm;
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003202 int dev, err;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003203
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003204 err = snd_hda_codec_parse_pcms(codec);
Takashi Iwaid2896192015-10-20 16:23:55 +02003205 if (err < 0)
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003206 return err;
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003207
3208 /* attach a new PCM streams */
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003209 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003210 if (cpcm->pcm)
3211 continue; /* already attached */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003212 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01003213 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003214
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003215 dev = get_empty_pcm_device(bus, cpcm->pcm_type);
Wang YanQing1f7f51a2017-09-03 21:18:49 +08003216 if (dev < 0) {
3217 cpcm->device = SNDRV_PCM_INVALID_DEVICE;
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003218 continue; /* no fatal error */
Wang YanQing1f7f51a2017-09-03 21:18:49 +08003219 }
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003220 cpcm->device = dev;
Takashi Iwai0a505752015-04-16 23:25:02 +02003221 err = snd_hda_attach_pcm_stream(bus, codec, cpcm);
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003222 if (err < 0) {
3223 codec_err(codec,
3224 "cannot attach PCM stream %d for codec #%d\n",
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003225 dev, codec->core.addr);
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003226 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003227 }
3228 }
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003229
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003230 return 0;
3231}
3232
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 * snd_hda_add_new_ctls - create controls from the array
3235 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003236 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 *
3238 * This helper function creates and add new controls in the given array.
3239 * The array must be terminated with an empty entry as terminator.
3240 *
3241 * Returns 0 if successful, or a negative error code.
3242 */
Takashi Iwai031024e2011-05-02 11:29:30 +02003243int snd_hda_add_new_ctls(struct hda_codec *codec,
3244 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01003246 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247
3248 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01003249 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01003250 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003251 if (knew->iface == -1) /* skip this codec private value */
3252 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01003253 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01003254 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003255 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01003256 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01003257 if (addr > 0)
3258 kctl->id.device = addr;
3259 if (idx > 0)
3260 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003261 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003262 if (!err)
3263 break;
3264 /* try first with another device index corresponding to
3265 * the codec addr; if it still fails (or it's the
3266 * primary codec), then try another control index
3267 */
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003268 if (!addr && codec->core.addr)
3269 addr = codec->core.addr;
Takashi Iwai1afe2062010-12-23 10:17:52 +01003270 else if (!idx && !knew->index) {
3271 idx = find_empty_mixer_ctl_idx(codec,
Takashi Iwaidcda5802012-10-12 17:24:51 +02003272 knew->name, 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003273 if (idx <= 0)
3274 return err;
3275 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01003276 return err;
3277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 }
3279 return 0;
3280}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003281EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282
Takashi Iwai83012a72012-08-24 18:38:08 +02003283#ifdef CONFIG_PM
Takashi Iwaibb573922015-02-20 09:26:04 +01003284static void codec_set_power_save(struct hda_codec *codec, int delay)
Takashi Iwaicc72da72015-02-19 16:00:22 +01003285{
3286 struct device *dev = hda_codec_dev(codec);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003287
Lu, Han2377c3c2015-06-09 16:50:38 +08003288 if (delay == 0 && codec->auto_runtime_pm)
3289 delay = 3000;
3290
Takashi Iwaicc72da72015-02-19 16:00:22 +01003291 if (delay > 0) {
3292 pm_runtime_set_autosuspend_delay(dev, delay);
3293 pm_runtime_use_autosuspend(dev);
3294 pm_runtime_allow(dev);
3295 if (!pm_runtime_suspended(dev))
3296 pm_runtime_mark_last_busy(dev);
3297 } else {
3298 pm_runtime_dont_use_autosuspend(dev);
3299 pm_runtime_forbid(dev);
3300 }
3301}
Takashi Iwaibb573922015-02-20 09:26:04 +01003302
3303/**
3304 * snd_hda_set_power_save - reprogram autosuspend for the given delay
3305 * @bus: HD-audio bus
3306 * @delay: autosuspend delay in msec, 0 = off
3307 *
3308 * Synchronize the runtime PM autosuspend state from the power_save option.
3309 */
3310void snd_hda_set_power_save(struct hda_bus *bus, int delay)
3311{
3312 struct hda_codec *c;
3313
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003314 list_for_each_codec(c, bus)
Takashi Iwaibb573922015-02-20 09:26:04 +01003315 codec_set_power_save(c, delay);
3316}
3317EXPORT_SYMBOL_GPL(snd_hda_set_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003318
Takashi Iwaid5191e52009-11-16 14:58:17 +01003319/**
3320 * snd_hda_check_amp_list_power - Check the amp list and update the power
3321 * @codec: HD-audio codec
3322 * @check: the object containing an AMP list and the status
3323 * @nid: NID to check / update
3324 *
3325 * Check whether the given NID is in the amp list. If it's in the list,
3326 * check the current AMP status, and update the the power-status according
3327 * to the mute status.
3328 *
3329 * This function is supposed to be set or called from the check_power_status
3330 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003331 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003332int snd_hda_check_amp_list_power(struct hda_codec *codec,
3333 struct hda_loopback_check *check,
3334 hda_nid_t nid)
3335{
Takashi Iwai031024e2011-05-02 11:29:30 +02003336 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003337 int ch, v;
3338
3339 if (!check->amplist)
3340 return 0;
3341 for (p = check->amplist; p->nid; p++) {
3342 if (p->nid == nid)
3343 break;
3344 }
3345 if (!p->nid)
3346 return 0; /* nothing changed */
3347
3348 for (p = check->amplist; p->nid; p++) {
3349 for (ch = 0; ch < 2; ch++) {
3350 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
3351 p->idx);
3352 if (!(v & HDA_AMP_MUTE) && v > 0) {
3353 if (!check->power_on) {
3354 check->power_on = 1;
Takashi Iwai664c7152015-04-08 11:43:14 +02003355 snd_hda_power_up_pm(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003356 }
3357 return 1;
3358 }
3359 }
3360 }
3361 if (check->power_on) {
3362 check->power_on = 0;
Takashi Iwai664c7152015-04-08 11:43:14 +02003363 snd_hda_power_down_pm(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003364 }
3365 return 0;
3366}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003367EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003368#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003370/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 * input MUX helper
3372 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01003373
3374/**
3375 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003376 * @imux: imux helper object
3377 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003378 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003379int snd_hda_input_mux_info(const struct hda_input_mux *imux,
3380 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381{
3382 unsigned int index;
3383
3384 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3385 uinfo->count = 1;
3386 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02003387 if (!imux->num_items)
3388 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 index = uinfo->value.enumerated.item;
3390 if (index >= imux->num_items)
3391 index = imux->num_items - 1;
3392 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
3393 return 0;
3394}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003395EXPORT_SYMBOL_GPL(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396
Takashi Iwaid5191e52009-11-16 14:58:17 +01003397/**
3398 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003399 * @codec: the HDA codec
3400 * @imux: imux helper object
3401 * @ucontrol: pointer to get/store the data
3402 * @nid: input mux NID
3403 * @cur_val: pointer to get/store the current imux value
Takashi Iwaid5191e52009-11-16 14:58:17 +01003404 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003405int snd_hda_input_mux_put(struct hda_codec *codec,
3406 const struct hda_input_mux *imux,
3407 struct snd_ctl_elem_value *ucontrol,
3408 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 unsigned int *cur_val)
3410{
3411 unsigned int idx;
3412
Takashi Iwai5513b0c2007-10-09 11:58:41 +02003413 if (!imux->num_items)
3414 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 idx = ucontrol->value.enumerated.item[0];
3416 if (idx >= imux->num_items)
3417 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003418 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003420 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
3421 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 *cur_val = idx;
3423 return 1;
3424}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003425EXPORT_SYMBOL_GPL(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426
3427
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003428/**
3429 * snd_hda_enum_helper_info - Helper for simple enum ctls
3430 * @kcontrol: ctl element
3431 * @uinfo: pointer to get/store the data
3432 * @num_items: number of enum items
3433 * @texts: enum item string array
3434 *
Takashi Iwaidda415d2012-11-30 18:34:38 +01003435 * process kcontrol info callback of a simple string enum array
3436 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
3437 */
3438int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
3439 struct snd_ctl_elem_info *uinfo,
3440 int num_items, const char * const *texts)
3441{
3442 static const char * const texts_default[] = {
3443 "Disabled", "Enabled"
3444 };
3445
3446 if (!texts || !num_items) {
3447 num_items = 2;
3448 texts = texts_default;
3449 }
3450
Takashi Iwai3ff72212014-10-20 18:17:28 +02003451 return snd_ctl_enum_info(uinfo, 1, num_items, texts);
Takashi Iwaidda415d2012-11-30 18:34:38 +01003452}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003453EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info);
Takashi Iwaidda415d2012-11-30 18:34:38 +01003454
3455/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 * Multi-channel / digital-out PCM helper functions
3457 */
3458
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003459/* setup SPDIF output stream */
3460static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
3461 unsigned int stream_tag, unsigned int format)
3462{
Laurence Darby3bef1c32012-11-03 17:00:06 +00003463 struct hda_spdif_out *spdif;
3464 unsigned int curr_fmt;
3465 bool reset;
Stephen Warren7c935972011-06-01 11:14:17 -06003466
Laurence Darby3bef1c32012-11-03 17:00:06 +00003467 spdif = snd_hda_spdif_out_of_nid(codec, nid);
Libin Yang960a5812016-06-16 11:13:25 +08003468 /* Add sanity check to pass klockwork check.
3469 * This should never happen.
3470 */
3471 if (WARN_ON(spdif == NULL))
3472 return;
3473
Laurence Darby3bef1c32012-11-03 17:00:06 +00003474 curr_fmt = snd_hda_codec_read(codec, nid, 0,
3475 AC_VERB_GET_STREAM_FORMAT, 0);
3476 reset = codec->spdif_status_reset &&
3477 (spdif->ctls & AC_DIG1_ENABLE) &&
3478 curr_fmt != format;
3479
3480 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
3481 updated */
3482 if (reset)
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003483 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06003484 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02003485 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003486 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02003487 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02003488 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02003489 for (d = codec->slave_dig_outs; *d; d++)
3490 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
3491 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04003492 }
Takashi Iwai2f728532008-09-25 16:32:41 +02003493 /* turn on again (if needed) */
Laurence Darby3bef1c32012-11-03 17:00:06 +00003494 if (reset)
Takashi Iwai2f728532008-09-25 16:32:41 +02003495 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06003496 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02003497}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04003498
Takashi Iwai2f728532008-09-25 16:32:41 +02003499static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
3500{
3501 snd_hda_codec_cleanup_stream(codec, nid);
3502 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02003503 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02003504 for (d = codec->slave_dig_outs; *d; d++)
3505 snd_hda_codec_cleanup_stream(codec, *d);
3506 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003507}
3508
Takashi Iwaid5191e52009-11-16 14:58:17 +01003509/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003510 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003511 * @codec: the HDA codec
3512 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003514int snd_hda_multi_out_dig_open(struct hda_codec *codec,
3515 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516{
Ingo Molnar62932df2006-01-16 16:34:20 +01003517 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02003518 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
3519 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02003520 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01003522 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 return 0;
3524}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003525EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526
Takashi Iwaid5191e52009-11-16 14:58:17 +01003527/**
3528 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003529 * @codec: the HDA codec
3530 * @mout: hda_multi_out object
3531 * @stream_tag: stream tag to assign
3532 * @format: format id to assign
3533 * @substream: PCM substream to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01003534 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003535int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
3536 struct hda_multi_out *mout,
3537 unsigned int stream_tag,
3538 unsigned int format,
3539 struct snd_pcm_substream *substream)
3540{
3541 mutex_lock(&codec->spdif_mutex);
3542 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
3543 mutex_unlock(&codec->spdif_mutex);
3544 return 0;
3545}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003546EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003547
Takashi Iwaid5191e52009-11-16 14:58:17 +01003548/**
3549 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003550 * @codec: the HDA codec
3551 * @mout: hda_multi_out object
Takashi Iwaid5191e52009-11-16 14:58:17 +01003552 */
Takashi Iwai9411e212009-02-13 11:32:28 +01003553int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
3554 struct hda_multi_out *mout)
3555{
3556 mutex_lock(&codec->spdif_mutex);
3557 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3558 mutex_unlock(&codec->spdif_mutex);
3559 return 0;
3560}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003561EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup);
Takashi Iwai9411e212009-02-13 11:32:28 +01003562
Takashi Iwaid5191e52009-11-16 14:58:17 +01003563/**
3564 * snd_hda_multi_out_dig_close - release the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003565 * @codec: the HDA codec
3566 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003568int snd_hda_multi_out_dig_close(struct hda_codec *codec,
3569 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570{
Ingo Molnar62932df2006-01-16 16:34:20 +01003571 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01003573 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 return 0;
3575}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003576EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577
Takashi Iwaid5191e52009-11-16 14:58:17 +01003578/**
3579 * snd_hda_multi_out_analog_open - open analog outputs
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003580 * @codec: the HDA codec
3581 * @mout: hda_multi_out object
3582 * @substream: PCM substream to assign
3583 * @hinfo: PCM information to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01003584 *
3585 * Open analog outputs and set up the hw-constraints.
3586 * If the digital outputs can be opened as slave, open the digital
3587 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003589int snd_hda_multi_out_analog_open(struct hda_codec *codec,
3590 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01003591 struct snd_pcm_substream *substream,
3592 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593{
Takashi Iwai9a081602008-02-12 18:37:26 +01003594 struct snd_pcm_runtime *runtime = substream->runtime;
3595 runtime->hw.channels_max = mout->max_channels;
3596 if (mout->dig_out_nid) {
3597 if (!mout->analog_rates) {
3598 mout->analog_rates = hinfo->rates;
3599 mout->analog_formats = hinfo->formats;
3600 mout->analog_maxbps = hinfo->maxbps;
3601 } else {
3602 runtime->hw.rates = mout->analog_rates;
3603 runtime->hw.formats = mout->analog_formats;
3604 hinfo->maxbps = mout->analog_maxbps;
3605 }
3606 if (!mout->spdif_rates) {
3607 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
3608 &mout->spdif_rates,
3609 &mout->spdif_formats,
3610 &mout->spdif_maxbps);
3611 }
3612 mutex_lock(&codec->spdif_mutex);
3613 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02003614 if ((runtime->hw.rates & mout->spdif_rates) &&
3615 (runtime->hw.formats & mout->spdif_formats)) {
3616 runtime->hw.rates &= mout->spdif_rates;
3617 runtime->hw.formats &= mout->spdif_formats;
3618 if (mout->spdif_maxbps < hinfo->maxbps)
3619 hinfo->maxbps = mout->spdif_maxbps;
3620 } else {
3621 mout->share_spdif = 0;
3622 /* FIXME: need notify? */
3623 }
Takashi Iwai9a081602008-02-12 18:37:26 +01003624 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02003625 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01003626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 return snd_pcm_hw_constraint_step(substream->runtime, 0,
3628 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
3629}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003630EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631
Takashi Iwaid5191e52009-11-16 14:58:17 +01003632/**
3633 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003634 * @codec: the HDA codec
3635 * @mout: hda_multi_out object
3636 * @stream_tag: stream tag to assign
3637 * @format: format id to assign
3638 * @substream: PCM substream to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01003639 *
3640 * Set up the i/o for analog out.
3641 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003643int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
3644 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 unsigned int stream_tag,
3646 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003647 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648{
Takashi Iwaidda14412011-05-02 11:29:30 +02003649 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003651 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 int i;
3653
Ingo Molnar62932df2006-01-16 16:34:20 +01003654 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003655 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01003656 if (mout->dig_out_nid && mout->share_spdif &&
3657 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Libin Yang960a5812016-06-16 11:13:25 +08003658 if (chs == 2 && spdif != NULL &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02003659 snd_hda_is_supported_format(codec, mout->dig_out_nid,
3660 format) &&
Stephen Warren7c935972011-06-01 11:14:17 -06003661 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003663 setup_dig_out_stream(codec, mout->dig_out_nid,
3664 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 } else {
3666 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02003667 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 }
3669 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003670 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671
3672 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003673 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
3674 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02003675 if (!mout->no_share_stream &&
3676 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003678 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
3679 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01003680 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02003681 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
3682 if (!mout->no_share_stream && mout->hp_out_nid[i])
3683 snd_hda_codec_setup_stream(codec,
3684 mout->hp_out_nid[i],
3685 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01003686
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 /* surrounds */
3688 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02003689 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003690 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3691 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02003692 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003693 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3694 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 }
David Henningssoncd4035e2013-10-10 09:01:25 +02003696
3697 /* extra surrounds */
3698 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
3699 int ch = 0;
3700 if (!mout->extra_out_nid[i])
3701 break;
3702 if (chs >= (i + 1) * 2)
3703 ch = i * 2;
3704 else if (!mout->no_share_stream)
3705 break;
3706 snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
3707 stream_tag, ch, format);
3708 }
3709
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 return 0;
3711}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003712EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713
Takashi Iwaid5191e52009-11-16 14:58:17 +01003714/**
3715 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003716 * @codec: the HDA codec
3717 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003719int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
3720 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721{
Takashi Iwaidda14412011-05-02 11:29:30 +02003722 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 int i;
3724
3725 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01003726 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01003728 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02003729 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
3730 if (mout->hp_out_nid[i])
3731 snd_hda_codec_cleanup_stream(codec,
3732 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01003733 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
3734 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01003735 snd_hda_codec_cleanup_stream(codec,
3736 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01003737 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02003739 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 mout->dig_out_used = 0;
3741 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003742 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 return 0;
3744}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003745EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746
Takashi Iwai47408602012-04-20 13:06:53 +02003747/**
3748 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003749 * @codec: the HDA codec
3750 * @pin: referred pin NID
Takashi Iwai47408602012-04-20 13:06:53 +02003751 *
3752 * Guess the suitable VREF pin bits to be set as the pin-control value.
3753 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
3754 */
3755unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
3756{
3757 unsigned int pincap;
3758 unsigned int oldval;
3759 oldval = snd_hda_codec_read(codec, pin, 0,
3760 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3761 pincap = snd_hda_query_pin_caps(codec, pin);
3762 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
3763 /* Exception: if the default pin setup is vref50, we give it priority */
3764 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
3765 return AC_PINCTL_VREF_80;
3766 else if (pincap & AC_PINCAP_VREF_50)
3767 return AC_PINCTL_VREF_50;
3768 else if (pincap & AC_PINCAP_VREF_100)
3769 return AC_PINCTL_VREF_100;
3770 else if (pincap & AC_PINCAP_VREF_GRD)
3771 return AC_PINCTL_VREF_GRD;
3772 return AC_PINCTL_VREF_HIZ;
3773}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003774EXPORT_SYMBOL_GPL(snd_hda_get_default_vref);
Takashi Iwai47408602012-04-20 13:06:53 +02003775
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003776/**
3777 * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap
3778 * @codec: the HDA codec
3779 * @pin: referred pin NID
3780 * @val: pin ctl value to audit
3781 */
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01003782unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
3783 hda_nid_t pin, unsigned int val)
3784{
3785 static unsigned int cap_lists[][2] = {
3786 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
3787 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
3788 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
3789 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
3790 };
3791 unsigned int cap;
3792
3793 if (!val)
3794 return 0;
3795 cap = snd_hda_query_pin_caps(codec, pin);
3796 if (!cap)
3797 return val; /* don't know what to do... */
3798
3799 if (val & AC_PINCTL_OUT_EN) {
3800 if (!(cap & AC_PINCAP_OUT))
3801 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
3802 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
3803 val &= ~AC_PINCTL_HP_EN;
3804 }
3805
3806 if (val & AC_PINCTL_IN_EN) {
3807 if (!(cap & AC_PINCAP_IN))
3808 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
3809 else {
3810 unsigned int vcap, vref;
3811 int i;
3812 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
3813 vref = val & AC_PINCTL_VREFEN;
3814 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
3815 if (vref == cap_lists[i][0] &&
3816 !(vcap & cap_lists[i][1])) {
3817 if (i == ARRAY_SIZE(cap_lists) - 1)
3818 vref = AC_PINCTL_VREF_HIZ;
3819 else
3820 vref = cap_lists[i + 1][0];
3821 }
3822 }
3823 val &= ~AC_PINCTL_VREFEN;
3824 val |= vref;
3825 }
3826 }
3827
3828 return val;
3829}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003830EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl);
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01003831
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003832/**
3833 * _snd_hda_pin_ctl - Helper to set pin ctl value
3834 * @codec: the HDA codec
3835 * @pin: referred pin NID
3836 * @val: pin control value to set
3837 * @cached: access over codec pinctl cache or direct write
3838 *
3839 * This function is a helper to set a pin ctl value more safely.
3840 * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the
3841 * value in pin target array via snd_hda_codec_set_pin_target(), then
3842 * actually writes the value via either snd_hda_codec_update_cache() or
3843 * snd_hda_codec_write() depending on @cached flag.
3844 */
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02003845int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
3846 unsigned int val, bool cached)
3847{
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01003848 val = snd_hda_correct_pin_ctl(codec, pin, val);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01003849 snd_hda_codec_set_pin_target(codec, pin, val);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02003850 if (cached)
3851 return snd_hda_codec_update_cache(codec, pin, 0,
3852 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
3853 else
3854 return snd_hda_codec_write(codec, pin, 0,
3855 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
3856}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003857EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02003858
Takashi Iwai990061c2010-09-09 22:08:44 +02003859/**
3860 * snd_hda_add_imux_item - Add an item to input_mux
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003861 * @codec: the HDA codec
3862 * @imux: imux helper object
3863 * @label: the name of imux item to assign
3864 * @index: index number of imux item to assign
3865 * @type_idx: pointer to store the resultant label index
Takashi Iwai990061c2010-09-09 22:08:44 +02003866 *
3867 * When the same label is used already in the existing items, the number
3868 * suffix is appended to the label. This label index number is stored
3869 * to type_idx when non-NULL pointer is given.
3870 */
Takashi Iwai6194b992014-06-06 18:12:16 +02003871int snd_hda_add_imux_item(struct hda_codec *codec,
3872 struct hda_input_mux *imux, const char *label,
Takashi Iwai10a20af2010-09-09 16:28:02 +02003873 int index, int *type_idx)
3874{
3875 int i, label_idx = 0;
3876 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
Takashi Iwai6194b992014-06-06 18:12:16 +02003877 codec_err(codec, "hda_codec: Too many imux items!\n");
Takashi Iwai10a20af2010-09-09 16:28:02 +02003878 return -EINVAL;
3879 }
3880 for (i = 0; i < imux->num_items; i++) {
3881 if (!strncmp(label, imux->items[i].label, strlen(label)))
3882 label_idx++;
3883 }
3884 if (type_idx)
3885 *type_idx = label_idx;
3886 if (label_idx > 0)
3887 snprintf(imux->items[imux->num_items].label,
3888 sizeof(imux->items[imux->num_items].label),
3889 "%s %d", label, label_idx);
3890 else
3891 strlcpy(imux->items[imux->num_items].label, label,
3892 sizeof(imux->items[imux->num_items].label));
3893 imux->items[imux->num_items].index = index;
3894 imux->num_items++;
3895 return 0;
3896}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003897EXPORT_SYMBOL_GPL(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02003898
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899/**
Takashi Iwai0a505752015-04-16 23:25:02 +02003900 * snd_hda_bus_reset_codecs - Reset the bus
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003901 * @bus: HD-audio bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 */
Takashi Iwai0a505752015-04-16 23:25:02 +02003903void snd_hda_bus_reset_codecs(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904{
Takashi Iwai0ba21762007-04-16 11:29:14 +02003905 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003907 list_for_each_codec(codec, bus) {
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003908 /* FIXME: maybe a better way needed for forced reset */
David Henningsson26a6cb62012-10-09 15:04:21 +02003909 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003910#ifdef CONFIG_PM
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05003911 if (hda_codec_is_power_on(codec)) {
Takashi Iwaicc72da72015-02-19 16:00:22 +01003912 hda_call_codec_suspend(codec);
Mengdong Lin12edb892013-11-26 23:32:23 -05003913 hda_call_codec_resume(codec);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003914 }
3915#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917}
Takashi Iwaib2e18592008-07-30 15:01:44 +02003918
Takashi Iwaid5191e52009-11-16 14:58:17 +01003919/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003920 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
3921 * @pcm: PCM caps bits
3922 * @buf: the string buffer to write
3923 * @buflen: the max buffer length
3924 *
3925 * used by hda_proc.c and hda_eld.c
3926 */
Takashi Iwaib2022262008-11-21 21:24:03 +01003927void snd_print_pcm_bits(int pcm, char *buf, int buflen)
3928{
3929 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
3930 int i, j;
3931
3932 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
3933 if (pcm & (AC_SUPPCM_BITS_8 << i))
3934 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
3935
3936 buf[j] = '\0'; /* necessary when j == 0 */
3937}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003938EXPORT_SYMBOL_GPL(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003939
3940MODULE_DESCRIPTION("HDA codec core");
3941MODULE_LICENSE("GPL");