blob: 8fd745cb3f36c49c09213dccab6b818c6f6a00bf [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Universal Interface for Intel High Definition Audio Codec
3 *
4 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
5 *
6 *
7 * This driver is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This driver is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
Takashi Iwai18478e82012-03-09 17:51:10 +010022#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/init.h>
24#include <linux/delay.h>
25#include <linux/slab.h>
Ingo Molnar62932df2006-01-16 16:34:20 +010026#include <linux/mutex.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040027#include <linux/module.h>
Takashi Iwaif4d6a552013-12-05 11:55:05 +010028#include <linux/async.h>
Takashi Iwaicc72da72015-02-19 16:00:22 +010029#include <linux/pm.h>
30#include <linux/pm_runtime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <sound/core.h>
32#include "hda_codec.h"
33#include <sound/asoundef.h>
Jaroslav Kysela302e9c52006-07-05 17:39:49 +020034#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <sound/initval.h>
Takashi Iwaicd372fb2011-03-03 14:40:14 +010036#include <sound/jack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "hda_local.h"
Jaroslav Kysela123c07a2009-10-21 14:48:23 +020038#include "hda_beep.h"
Takashi Iwai1835a0f2011-10-27 22:12:46 +020039#include "hda_jack.h"
Takashi Iwai28073142007-07-27 18:58:06 +020040#include <sound/hda_hwdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Takashi Iwai83012a72012-08-24 18:38:08 +020042#ifdef CONFIG_PM
Takashi Iwai7639a062015-03-03 10:07:24 +010043#define codec_in_pm(codec) atomic_read(&(codec)->core.in_pm)
Takashi Iwaicc72da72015-02-19 16:00:22 +010044#define hda_codec_is_power_on(codec) \
45 (!pm_runtime_suspended(hda_codec_dev(codec)))
Takashi Iwaicb53c622007-08-10 17:21:45 +020046#else
Takashi Iwaid846b172012-11-24 11:58:24 +010047#define codec_in_pm(codec) 0
Takashi Iwaie581f3d2011-07-26 10:19:20 +020048#define hda_codec_is_power_on(codec) 1
Takashi Iwaicb53c622007-08-10 17:21:45 +020049#endif
50
Takashi Iwai7639a062015-03-03 10:07:24 +010051#define codec_has_epss(codec) \
52 ((codec)->core.power_caps & AC_PWRST_EPSS)
53#define codec_has_clkstop(codec) \
54 ((codec)->core.power_caps & AC_PWRST_CLKSTOP)
55
Takashi Iwai33fa35e2008-11-06 16:50:40 +010056/*
Takashi Iwai058524482015-03-03 15:40:08 +010057 * Send and receive a verb - passed to exec_verb override for hdac_device
Takashi Iwaiaa2936f2009-05-26 16:07:57 +020058 */
Takashi Iwai058524482015-03-03 15:40:08 +010059static int codec_exec_verb(struct hdac_device *dev, unsigned int cmd,
60 unsigned int flags, unsigned int *res)
Takashi Iwaiaa2936f2009-05-26 16:07:57 +020061{
Takashi Iwai058524482015-03-03 15:40:08 +010062 struct hda_codec *codec = container_of(dev, struct hda_codec, core);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +020063 struct hda_bus *bus = codec->bus;
Takashi Iwai8dd78332009-06-02 01:16:07 +020064 int err;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +020065
Wu Fengguang6430aee2009-07-17 16:49:19 +080066 if (cmd == ~0)
67 return -1;
68
Takashi Iwai8dd78332009-06-02 01:16:07 +020069 again:
Takashi Iwai664c7152015-04-08 11:43:14 +020070 snd_hda_power_up_pm(codec);
Takashi Iwaid068ebc2015-03-02 23:22:59 +010071 mutex_lock(&bus->core.cmd_mutex);
Takashi Iwai63e51fd72013-06-06 14:20:19 +020072 if (flags & HDA_RW_NO_RESPONSE_FALLBACK)
73 bus->no_response_fallback = 1;
Takashi Iwaid068ebc2015-03-02 23:22:59 +010074 err = snd_hdac_bus_exec_verb_unlocked(&bus->core, codec->core.addr,
75 cmd, res);
Takashi Iwai63e51fd72013-06-06 14:20:19 +020076 bus->no_response_fallback = 0;
Takashi Iwaid068ebc2015-03-02 23:22:59 +010077 mutex_unlock(&bus->core.cmd_mutex);
Takashi Iwai664c7152015-04-08 11:43:14 +020078 snd_hda_power_down_pm(codec);
Takashi Iwaicad372f2015-03-25 17:57:00 +010079 if (!codec_in_pm(codec) && res && err == -EAGAIN) {
Takashi Iwai8dd78332009-06-02 01:16:07 +020080 if (bus->response_reset) {
Takashi Iwai4e76a882014-02-25 12:21:03 +010081 codec_dbg(codec,
82 "resetting BUS due to fatal communication error\n");
Takashi Iwai0a505752015-04-16 23:25:02 +020083 snd_hda_bus_reset(bus);
Takashi Iwai8dd78332009-06-02 01:16:07 +020084 }
85 goto again;
86 }
87 /* clear reset-flag when the communication gets recovered */
Takashi Iwaid846b172012-11-24 11:58:24 +010088 if (!err || codec_in_pm(codec))
Takashi Iwai8dd78332009-06-02 01:16:07 +020089 bus->response_reset = 0;
Takashi Iwaiaa2936f2009-05-26 16:07:57 +020090 return err;
91}
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 * snd_hda_sequence_write - sequence writes
95 * @codec: the HDA codec
96 * @seq: VERB array to send
97 *
98 * Send the commands sequentially from the given array.
99 * The array must be terminated with NID=0.
100 */
101void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
102{
103 for (; seq->nid; seq++)
104 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
105}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100106EXPORT_SYMBOL_GPL(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100108/* connection list element */
109struct hda_conn_list {
110 struct list_head list;
111 int len;
112 hda_nid_t nid;
113 hda_nid_t conns[0];
114};
115
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200116/* look up the cached results */
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100117static struct hda_conn_list *
118lookup_conn_list(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200119{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100120 struct hda_conn_list *p;
121 list_for_each_entry(p, &codec->conn_list, list) {
122 if (p->nid == nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200123 return p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200124 }
125 return NULL;
126}
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200127
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100128static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
129 const hda_nid_t *list)
130{
131 struct hda_conn_list *p;
132
133 p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL);
134 if (!p)
135 return -ENOMEM;
136 p->len = len;
137 p->nid = nid;
138 memcpy(p->conns, list, len * sizeof(hda_nid_t));
139 list_add(&p->list, &codec->conn_list);
140 return 0;
141}
142
143static void remove_conn_list(struct hda_codec *codec)
144{
145 while (!list_empty(&codec->conn_list)) {
146 struct hda_conn_list *p;
147 p = list_first_entry(&codec->conn_list, typeof(*p), list);
148 list_del(&p->list);
149 kfree(p);
150 }
151}
152
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200153/* read the connection and add to the cache */
154static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200155{
Takashi Iwai4eea3092013-02-07 18:18:19 +0100156 hda_nid_t list[32];
157 hda_nid_t *result = list;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200158 int len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200159
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200160 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
Takashi Iwai4eea3092013-02-07 18:18:19 +0100161 if (len == -ENOSPC) {
162 len = snd_hda_get_num_raw_conns(codec, nid);
163 result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL);
164 if (!result)
165 return -ENOMEM;
166 len = snd_hda_get_raw_connections(codec, nid, result, len);
167 }
168 if (len >= 0)
169 len = snd_hda_override_conn_list(codec, nid, len, result);
170 if (result != list)
171 kfree(result);
172 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200173}
Takashi Iwaidce20792011-06-24 14:10:28 +0200174
175/**
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100176 * snd_hda_get_conn_list - get connection list
177 * @codec: the HDA codec
178 * @nid: NID to parse
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100179 * @listp: the pointer to store NID list
180 *
181 * Parses the connection list of the given widget and stores the pointer
182 * to the list of NIDs.
183 *
184 * Returns the number of connections, or a negative error code.
185 *
186 * Note that the returned pointer isn't protected against the list
187 * modification. If snd_hda_override_conn_list() might be called
188 * concurrently, protect with a mutex appropriately.
189 */
190int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
191 const hda_nid_t **listp)
192{
193 bool added = false;
194
195 for (;;) {
196 int err;
197 const struct hda_conn_list *p;
198
199 /* if the connection-list is already cached, read it */
200 p = lookup_conn_list(codec, nid);
201 if (p) {
202 if (listp)
203 *listp = p->conns;
204 return p->len;
205 }
206 if (snd_BUG_ON(added))
207 return -EINVAL;
208
209 err = read_and_add_raw_conns(codec, nid);
210 if (err < 0)
211 return err;
212 added = true;
213 }
214}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100215EXPORT_SYMBOL_GPL(snd_hda_get_conn_list);
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100216
217/**
Takashi Iwaidce20792011-06-24 14:10:28 +0200218 * snd_hda_get_connections - copy connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 * @codec: the HDA codec
220 * @nid: NID to parse
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200221 * @conn_list: connection list array; when NULL, checks only the size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 * @max_conns: max. number of connections to store
223 *
224 * Parses the connection list of the given widget and stores the list
225 * of NIDs.
226 *
227 * Returns the number of connections, or a negative error code.
228 */
229int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200230 hda_nid_t *conn_list, int max_conns)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200231{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100232 const hda_nid_t *list;
233 int len = snd_hda_get_conn_list(codec, nid, &list);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200234
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100235 if (len > 0 && conn_list) {
236 if (len > max_conns) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100237 codec_err(codec, "Too many connections %d for NID 0x%x\n",
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200238 len, nid);
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200239 return -EINVAL;
240 }
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100241 memcpy(conn_list, list, len * sizeof(hda_nid_t));
Takashi Iwaidce20792011-06-24 14:10:28 +0200242 }
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200243
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100244 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200245}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100246EXPORT_SYMBOL_GPL(snd_hda_get_connections);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200249 * snd_hda_override_conn_list - add/modify the connection-list to cache
250 * @codec: the HDA codec
251 * @nid: NID to parse
252 * @len: number of connection list entries
253 * @list: the list of connection entries
254 *
255 * Add or modify the given connection-list to the cache. If the corresponding
256 * cache already exists, invalidate it and append a new one.
257 *
258 * Returns zero or a negative error code.
259 */
260int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
261 const hda_nid_t *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100263 struct hda_conn_list *p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200264
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100265 p = lookup_conn_list(codec, nid);
266 if (p) {
267 list_del(&p->list);
268 kfree(p);
269 }
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200270
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100271 return add_conn_list(codec, nid, len, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100273EXPORT_SYMBOL_GPL(snd_hda_override_conn_list);
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200274
275/**
Takashi Iwai8d087c72011-06-28 12:45:47 +0200276 * snd_hda_get_conn_index - get the connection index of the given NID
277 * @codec: the HDA codec
278 * @mux: NID containing the list
279 * @nid: NID to select
280 * @recursive: 1 when searching NID recursively, otherwise 0
281 *
282 * Parses the connection list of the widget @mux and checks whether the
283 * widget @nid is present. If it is, return the connection index.
284 * Otherwise it returns -1.
285 */
286int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
287 hda_nid_t nid, int recursive)
288{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100289 const hda_nid_t *conn;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200290 int i, nums;
291
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100292 nums = snd_hda_get_conn_list(codec, mux, &conn);
Takashi Iwai8d087c72011-06-28 12:45:47 +0200293 for (i = 0; i < nums; i++)
294 if (conn[i] == nid)
295 return i;
296 if (!recursive)
297 return -1;
Takashi Iwaid94ddd82012-12-20 14:42:42 +0100298 if (recursive > 10) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100299 codec_dbg(codec, "too deep connection for 0x%x\n", nid);
Takashi Iwai8d087c72011-06-28 12:45:47 +0200300 return -1;
301 }
302 recursive++;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200303 for (i = 0; i < nums; i++) {
304 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
305 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
306 continue;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200307 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
308 return i;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200309 }
Takashi Iwai8d087c72011-06-28 12:45:47 +0200310 return -1;
311}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100312EXPORT_SYMBOL_GPL(snd_hda_get_conn_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Libin Yang13800f32017-01-12 16:04:52 +0800314/**
315 * snd_hda_get_num_devices - get DEVLIST_LEN parameter of the given widget
316 * @codec: the HDA codec
317 * @nid: NID of the pin to parse
318 *
319 * Get the device entry number on the given widget. This is a feature of
320 * DP MST audio. Each pin can have several device entries in it.
321 */
322unsigned int snd_hda_get_num_devices(struct hda_codec *codec, hda_nid_t nid)
Mengdong Linf1aa0682013-08-26 21:35:21 -0400323{
324 unsigned int wcaps = get_wcaps(codec, nid);
325 unsigned int parm;
326
327 if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) ||
328 get_wcaps_type(wcaps) != AC_WID_PIN)
329 return 0;
330
Dave Airlie132bd962015-06-09 13:39:31 +1000331 parm = snd_hdac_read_parm_uncached(&codec->core, nid, AC_PAR_DEVLIST_LEN);
Takashi Iwai86548442015-06-09 07:22:26 +0200332 if (parm == -1)
Mengdong Linf1aa0682013-08-26 21:35:21 -0400333 parm = 0;
334 return parm & AC_DEV_LIST_LEN_MASK;
335}
Libin Yang13800f32017-01-12 16:04:52 +0800336EXPORT_SYMBOL_GPL(snd_hda_get_num_devices);
Mengdong Linf1aa0682013-08-26 21:35:21 -0400337
338/**
339 * snd_hda_get_devices - copy device list without cache
340 * @codec: the HDA codec
341 * @nid: NID of the pin to parse
342 * @dev_list: device list array
343 * @max_devices: max. number of devices to store
344 *
345 * Copy the device list. This info is dynamic and so not cached.
346 * Currently called only from hda_proc.c, so not exported.
347 */
348int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid,
349 u8 *dev_list, int max_devices)
350{
351 unsigned int parm;
352 int i, dev_len, devices;
353
Libin Yang13800f32017-01-12 16:04:52 +0800354 parm = snd_hda_get_num_devices(codec, nid);
Mengdong Linf1aa0682013-08-26 21:35:21 -0400355 if (!parm) /* not multi-stream capable */
356 return 0;
357
358 dev_len = parm + 1;
359 dev_len = dev_len < max_devices ? dev_len : max_devices;
360
361 devices = 0;
362 while (devices < dev_len) {
Takashi Iwaicad372f2015-03-25 17:57:00 +0100363 if (snd_hdac_read(&codec->core, nid,
364 AC_VERB_GET_DEVICE_LIST, devices, &parm))
365 break; /* error */
Mengdong Linf1aa0682013-08-26 21:35:21 -0400366
367 for (i = 0; i < 8; i++) {
368 dev_list[devices] = (u8)parm;
369 parm >>= 4;
370 devices++;
371 if (devices >= dev_len)
372 break;
373 }
374 }
375 return devices;
376}
377
Libin Yang13800f32017-01-12 16:04:52 +0800378/**
379 * snd_hda_get_dev_select - get device entry select on the pin
380 * @codec: the HDA codec
381 * @nid: NID of the pin to get device entry select
382 *
383 * Get the devcie entry select on the pin. Return the device entry
384 * id selected on the pin. Return 0 means the first device entry
385 * is selected or MST is not supported.
386 */
387int snd_hda_get_dev_select(struct hda_codec *codec, hda_nid_t nid)
388{
389 /* not support dp_mst will always return 0, using first dev_entry */
390 if (!codec->dp_mst)
391 return 0;
392
393 return snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DEVICE_SEL, 0);
394}
395EXPORT_SYMBOL_GPL(snd_hda_get_dev_select);
396
397/**
398 * snd_hda_set_dev_select - set device entry select on the pin
399 * @codec: the HDA codec
400 * @nid: NID of the pin to set device entry select
401 * @dev_id: device entry id to be set
402 *
403 * Set the device entry select on the pin nid.
404 */
405int snd_hda_set_dev_select(struct hda_codec *codec, hda_nid_t nid, int dev_id)
406{
407 int ret, num_devices;
408
409 /* not support dp_mst will always return 0, using first dev_entry */
410 if (!codec->dp_mst)
411 return 0;
412
413 /* AC_PAR_DEVLIST_LEN is 0 based. */
414 num_devices = snd_hda_get_num_devices(codec, nid) + 1;
415 /* If Device List Length is 0 (num_device = 1),
416 * the pin is not multi stream capable.
417 * Do nothing in this case.
418 */
419 if (num_devices == 1)
420 return 0;
421
422 /* Behavior of setting index being equal to or greater than
423 * Device List Length is not predictable
424 */
425 if (num_devices <= dev_id)
426 return -EINVAL;
427
428 ret = snd_hda_codec_write(codec, nid, 0,
429 AC_VERB_SET_DEVICE_SEL, dev_id);
430
431 return ret;
432}
433EXPORT_SYMBOL_GPL(snd_hda_set_dev_select);
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100436 * read widget caps for each widget and store in cache
437 */
438static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
439{
440 int i;
441 hda_nid_t nid;
442
Takashi Iwai7639a062015-03-03 10:07:24 +0100443 codec->wcaps = kmalloc(codec->core.num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200444 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100445 return -ENOMEM;
Takashi Iwai7639a062015-03-03 10:07:24 +0100446 nid = codec->core.start_nid;
447 for (i = 0; i < codec->core.num_nodes; i++, nid++)
Takashi Iwai9ba17b42015-03-03 23:29:47 +0100448 codec->wcaps[i] = snd_hdac_read_parm_uncached(&codec->core,
449 nid, AC_PAR_AUDIO_WIDGET_CAP);
Takashi Iwai54d17402005-11-21 16:33:22 +0100450 return 0;
451}
452
Takashi Iwai3be14142009-02-20 14:11:16 +0100453/* read all pin default configurations and save codec->init_pins */
454static int read_pin_defaults(struct hda_codec *codec)
455{
Takashi Iwai7639a062015-03-03 10:07:24 +0100456 hda_nid_t nid;
Takashi Iwai3be14142009-02-20 14:11:16 +0100457
Takashi Iwai7639a062015-03-03 10:07:24 +0100458 for_each_hda_codec_node(nid, codec) {
Takashi Iwai3be14142009-02-20 14:11:16 +0100459 struct hda_pincfg *pin;
460 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +0200461 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +0100462 if (wid_type != AC_WID_PIN)
463 continue;
464 pin = snd_array_new(&codec->init_pins);
465 if (!pin)
466 return -ENOMEM;
467 pin->nid = nid;
468 pin->cfg = snd_hda_codec_read(codec, nid, 0,
469 AC_VERB_GET_CONFIG_DEFAULT, 0);
Libin Yang91520852017-01-12 16:04:53 +0800470 /*
471 * all device entries are the same widget control so far
472 * fixme: if any codec is different, need fix here
473 */
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200474 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
475 AC_VERB_GET_PIN_WIDGET_CONTROL,
476 0);
Takashi Iwai3be14142009-02-20 14:11:16 +0100477 }
478 return 0;
479}
480
481/* look up the given pin config list and return the item matching with NID */
482static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
483 struct snd_array *array,
484 hda_nid_t nid)
485{
486 int i;
487 for (i = 0; i < array->used; i++) {
488 struct hda_pincfg *pin = snd_array_elem(array, i);
489 if (pin->nid == nid)
490 return pin;
491 }
492 return NULL;
493}
494
Takashi Iwai3be14142009-02-20 14:11:16 +0100495/* set the current pin config value for the given NID.
496 * the value is cached, and read via snd_hda_codec_get_pincfg()
497 */
498int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
499 hda_nid_t nid, unsigned int cfg)
500{
501 struct hda_pincfg *pin;
502
Takashi Iwaid5657ec2013-04-18 09:59:28 +0200503 /* the check below may be invalid when pins are added by a fixup
504 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
505 * for now
506 */
507 /*
Takashi Iwaib82855a2009-12-27 11:24:56 +0100508 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
509 return -EINVAL;
Takashi Iwaid5657ec2013-04-18 09:59:28 +0200510 */
Takashi Iwaib82855a2009-12-27 11:24:56 +0100511
Takashi Iwai3be14142009-02-20 14:11:16 +0100512 pin = look_up_pincfg(codec, list, nid);
513 if (!pin) {
514 pin = snd_array_new(list);
515 if (!pin)
516 return -ENOMEM;
517 pin->nid = nid;
518 }
519 pin->cfg = cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100520 return 0;
521}
522
Takashi Iwaid5191e52009-11-16 14:58:17 +0100523/**
524 * snd_hda_codec_set_pincfg - Override a pin default configuration
525 * @codec: the HDA codec
526 * @nid: NID to set the pin config
527 * @cfg: the pin default config value
528 *
529 * Override a pin default configuration value in the cache.
530 * This value can be read by snd_hda_codec_get_pincfg() in a higher
531 * priority than the real hardware value.
532 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100533int snd_hda_codec_set_pincfg(struct hda_codec *codec,
534 hda_nid_t nid, unsigned int cfg)
535{
Takashi Iwai346ff702009-02-23 09:42:57 +0100536 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100537}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100538EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100539
Takashi Iwaid5191e52009-11-16 14:58:17 +0100540/**
541 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
542 * @codec: the HDA codec
543 * @nid: NID to get the pin config
544 *
545 * Get the current pin config value of the given pin NID.
546 * If the pincfg value is cached or overridden via sysfs or driver,
547 * returns the cached value.
548 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100549unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
550{
551 struct hda_pincfg *pin;
552
Takashi Iwai648a8d22014-02-25 10:38:13 +0100553#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai09b70e82013-01-10 18:21:56 +0100554 {
555 unsigned int cfg = 0;
556 mutex_lock(&codec->user_mutex);
557 pin = look_up_pincfg(codec, &codec->user_pins, nid);
558 if (pin)
559 cfg = pin->cfg;
560 mutex_unlock(&codec->user_mutex);
561 if (cfg)
562 return cfg;
563 }
Takashi Iwai3be14142009-02-20 14:11:16 +0100564#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100565 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
566 if (pin)
567 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100568 pin = look_up_pincfg(codec, &codec->init_pins, nid);
569 if (pin)
570 return pin->cfg;
571 return 0;
572}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100573EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100574
Takashi Iwai95a962c2014-10-29 16:03:58 +0100575/**
576 * snd_hda_codec_set_pin_target - remember the current pinctl target value
577 * @codec: the HDA codec
578 * @nid: pin NID
579 * @val: assigned pinctl value
580 *
581 * This function stores the given value to a pinctl target value in the
582 * pincfg table. This isn't always as same as the actually written value
583 * but can be referred at any time via snd_hda_codec_get_pin_target().
584 */
Takashi Iwaid7fdc002013-01-10 08:38:04 +0100585int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
586 unsigned int val)
587{
588 struct hda_pincfg *pin;
589
590 pin = look_up_pincfg(codec, &codec->init_pins, nid);
591 if (!pin)
592 return -EINVAL;
593 pin->target = val;
594 return 0;
595}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100596EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target);
Takashi Iwaid7fdc002013-01-10 08:38:04 +0100597
Takashi Iwai95a962c2014-10-29 16:03:58 +0100598/**
599 * snd_hda_codec_get_pin_target - return the current pinctl target value
600 * @codec: the HDA codec
601 * @nid: pin NID
602 */
Takashi Iwaid7fdc002013-01-10 08:38:04 +0100603int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
604{
605 struct hda_pincfg *pin;
606
607 pin = look_up_pincfg(codec, &codec->init_pins, nid);
608 if (!pin)
609 return 0;
610 return pin->target;
611}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100612EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target);
Takashi Iwaid7fdc002013-01-10 08:38:04 +0100613
Takashi Iwai92ee6162009-12-27 11:18:59 +0100614/**
615 * snd_hda_shutup_pins - Shut up all pins
616 * @codec: the HDA codec
617 *
618 * Clear all pin controls to shup up before suspend for avoiding click noise.
619 * The controls aren't cached so that they can be resumed properly.
620 */
621void snd_hda_shutup_pins(struct hda_codec *codec)
622{
623 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200624 /* don't shut up pins when unloading the driver; otherwise it breaks
625 * the default pin setup at the next load of the driver
626 */
627 if (codec->bus->shutdown)
628 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +0100629 for (i = 0; i < codec->init_pins.used; i++) {
630 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
631 /* use read here for syncing after issuing each verb */
632 snd_hda_codec_read(codec, pin->nid, 0,
633 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
634 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200635 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +0100636}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100637EXPORT_SYMBOL_GPL(snd_hda_shutup_pins);
Takashi Iwai92ee6162009-12-27 11:18:59 +0100638
Takashi Iwai2a439522011-07-26 09:52:50 +0200639#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200640/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
641static void restore_shutup_pins(struct hda_codec *codec)
642{
643 int i;
644 if (!codec->pins_shutup)
645 return;
646 if (codec->bus->shutdown)
647 return;
648 for (i = 0; i < codec->init_pins.used; i++) {
649 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
650 snd_hda_codec_write(codec, pin->nid, 0,
651 AC_VERB_SET_PIN_WIDGET_CONTROL,
652 pin->ctrl);
653 }
654 codec->pins_shutup = 0;
655}
Mike Waychison1c7276c2011-04-20 12:04:36 -0700656#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200657
David Henningsson26a6cb62012-10-09 15:04:21 +0200658static void hda_jackpoll_work(struct work_struct *work)
659{
660 struct hda_codec *codec =
661 container_of(work, struct hda_codec, jackpoll_work.work);
David Henningsson26a6cb62012-10-09 15:04:21 +0200662
663 snd_hda_jack_set_dirty_all(codec);
664 snd_hda_jack_poll_all(codec);
Wang Xingchao18e60622013-07-25 23:34:45 -0400665
666 if (!codec->jackpoll_interval)
667 return;
668
Takashi Iwai2f35c632015-02-27 22:43:26 +0100669 schedule_delayed_work(&codec->jackpoll_work,
670 codec->jackpoll_interval);
David Henningsson26a6cb62012-10-09 15:04:21 +0200671}
672
Takashi Iwai3fdf1462012-11-22 08:16:31 +0100673/* release all pincfg lists */
674static void free_init_pincfgs(struct hda_codec *codec)
Takashi Iwai3be14142009-02-20 14:11:16 +0100675{
Takashi Iwai346ff702009-02-23 09:42:57 +0100676 snd_array_free(&codec->driver_pins);
Takashi Iwai648a8d22014-02-25 10:38:13 +0100677#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai346ff702009-02-23 09:42:57 +0100678 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +0100679#endif
Takashi Iwai3be14142009-02-20 14:11:16 +0100680 snd_array_free(&codec->init_pins);
681}
682
Takashi Iwai54d17402005-11-21 16:33:22 +0100683/*
Takashi Iwaieb541332010-08-06 13:48:11 +0200684 * audio-converter setup caches
685 */
686struct hda_cvt_setup {
687 hda_nid_t nid;
688 u8 stream_tag;
689 u8 channel_id;
690 u16 format_id;
691 unsigned char active; /* cvt is currently used */
692 unsigned char dirty; /* setups should be cleared */
693};
694
695/* get or create a cache entry for the given audio converter NID */
696static struct hda_cvt_setup *
697get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
698{
699 struct hda_cvt_setup *p;
700 int i;
701
702 for (i = 0; i < codec->cvt_setups.used; i++) {
703 p = snd_array_elem(&codec->cvt_setups, i);
704 if (p->nid == nid)
705 return p;
706 }
707 p = snd_array_new(&codec->cvt_setups);
708 if (p)
709 p->nid = nid;
710 return p;
711}
712
713/*
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100714 * PCM device
715 */
716static void release_pcm(struct kref *kref)
717{
718 struct hda_pcm *pcm = container_of(kref, struct hda_pcm, kref);
719
720 if (pcm->pcm)
721 snd_device_free(pcm->codec->card, pcm->pcm);
722 clear_bit(pcm->device, pcm->codec->bus->pcm_dev_bits);
723 kfree(pcm->name);
724 kfree(pcm);
725}
726
727void snd_hda_codec_pcm_put(struct hda_pcm *pcm)
728{
729 kref_put(&pcm->kref, release_pcm);
730}
731EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_put);
732
733struct hda_pcm *snd_hda_codec_pcm_new(struct hda_codec *codec,
734 const char *fmt, ...)
735{
736 struct hda_pcm *pcm;
737 va_list args;
738
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100739 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
740 if (!pcm)
741 return NULL;
742
743 pcm->codec = codec;
744 kref_init(&pcm->kref);
Takashi Iwai30e5f002015-04-27 16:39:19 +0200745 va_start(args, fmt);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100746 pcm->name = kvasprintf(GFP_KERNEL, fmt, args);
Takashi Iwai30e5f002015-04-27 16:39:19 +0200747 va_end(args);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100748 if (!pcm->name) {
749 kfree(pcm);
750 return NULL;
751 }
752
753 list_add_tail(&pcm->list, &codec->pcm_list_head);
754 return pcm;
755}
756EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_new);
757
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100758/*
759 * codec destructor
760 */
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100761static void codec_release_pcms(struct hda_codec *codec)
762{
763 struct hda_pcm *pcm, *n;
764
765 list_for_each_entry_safe(pcm, n, &codec->pcm_list_head, list) {
766 list_del_init(&pcm->list);
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100767 if (pcm->pcm)
768 snd_device_disconnect(codec->card, pcm->pcm);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100769 snd_hda_codec_pcm_put(pcm);
770 }
771}
772
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100773void snd_hda_codec_cleanup_for_unbind(struct hda_codec *codec)
774{
Takashi Iwaic4c25332015-03-03 17:22:12 +0100775 if (codec->registered) {
776 /* pm_runtime_put() is called in snd_hdac_device_exit() */
777 pm_runtime_get_noresume(hda_codec_dev(codec));
778 pm_runtime_disable(hda_codec_dev(codec));
779 codec->registered = 0;
780 }
781
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100782 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100783 if (!codec->in_freeing)
784 snd_hda_ctls_clear(codec);
785 codec_release_pcms(codec);
786 snd_hda_detach_beep_device(codec);
787 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
788 snd_hda_jack_tbl_clear(codec);
789 codec->proc_widget_hook = NULL;
790 codec->spec = NULL;
791
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100792 /* free only driver_pins so that init_pins + user_pins are restored */
793 snd_array_free(&codec->driver_pins);
794 snd_array_free(&codec->cvt_setups);
795 snd_array_free(&codec->spdif_out);
796 snd_array_free(&codec->verbs);
797 codec->preset = NULL;
798 codec->slave_dig_outs = NULL;
799 codec->spdif_status_reset = 0;
800 snd_array_free(&codec->mixers);
801 snd_array_free(&codec->nids);
802 remove_conn_list(codec);
Takashi Iwai4d75faa02015-02-25 14:42:38 +0100803 snd_hdac_regmap_exit(&codec->core);
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100804}
805
Takashi Iwaid8193872012-08-31 07:54:38 -0700806static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwaibb6ac722009-03-13 09:02:42 +0100807 unsigned int power_state);
808
Takashi Iwaic4c25332015-03-03 17:22:12 +0100809/* also called from hda_bind.c */
810void snd_hda_codec_register(struct hda_codec *codec)
811{
812 if (codec->registered)
813 return;
814 if (device_is_registered(hda_codec_dev(codec))) {
815 snd_hda_register_beep_device(codec);
Mengdong Lina5e7e072015-04-29 17:43:20 +0800816 snd_hdac_link_power(&codec->core, true);
Takashi Iwaic4c25332015-03-03 17:22:12 +0100817 pm_runtime_enable(hda_codec_dev(codec));
818 /* it was powered up in snd_hda_codec_new(), now all done */
819 snd_hda_power_down(codec);
820 codec->registered = 1;
821 }
822}
823
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100824static int snd_hda_codec_dev_register(struct snd_device *device)
825{
Takashi Iwaic4c25332015-03-03 17:22:12 +0100826 snd_hda_codec_register(device->device_data);
Takashi Iwaid604b392014-02-28 13:42:09 +0100827 return 0;
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100828}
829
830static int snd_hda_codec_dev_disconnect(struct snd_device *device)
831{
832 struct hda_codec *codec = device->device_data;
833
Takashi Iwaid604b392014-02-28 13:42:09 +0100834 snd_hda_detach_beep_device(codec);
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100835 return 0;
836}
837
Takashi Iwai2565c892014-02-19 11:41:09 +0100838static int snd_hda_codec_dev_free(struct snd_device *device)
839{
Takashi Iwaid56db742015-02-27 23:25:35 +0100840 struct hda_codec *codec = device->device_data;
841
842 codec->in_freeing = 1;
Takashi Iwai3256be62015-02-24 14:59:42 +0100843 snd_hdac_device_unregister(&codec->core);
Takashi Iwaia4577822015-06-11 14:02:49 +0200844 snd_hdac_link_power(&codec->core, false);
Takashi Iwaid56db742015-02-27 23:25:35 +0100845 put_device(hda_codec_dev(codec));
Takashi Iwai2565c892014-02-19 11:41:09 +0100846 return 0;
847}
848
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100849static void snd_hda_codec_dev_release(struct device *dev)
850{
Takashi Iwaid56db742015-02-27 23:25:35 +0100851 struct hda_codec *codec = dev_to_hda_codec(dev);
852
853 free_init_pincfgs(codec);
Takashi Iwai7639a062015-03-03 10:07:24 +0100854 snd_hdac_device_exit(&codec->core);
Takashi Iwaid56db742015-02-27 23:25:35 +0100855 snd_hda_sysfs_clear(codec);
Takashi Iwaid56db742015-02-27 23:25:35 +0100856 kfree(codec->modelname);
857 kfree(codec->wcaps);
Takashi Iwaid56db742015-02-27 23:25:35 +0100858 kfree(codec);
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100859}
860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861/**
862 * snd_hda_codec_new - create a HDA codec
863 * @bus: the bus to assign
864 * @codec_addr: the codec address
865 * @codecp: the pointer to store the generated codec
866 *
867 * Returns 0 if successful, or a negative error code.
868 */
Takashi Iwai6efdd852015-02-27 16:09:22 +0100869int snd_hda_codec_new(struct hda_bus *bus, struct snd_card *card,
870 unsigned int codec_addr, struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
872 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +0200873 char component[31];
Takashi Iwaid8193872012-08-31 07:54:38 -0700874 hda_nid_t fg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 int err;
Takashi Iwai2565c892014-02-19 11:41:09 +0100876 static struct snd_device_ops dev_ops = {
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100877 .dev_register = snd_hda_codec_dev_register,
878 .dev_disconnect = snd_hda_codec_dev_disconnect,
Takashi Iwai2565c892014-02-19 11:41:09 +0100879 .dev_free = snd_hda_codec_dev_free,
880 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Takashi Iwaida3cec32008-08-08 17:12:14 +0200882 if (snd_BUG_ON(!bus))
883 return -EINVAL;
884 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
885 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200887 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Takashi Iwaif4de8fe2015-02-27 16:17:18 +0100888 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Takashi Iwai7639a062015-03-03 10:07:24 +0100891 sprintf(component, "hdaudioC%dD%d", card->number, codec_addr);
892 err = snd_hdac_device_init(&codec->core, &bus->core, component,
893 codec_addr);
894 if (err < 0) {
895 kfree(codec);
896 return err;
897 }
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100898
Takashi Iwai7639a062015-03-03 10:07:24 +0100899 codec->core.dev.release = snd_hda_codec_dev_release;
900 codec->core.type = HDA_DEV_LEGACY;
Takashi Iwai058524482015-03-03 15:40:08 +0100901 codec->core.exec_verb = codec_exec_verb;
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 codec->bus = bus;
Takashi Iwai6efdd852015-02-27 16:09:22 +0100904 codec->card = card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +0100906 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +0800907 mutex_init(&codec->control_mutex);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +0100908 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
909 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +0100910 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +0100911 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +0200912 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Stephen Warren7c935972011-06-01 11:14:17 -0600913 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai361dab32012-05-09 14:35:27 +0200914 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100915 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100916 INIT_LIST_HEAD(&codec->conn_list);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100917 INIT_LIST_HEAD(&codec->pcm_list_head);
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100918
David Henningsson26a6cb62012-10-09 15:04:21 +0200919 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
Mengdong Lin7f132922013-11-29 01:48:45 -0500920 codec->depop_delay = -1;
David Henningssonf5662e12014-07-22 14:09:34 +0200921 codec->fixup_id = HDA_FIXUP_ID_NOT_SET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Takashi Iwai83012a72012-08-24 18:38:08 +0200923#ifdef CONFIG_PM
Takashi Iwaicc72da72015-02-19 16:00:22 +0100924 codec->power_jiffies = jiffies;
Takashi Iwaicb53c622007-08-10 17:21:45 +0200925#endif
926
Takashi Iwai648a8d22014-02-25 10:38:13 +0100927 snd_hda_sysfs_init(codec);
928
Takashi Iwaic382a9f2012-05-07 15:01:02 +0200929 if (codec->bus->modelname) {
930 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
931 if (!codec->modelname) {
Heloise NH6986a0e2015-07-17 09:42:06 +0800932 err = -ENOMEM;
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100933 goto error;
Takashi Iwaic382a9f2012-05-07 15:01:02 +0200934 }
935 }
936
Takashi Iwai7639a062015-03-03 10:07:24 +0100937 fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
Takashi Iwaid8193872012-08-31 07:54:38 -0700938 err = read_widget_caps(codec, fg);
Takashi Iwaif4de8fe2015-02-27 16:17:18 +0100939 if (err < 0)
Takashi Iwai3be14142009-02-20 14:11:16 +0100940 goto error;
Takashi Iwai3be14142009-02-20 14:11:16 +0100941 err = read_pin_defaults(codec);
942 if (err < 0)
943 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +0100944
Takashi Iwaibb6ac722009-03-13 09:02:42 +0100945 /* power-up all before initialization */
Takashi Iwaid8193872012-08-31 07:54:38 -0700946 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaibb6ac722009-03-13 09:02:42 +0100947
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200948 snd_hda_codec_proc_new(codec);
949
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200950 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200951
Takashi Iwai7639a062015-03-03 10:07:24 +0100952 sprintf(component, "HDA:%08x,%08x,%08x", codec->core.vendor_id,
953 codec->core.subsystem_id, codec->core.revision_id);
Takashi Iwai6efdd852015-02-27 16:09:22 +0100954 snd_component_add(card, component);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200955
Takashi Iwai6efdd852015-02-27 16:09:22 +0100956 err = snd_device_new(card, SNDRV_DEV_CODEC, codec, &dev_ops);
Takashi Iwai2565c892014-02-19 11:41:09 +0100957 if (err < 0)
958 goto error;
959
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200960 if (codecp)
961 *codecp = codec;
962 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +0100963
964 error:
Takashi Iwaid56db742015-02-27 23:25:35 +0100965 put_device(hda_codec_dev(codec));
Takashi Iwai3be14142009-02-20 14:11:16 +0100966 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200967}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100968EXPORT_SYMBOL_GPL(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200969
Takashi Iwai95a962c2014-10-29 16:03:58 +0100970/**
971 * snd_hda_codec_update_widgets - Refresh widget caps and pin defaults
972 * @codec: the HDA codec
973 *
974 * Forcibly refresh the all widget caps and the init pin configurations of
975 * the given codec.
976 */
Mengdong Lina15d05d2013-02-08 17:09:31 -0500977int snd_hda_codec_update_widgets(struct hda_codec *codec)
978{
979 hda_nid_t fg;
980 int err;
981
Takashi Iwai9dcc1442015-08-25 08:52:55 +0200982 err = snd_hdac_refresh_widget_sysfs(&codec->core);
Takashi Iwai7639a062015-03-03 10:07:24 +0100983 if (err < 0)
984 return err;
985
Mengdong Lina15d05d2013-02-08 17:09:31 -0500986 /* Assume the function group node does not change,
987 * only the widget nodes may change.
988 */
989 kfree(codec->wcaps);
Takashi Iwai7639a062015-03-03 10:07:24 +0100990 fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
Mengdong Lina15d05d2013-02-08 17:09:31 -0500991 err = read_widget_caps(codec, fg);
Takashi Iwaif4de8fe2015-02-27 16:17:18 +0100992 if (err < 0)
Mengdong Lina15d05d2013-02-08 17:09:31 -0500993 return err;
Mengdong Lina15d05d2013-02-08 17:09:31 -0500994
995 snd_array_free(&codec->init_pins);
996 err = read_pin_defaults(codec);
997
998 return err;
999}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001000EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets);
Mengdong Lina15d05d2013-02-08 17:09:31 -05001001
Takashi Iwaied360812012-08-08 17:12:52 +02001002/* update the stream-id if changed */
1003static void update_pcm_stream_id(struct hda_codec *codec,
1004 struct hda_cvt_setup *p, hda_nid_t nid,
1005 u32 stream_tag, int channel_id)
1006{
1007 unsigned int oldval, newval;
1008
1009 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1010 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1011 newval = (stream_tag << 4) | channel_id;
1012 if (oldval != newval)
1013 snd_hda_codec_write(codec, nid, 0,
1014 AC_VERB_SET_CHANNEL_STREAMID,
1015 newval);
1016 p->stream_tag = stream_tag;
1017 p->channel_id = channel_id;
1018 }
1019}
1020
1021/* update the format-id if changed */
1022static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1023 hda_nid_t nid, int format)
1024{
1025 unsigned int oldval;
1026
1027 if (p->format_id != format) {
1028 oldval = snd_hda_codec_read(codec, nid, 0,
1029 AC_VERB_GET_STREAM_FORMAT, 0);
1030 if (oldval != format) {
1031 msleep(1);
1032 snd_hda_codec_write(codec, nid, 0,
1033 AC_VERB_SET_STREAM_FORMAT,
1034 format);
1035 }
1036 p->format_id = format;
1037 }
1038}
1039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040/**
1041 * snd_hda_codec_setup_stream - set up the codec for streaming
1042 * @codec: the CODEC to set up
1043 * @nid: the NID to set up
1044 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1045 * @channel_id: channel id to pass, zero based.
1046 * @format: stream format.
1047 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001048void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1049 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 int channel_id, int format)
1051{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001052 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001053 struct hda_cvt_setup *p;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001054 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001055 int i;
1056
Takashi Iwai0ba21762007-04-16 11:29:14 +02001057 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001058 return;
1059
Takashi Iwai4e76a882014-02-25 12:21:03 +01001060 codec_dbg(codec,
1061 "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1062 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001063 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001064 if (!p)
Takashi Iwaieb541332010-08-06 13:48:11 +02001065 return;
Takashi Iwaied360812012-08-08 17:12:52 +02001066
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01001067 if (codec->patch_ops.stream_pm)
1068 codec->patch_ops.stream_pm(codec, nid, true);
Takashi Iwaied360812012-08-08 17:12:52 +02001069 if (codec->pcm_format_first)
1070 update_pcm_format(codec, p, nid, format);
1071 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1072 if (!codec->pcm_format_first)
1073 update_pcm_format(codec, p, nid, format);
1074
Takashi Iwaieb541332010-08-06 13:48:11 +02001075 p->active = 1;
1076 p->dirty = 0;
1077
1078 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001079 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwaid068ebc2015-03-02 23:22:59 +01001080 list_for_each_codec(c, codec->bus) {
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001081 for (i = 0; i < c->cvt_setups.used; i++) {
1082 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001083 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001084 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001085 p->dirty = 1;
1086 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001089EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Takashi Iwaif0cea792010-08-13 11:56:53 +02001091static void really_cleanup_stream(struct hda_codec *codec,
1092 struct hda_cvt_setup *q);
1093
Takashi Iwaid5191e52009-11-16 14:58:17 +01001094/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001095 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001096 * @codec: the CODEC to clean up
1097 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001098 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001099 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001100void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1101 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001102{
Takashi Iwaieb541332010-08-06 13:48:11 +02001103 struct hda_cvt_setup *p;
1104
Takashi Iwai888afa12008-03-18 09:57:50 +01001105 if (!nid)
1106 return;
1107
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001108 if (codec->no_sticky_stream)
1109 do_now = 1;
1110
Takashi Iwai4e76a882014-02-25 12:21:03 +01001111 codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001112 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001113 if (p) {
Takashi Iwaif0cea792010-08-13 11:56:53 +02001114 /* here we just clear the active flag when do_now isn't set;
1115 * actual clean-ups will be done later in
1116 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1117 */
1118 if (do_now)
1119 really_cleanup_stream(codec, p);
1120 else
1121 p->active = 0;
1122 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001123}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001124EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001125
Takashi Iwaieb541332010-08-06 13:48:11 +02001126static void really_cleanup_stream(struct hda_codec *codec,
1127 struct hda_cvt_setup *q)
1128{
1129 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001130 if (q->stream_tag || q->channel_id)
1131 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1132 if (q->format_id)
1133 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1134);
Takashi Iwaieb541332010-08-06 13:48:11 +02001135 memset(q, 0, sizeof(*q));
1136 q->nid = nid;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01001137 if (codec->patch_ops.stream_pm)
1138 codec->patch_ops.stream_pm(codec, nid, false);
Takashi Iwaieb541332010-08-06 13:48:11 +02001139}
1140
1141/* clean up the all conflicting obsolete streams */
1142static void purify_inactive_streams(struct hda_codec *codec)
1143{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001144 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001145 int i;
1146
Takashi Iwaid068ebc2015-03-02 23:22:59 +01001147 list_for_each_codec(c, codec->bus) {
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001148 for (i = 0; i < c->cvt_setups.used; i++) {
1149 struct hda_cvt_setup *p;
1150 p = snd_array_elem(&c->cvt_setups, i);
1151 if (p->dirty)
1152 really_cleanup_stream(c, p);
1153 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001154 }
1155}
1156
Takashi Iwai2a439522011-07-26 09:52:50 +02001157#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001158/* clean up all streams; called from suspend */
1159static void hda_cleanup_all_streams(struct hda_codec *codec)
1160{
1161 int i;
1162
1163 for (i = 0; i < codec->cvt_setups.used; i++) {
1164 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1165 if (p->stream_tag)
1166 really_cleanup_stream(codec, p);
1167 }
1168}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001169#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171/*
1172 * amp access functions
1173 */
1174
Takashi Iwaid5191e52009-11-16 14:58:17 +01001175/**
1176 * query_amp_caps - query AMP capabilities
1177 * @codec: the HD-auio codec
1178 * @nid: the NID to query
1179 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1180 *
1181 * Query AMP capabilities for the given widget and direction.
1182 * Returns the obtained capability bits.
1183 *
1184 * When cap bits have been already read, this doesn't read again but
1185 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001187u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188{
Takashi Iwaifaa75f82015-02-26 08:54:56 +01001189 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1190 nid = codec->core.afg;
1191 return snd_hda_param_read(codec, nid,
1192 direction == HDA_OUTPUT ?
1193 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001195EXPORT_SYMBOL_GPL(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Takashi Iwaid5191e52009-11-16 14:58:17 +01001197/**
David Henningsson861a04e2014-09-23 10:38:17 +02001198 * snd_hda_check_amp_caps - query AMP capabilities
1199 * @codec: the HD-audio codec
1200 * @nid: the NID to query
1201 * @dir: either #HDA_INPUT or #HDA_OUTPUT
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001202 * @bits: bit mask to check the result
David Henningsson861a04e2014-09-23 10:38:17 +02001203 *
1204 * Check whether the widget has the given amp capability for the direction.
1205 */
1206bool snd_hda_check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
1207 int dir, unsigned int bits)
1208{
1209 if (!nid)
1210 return false;
1211 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
1212 if (query_amp_caps(codec, nid, dir) & bits)
1213 return true;
1214 return false;
1215}
1216EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps);
1217
1218/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01001219 * snd_hda_override_amp_caps - Override the AMP capabilities
1220 * @codec: the CODEC to clean up
1221 * @nid: the NID to clean up
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001222 * @dir: either #HDA_INPUT or #HDA_OUTPUT
Takashi Iwaid5191e52009-11-16 14:58:17 +01001223 * @caps: the capability bits to set
1224 *
1225 * Override the cached AMP caps bits value by the given one.
1226 * This function is useful if the driver needs to adjust the AMP ranges,
1227 * e.g. limit to 0dB, etc.
1228 *
1229 * Returns zero if successful or a negative error code.
1230 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001231int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1232 unsigned int caps)
1233{
Takashi Iwaifaa75f82015-02-26 08:54:56 +01001234 unsigned int parm;
1235
1236 snd_hda_override_wcaps(codec, nid,
1237 get_wcaps(codec, nid) | AC_WCAP_AMP_OVRD);
1238 parm = dir == HDA_OUTPUT ? AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP;
1239 return snd_hdac_override_parm(&codec->core, nid, parm, caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001240}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001241EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001242
Takashi Iwaid5191e52009-11-16 14:58:17 +01001243/**
Takashi Iwaia686ec42015-06-11 10:51:28 +02001244 * snd_hda_codec_amp_update - update the AMP mono value
1245 * @codec: HD-audio codec
1246 * @nid: NID to read the AMP value
1247 * @ch: channel to update (0 or 1)
1248 * @dir: #HDA_INPUT or #HDA_OUTPUT
1249 * @idx: the index value (only for input direction)
1250 * @mask: bit mask to set
1251 * @val: the bits value to set
1252 *
1253 * Update the AMP values for the given channel, direction and index.
1254 */
1255int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid,
1256 int ch, int dir, int idx, int mask, int val)
1257{
1258 unsigned int cmd = snd_hdac_regmap_encode_amp(nid, ch, dir, idx);
1259
1260 /* enable fake mute if no h/w mute but min=mute */
1261 if ((query_amp_caps(codec, nid, dir) &
1262 (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) == AC_AMPCAP_MIN_MUTE)
1263 cmd |= AC_AMP_FAKE_MUTE;
1264 return snd_hdac_regmap_update_raw(&codec->core, cmd, mask, val);
1265}
1266EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update);
1267
1268/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01001269 * snd_hda_codec_amp_stereo - update the AMP stereo values
1270 * @codec: HD-audio codec
1271 * @nid: NID to read the AMP value
1272 * @direction: #HDA_INPUT or #HDA_OUTPUT
1273 * @idx: the index value (only for input direction)
1274 * @mask: bit mask to set
1275 * @val: the bits value to set
1276 *
1277 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1278 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001279 */
1280int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1281 int direction, int idx, int mask, int val)
1282{
1283 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001284
1285 if (snd_BUG_ON(mask & ~0xff))
1286 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001287 for (ch = 0; ch < 2; ch++)
1288 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1289 idx, mask, val);
1290 return ret;
1291}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001292EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001293
Takashi Iwai95a962c2014-10-29 16:03:58 +01001294/**
1295 * snd_hda_codec_amp_init - initialize the AMP value
1296 * @codec: the HDA codec
1297 * @nid: NID to read the AMP value
1298 * @ch: channel (left=0 or right=1)
1299 * @dir: #HDA_INPUT or #HDA_OUTPUT
1300 * @idx: the index value (only for input direction)
1301 * @mask: bit mask to set
1302 * @val: the bits value to set
1303 *
1304 * Works like snd_hda_codec_amp_update() but it writes the value only at
Takashi Iwai280e57d2012-12-14 10:32:21 +01001305 * the first access. If the amp was already initialized / updated beforehand,
1306 * this does nothing.
1307 */
1308int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
1309 int dir, int idx, int mask, int val)
1310{
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01001311 int orig;
1312
1313 if (!codec->core.regmap)
1314 return -EINVAL;
1315 regcache_cache_only(codec->core.regmap, true);
1316 orig = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1317 regcache_cache_only(codec->core.regmap, false);
1318 if (orig >= 0)
1319 return 0;
1320 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx, mask, val);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001321}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001322EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001323
Takashi Iwai95a962c2014-10-29 16:03:58 +01001324/**
1325 * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value
1326 * @codec: the HDA codec
1327 * @nid: NID to read the AMP value
1328 * @dir: #HDA_INPUT or #HDA_OUTPUT
1329 * @idx: the index value (only for input direction)
1330 * @mask: bit mask to set
1331 * @val: the bits value to set
1332 *
1333 * Call snd_hda_codec_amp_init() for both stereo channels.
1334 */
Takashi Iwai280e57d2012-12-14 10:32:21 +01001335int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
1336 int dir, int idx, int mask, int val)
1337{
1338 int ch, ret = 0;
1339
1340 if (snd_BUG_ON(mask & ~0xff))
1341 mask &= 0xff;
1342 for (ch = 0; ch < 2; ch++)
1343 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
1344 idx, mask, val);
1345 return ret;
1346}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001347EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001348
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001349static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1350 unsigned int ofs)
1351{
1352 u32 caps = query_amp_caps(codec, nid, dir);
1353 /* get num steps */
1354 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1355 if (ofs < caps)
1356 caps -= ofs;
1357 return caps;
1358}
1359
Takashi Iwaid5191e52009-11-16 14:58:17 +01001360/**
1361 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001362 * @kcontrol: referred ctl element
1363 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01001364 *
1365 * The control element is supposed to have the private_value field
1366 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1367 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001368int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1369 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370{
1371 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1372 u16 nid = get_amp_nid(kcontrol);
1373 u8 chs = get_amp_channels(kcontrol);
1374 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001375 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001377 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1378 uinfo->count = chs == 3 ? 2 : 1;
1379 uinfo->value.integer.min = 0;
1380 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1381 if (!uinfo->value.integer.max) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001382 codec_warn(codec,
1383 "num_steps = 0 for NID=0x%x (ctl = %s)\n",
1384 nid, kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 return -EINVAL;
1386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 return 0;
1388}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001389EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001391
1392static inline unsigned int
1393read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1394 int ch, int dir, int idx, unsigned int ofs)
1395{
1396 unsigned int val;
1397 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1398 val &= HDA_AMP_VOLMASK;
1399 if (val >= ofs)
1400 val -= ofs;
1401 else
1402 val = 0;
1403 return val;
1404}
1405
1406static inline int
1407update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1408 int ch, int dir, int idx, unsigned int ofs,
1409 unsigned int val)
1410{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001411 unsigned int maxval;
1412
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001413 if (val > 0)
1414 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02001415 /* ofs = 0: raw max value */
1416 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001417 if (val > maxval)
1418 val = maxval;
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01001419 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1420 HDA_AMP_VOLMASK, val);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001421}
1422
Takashi Iwaid5191e52009-11-16 14:58:17 +01001423/**
1424 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001425 * @kcontrol: ctl element
1426 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01001427 *
1428 * The control element is supposed to have the private_value field
1429 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1430 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001431int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1432 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
1434 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1435 hda_nid_t nid = get_amp_nid(kcontrol);
1436 int chs = get_amp_channels(kcontrol);
1437 int dir = get_amp_direction(kcontrol);
1438 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001439 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 long *valp = ucontrol->value.integer.value;
1441
1442 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001443 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001445 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 return 0;
1447}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001448EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Takashi Iwaid5191e52009-11-16 14:58:17 +01001450/**
1451 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001452 * @kcontrol: ctl element
1453 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01001454 *
1455 * The control element is supposed to have the private_value field
1456 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1457 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001458int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1459 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460{
1461 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1462 hda_nid_t nid = get_amp_nid(kcontrol);
1463 int chs = get_amp_channels(kcontrol);
1464 int dir = get_amp_direction(kcontrol);
1465 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001466 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 long *valp = ucontrol->value.integer.value;
1468 int change = 0;
1469
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001470 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001471 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001472 valp++;
1473 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001474 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001475 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 return change;
1477}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001478EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Takashi Iwaid5191e52009-11-16 14:58:17 +01001480/**
1481 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001482 * @kcontrol: ctl element
1483 * @op_flag: operation flag
1484 * @size: byte size of input TLV
1485 * @_tlv: TLV data
Takashi Iwaid5191e52009-11-16 14:58:17 +01001486 *
1487 * The control element is supposed to have the private_value field
1488 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1489 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001490int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1491 unsigned int size, unsigned int __user *_tlv)
1492{
1493 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1494 hda_nid_t nid = get_amp_nid(kcontrol);
1495 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001496 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02001497 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001498 u32 caps, val1, val2;
1499
1500 if (size < 4 * sizeof(unsigned int))
1501 return -ENOMEM;
1502 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001503 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1504 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001505 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001506 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001507 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01001508 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02001509 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001510 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1511 return -EFAULT;
1512 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1513 return -EFAULT;
1514 if (put_user(val1, _tlv + 2))
1515 return -EFAULT;
1516 if (put_user(val2, _tlv + 3))
1517 return -EFAULT;
1518 return 0;
1519}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001520EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001521
Takashi Iwaid5191e52009-11-16 14:58:17 +01001522/**
1523 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
1524 * @codec: HD-audio codec
1525 * @nid: NID of a reference widget
1526 * @dir: #HDA_INPUT or #HDA_OUTPUT
1527 * @tlv: TLV data to be stored, at least 4 elements
1528 *
1529 * Set (static) TLV data for a virtual master volume using the AMP caps
1530 * obtained from the reference NID.
1531 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01001532 */
1533void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1534 unsigned int *tlv)
1535{
1536 u32 caps;
1537 int nums, step;
1538
1539 caps = query_amp_caps(codec, nid, dir);
1540 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1541 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1542 step = (step + 1) * 25;
1543 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1544 tlv[1] = 2 * sizeof(unsigned int);
1545 tlv[2] = -nums * step;
1546 tlv[3] = step;
1547}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001548EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001549
1550/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01001551static struct snd_kcontrol *
Takashi Iwaidcda5802012-10-12 17:24:51 +02001552find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01001553{
1554 struct snd_ctl_elem_id id;
1555 memset(&id, 0, sizeof(id));
1556 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwaidcda5802012-10-12 17:24:51 +02001557 id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01001558 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02001559 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
1560 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01001561 strcpy(id.name, name);
Takashi Iwai6efdd852015-02-27 16:09:22 +01001562 return snd_ctl_find_id(codec->card, &id);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001563}
1564
Takashi Iwaid5191e52009-11-16 14:58:17 +01001565/**
1566 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
1567 * @codec: HD-audio codec
1568 * @name: ctl id name string
1569 *
1570 * Get the control element with the given id string and IFACE_MIXER.
1571 */
Takashi Iwai09f99702008-02-04 12:31:13 +01001572struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1573 const char *name)
1574{
Takashi Iwaidcda5802012-10-12 17:24:51 +02001575 return find_mixer_ctl(codec, name, 0, 0);
Takashi Iwai09f99702008-02-04 12:31:13 +01001576}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001577EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01001578
Takashi Iwaidcda5802012-10-12 17:24:51 +02001579static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01001580 int start_idx)
Takashi Iwai1afe2062010-12-23 10:17:52 +01001581{
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01001582 int i, idx;
1583 /* 16 ctlrs should be large enough */
1584 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
1585 if (!find_mixer_ctl(codec, name, 0, idx))
Takashi Iwai1afe2062010-12-23 10:17:52 +01001586 return idx;
1587 }
1588 return -EBUSY;
1589}
1590
Takashi Iwaid5191e52009-11-16 14:58:17 +01001591/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001592 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01001593 * @codec: HD-audio codec
1594 * @nid: corresponding NID (optional)
1595 * @kctl: the control element to assign
1596 *
1597 * Add the given control element to an array inside the codec instance.
1598 * All control elements belonging to a codec are supposed to be added
1599 * by this function so that a proper clean-up works at the free or
1600 * reconfiguration time.
1601 *
1602 * If non-zero @nid is passed, the NID is assigned to the control element.
1603 * The assignment is shown in the codec proc file.
1604 *
1605 * snd_hda_ctl_add() checks the control subdev id field whether
1606 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001607 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
1608 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01001609 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001610int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
1611 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02001612{
1613 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001614 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001615 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001616
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001617 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001618 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001619 if (nid == 0)
1620 nid = get_amp_nid_(kctl->private_value);
1621 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001622 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
1623 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001624 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01001625 kctl->id.subdevice = 0;
Takashi Iwai6efdd852015-02-27 16:09:22 +01001626 err = snd_ctl_add(codec->card, kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001627 if (err < 0)
1628 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001629 item = snd_array_new(&codec->mixers);
1630 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02001631 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001632 item->kctl = kctl;
1633 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001634 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001635 return 0;
1636}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001637EXPORT_SYMBOL_GPL(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001638
Takashi Iwaid5191e52009-11-16 14:58:17 +01001639/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001640 * snd_hda_add_nid - Assign a NID to a control element
1641 * @codec: HD-audio codec
1642 * @nid: corresponding NID (optional)
1643 * @kctl: the control element to assign
1644 * @index: index to kctl
1645 *
1646 * Add the given control element to an array inside the codec instance.
1647 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
1648 * NID:KCTL mapping - for example "Capture Source" selector.
1649 */
1650int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
1651 unsigned int index, hda_nid_t nid)
1652{
1653 struct hda_nid_item *item;
1654
1655 if (nid > 0) {
1656 item = snd_array_new(&codec->nids);
1657 if (!item)
1658 return -ENOMEM;
1659 item->kctl = kctl;
1660 item->index = index;
1661 item->nid = nid;
1662 return 0;
1663 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01001664 codec_err(codec, "no NID for mapping control %s:%d:%d\n",
1665 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001666 return -EINVAL;
1667}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001668EXPORT_SYMBOL_GPL(snd_hda_add_nid);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001669
1670/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01001671 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
1672 * @codec: HD-audio codec
1673 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02001674void snd_hda_ctls_clear(struct hda_codec *codec)
1675{
1676 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001677 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001678 for (i = 0; i < codec->mixers.used; i++)
Takashi Iwai6efdd852015-02-27 16:09:22 +01001679 snd_ctl_remove(codec->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001680 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001681 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001682}
1683
Takashi Iwai95a962c2014-10-29 16:03:58 +01001684/**
1685 * snd_hda_lock_devices - pseudo device locking
1686 * @bus: the BUS
1687 *
Takashi Iwaia65d6292009-02-23 16:57:04 +01001688 * toggle card->shutdown to allow/disallow the device access (as a hack)
1689 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001690int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001691{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001692 struct snd_card *card = bus->card;
1693 struct hda_codec *codec;
1694
Takashi Iwaia65d6292009-02-23 16:57:04 +01001695 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001696 if (card->shutdown)
1697 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01001698 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001699 if (!list_empty(&card->ctl_files))
1700 goto err_clear;
1701
Takashi Iwaid068ebc2015-03-02 23:22:59 +01001702 list_for_each_codec(codec, bus) {
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01001703 struct hda_pcm *cpcm;
1704 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001705 if (!cpcm->pcm)
1706 continue;
1707 if (cpcm->pcm->streams[0].substream_opened ||
1708 cpcm->pcm->streams[1].substream_opened)
1709 goto err_clear;
1710 }
1711 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01001712 spin_unlock(&card->files_lock);
1713 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01001714
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001715 err_clear:
1716 card->shutdown = 0;
1717 err_unlock:
1718 spin_unlock(&card->files_lock);
1719 return -EINVAL;
1720}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001721EXPORT_SYMBOL_GPL(snd_hda_lock_devices);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001722
Takashi Iwai95a962c2014-10-29 16:03:58 +01001723/**
1724 * snd_hda_unlock_devices - pseudo device unlocking
1725 * @bus: the BUS
1726 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001727void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01001728{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001729 struct snd_card *card = bus->card;
1730
Takashi Iwaia65d6292009-02-23 16:57:04 +01001731 spin_lock(&card->files_lock);
1732 card->shutdown = 0;
1733 spin_unlock(&card->files_lock);
1734}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001735EXPORT_SYMBOL_GPL(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01001736
Takashi Iwaid5191e52009-11-16 14:58:17 +01001737/**
1738 * snd_hda_codec_reset - Clear all objects assigned to the codec
1739 * @codec: HD-audio codec
1740 *
1741 * This frees the all PCM and control elements assigned to the codec, and
1742 * clears the caches and restores the pin default configurations.
1743 *
1744 * When a device is being used, it returns -EBSY. If successfully freed,
1745 * returns zero.
1746 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01001747int snd_hda_codec_reset(struct hda_codec *codec)
1748{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001749 struct hda_bus *bus = codec->bus;
Takashi Iwaia65d6292009-02-23 16:57:04 +01001750
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001751 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01001752 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01001753
1754 /* OK, let it free */
Takashi Iwai3256be62015-02-24 14:59:42 +01001755 snd_hdac_device_unregister(&codec->core);
Takashi Iwaid8a766a2015-02-17 15:25:37 +01001756
Takashi Iwaia65d6292009-02-23 16:57:04 +01001757 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001758 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01001759 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001760}
1761
Takashi Iwai6194b992014-06-06 18:12:16 +02001762typedef int (*map_slave_func_t)(struct hda_codec *, void *, struct snd_kcontrol *);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01001763
1764/* apply the function to all matching slave ctls in the mixer list */
1765static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01001766 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01001767{
1768 struct hda_nid_item *items;
1769 const char * const *s;
1770 int i, err;
1771
1772 items = codec->mixers.list;
1773 for (i = 0; i < codec->mixers.used; i++) {
1774 struct snd_kcontrol *sctl = items[i].kctl;
Takashi Iwaica16ec02013-10-28 12:00:35 +01001775 if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01001776 continue;
1777 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01001778 char tmpname[sizeof(sctl->id.name)];
1779 const char *name = *s;
1780 if (suffix) {
1781 snprintf(tmpname, sizeof(tmpname), "%s %s",
1782 name, suffix);
1783 name = tmpname;
1784 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01001785 if (!strcmp(sctl->id.name, name)) {
Takashi Iwai6194b992014-06-06 18:12:16 +02001786 err = func(codec, data, sctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01001787 if (err)
1788 return err;
1789 break;
1790 }
1791 }
1792 }
1793 return 0;
1794}
1795
Takashi Iwai6194b992014-06-06 18:12:16 +02001796static int check_slave_present(struct hda_codec *codec,
1797 void *data, struct snd_kcontrol *sctl)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01001798{
1799 return 1;
1800}
1801
Takashi Iwai18478e82012-03-09 17:51:10 +01001802/* guess the value corresponding to 0dB */
Takashi Iwai6194b992014-06-06 18:12:16 +02001803static int get_kctl_0dB_offset(struct hda_codec *codec,
1804 struct snd_kcontrol *kctl, int *step_to_check)
Takashi Iwai18478e82012-03-09 17:51:10 +01001805{
1806 int _tlv[4];
1807 const int *tlv = NULL;
1808 int val = -1;
1809
1810 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
1811 /* FIXME: set_fs() hack for obtaining user-space TLV data */
1812 mm_segment_t fs = get_fs();
1813 set_fs(get_ds());
1814 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
1815 tlv = _tlv;
1816 set_fs(fs);
1817 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
1818 tlv = kctl->tlv.p;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01001819 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) {
1820 int step = tlv[3];
1821 step &= ~TLV_DB_SCALE_MUTE;
1822 if (!step)
1823 return -1;
Takashi Iwai485e3e0c2013-11-04 15:51:00 +01001824 if (*step_to_check && *step_to_check != step) {
Takashi Iwai6194b992014-06-06 18:12:16 +02001825 codec_err(codec, "Mismatching dB step for vmaster slave (%d!=%d)\n",
Takashi Iwai4e76a882014-02-25 12:21:03 +01001826- *step_to_check, step);
Takashi Iwai485e3e0c2013-11-04 15:51:00 +01001827 return -1;
1828 }
1829 *step_to_check = step;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01001830 val = -tlv[2] / step;
1831 }
Takashi Iwai18478e82012-03-09 17:51:10 +01001832 return val;
1833}
1834
1835/* call kctl->put with the given value(s) */
1836static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
1837{
1838 struct snd_ctl_elem_value *ucontrol;
1839 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
1840 if (!ucontrol)
1841 return -ENOMEM;
1842 ucontrol->value.integer.value[0] = val;
1843 ucontrol->value.integer.value[1] = val;
1844 kctl->put(kctl, ucontrol);
1845 kfree(ucontrol);
1846 return 0;
1847}
1848
1849/* initialize the slave volume with 0dB */
Takashi Iwai6194b992014-06-06 18:12:16 +02001850static int init_slave_0dB(struct hda_codec *codec,
1851 void *data, struct snd_kcontrol *slave)
Takashi Iwai18478e82012-03-09 17:51:10 +01001852{
Takashi Iwai6194b992014-06-06 18:12:16 +02001853 int offset = get_kctl_0dB_offset(codec, slave, data);
Takashi Iwai18478e82012-03-09 17:51:10 +01001854 if (offset > 0)
1855 put_kctl_with_value(slave, offset);
1856 return 0;
1857}
1858
1859/* unmute the slave */
Takashi Iwai6194b992014-06-06 18:12:16 +02001860static int init_slave_unmute(struct hda_codec *codec,
1861 void *data, struct snd_kcontrol *slave)
Takashi Iwai18478e82012-03-09 17:51:10 +01001862{
1863 return put_kctl_with_value(slave, 1);
1864}
1865
Takashi Iwaie8750942014-06-30 14:02:39 +02001866static int add_slave(struct hda_codec *codec,
1867 void *data, struct snd_kcontrol *slave)
1868{
1869 return snd_ctl_add_slave(data, slave);
1870}
1871
Takashi Iwaid5191e52009-11-16 14:58:17 +01001872/**
Takashi Iwai95a962c2014-10-29 16:03:58 +01001873 * __snd_hda_add_vmaster - create a virtual master control and add slaves
Takashi Iwaid5191e52009-11-16 14:58:17 +01001874 * @codec: HD-audio codec
1875 * @name: vmaster control name
1876 * @tlv: TLV data (optional)
1877 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01001878 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01001879 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01001880 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01001881 *
1882 * Create a virtual master control with the given name. The TLV data
1883 * must be either NULL or a valid data.
1884 *
1885 * @slaves is a NULL-terminated array of strings, each of which is a
1886 * slave control name. All controls with these names are assigned to
1887 * the new virtual master control.
1888 *
1889 * This function returns zero if successful or a negative error code.
1890 */
Takashi Iwai18478e82012-03-09 17:51:10 +01001891int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01001892 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01001893 const char *suffix, bool init_slave_vol,
1894 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01001895{
1896 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01001897 int err;
1898
Takashi Iwai29e58532012-03-12 12:25:03 +01001899 if (ctl_ret)
1900 *ctl_ret = NULL;
1901
Takashi Iwai9322ca52012-02-03 14:28:01 +01001902 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01001903 if (err != 1) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001904 codec_dbg(codec, "No slave found for %s\n", name);
Takashi Iwai2f085542008-02-22 18:43:50 +01001905 return 0;
1906 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01001907 kctl = snd_ctl_make_virtual_master(name, tlv);
1908 if (!kctl)
1909 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001910 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001911 if (err < 0)
1912 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001913
Takashi Iwaie8750942014-06-30 14:02:39 +02001914 err = map_slaves(codec, slaves, suffix, add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01001915 if (err < 0)
1916 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01001917
1918 /* init with master mute & zero volume */
1919 put_kctl_with_value(kctl, 0);
Takashi Iwai485e3e0c2013-11-04 15:51:00 +01001920 if (init_slave_vol) {
1921 int step = 0;
Takashi Iwai18478e82012-03-09 17:51:10 +01001922 map_slaves(codec, slaves, suffix,
Takashi Iwai485e3e0c2013-11-04 15:51:00 +01001923 tlv ? init_slave_0dB : init_slave_unmute, &step);
1924 }
Takashi Iwai18478e82012-03-09 17:51:10 +01001925
Takashi Iwai29e58532012-03-12 12:25:03 +01001926 if (ctl_ret)
1927 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01001928 return 0;
1929}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001930EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001931
Takashi Iwaid2f344b2012-03-12 16:59:58 +01001932/*
1933 * mute-LED control using vmaster
1934 */
1935static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
1936 struct snd_ctl_elem_info *uinfo)
1937{
1938 static const char * const texts[] = {
David Henningssonc86c2d42013-01-03 14:12:29 +01001939 "On", "Off", "Follow Master"
Takashi Iwaid2f344b2012-03-12 16:59:58 +01001940 };
Takashi Iwaid2f344b2012-03-12 16:59:58 +01001941
Takashi Iwai3ff72212014-10-20 18:17:28 +02001942 return snd_ctl_enum_info(uinfo, 1, 3, texts);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01001943}
1944
1945static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
1946 struct snd_ctl_elem_value *ucontrol)
1947{
1948 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
1949 ucontrol->value.enumerated.item[0] = hook->mute_mode;
1950 return 0;
1951}
1952
1953static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
1954 struct snd_ctl_elem_value *ucontrol)
1955{
1956 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
1957 unsigned int old_mode = hook->mute_mode;
1958
1959 hook->mute_mode = ucontrol->value.enumerated.item[0];
1960 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
1961 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
1962 if (old_mode == hook->mute_mode)
1963 return 0;
1964 snd_hda_sync_vmaster_hook(hook);
1965 return 1;
1966}
1967
1968static struct snd_kcontrol_new vmaster_mute_mode = {
1969 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1970 .name = "Mute-LED Mode",
1971 .info = vmaster_mute_mode_info,
1972 .get = vmaster_mute_mode_get,
1973 .put = vmaster_mute_mode_put,
1974};
1975
Takashi Iwaiee52e562015-04-27 10:36:11 +02001976/* meta hook to call each driver's vmaster hook */
1977static void vmaster_hook(void *private_data, int enabled)
1978{
1979 struct hda_vmaster_mute_hook *hook = private_data;
1980
1981 if (hook->mute_mode != HDA_VMUTE_FOLLOW_MASTER)
1982 enabled = hook->mute_mode;
1983 hook->hook(hook->codec, enabled);
1984}
1985
Takashi Iwai95a962c2014-10-29 16:03:58 +01001986/**
1987 * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED
1988 * @codec: the HDA codec
1989 * @hook: the vmaster hook object
1990 * @expose_enum_ctl: flag to create an enum ctl
1991 *
1992 * Add a mute-LED hook with the given vmaster switch kctl.
1993 * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically
1994 * created and associated with the given hook.
Takashi Iwaid2f344b2012-03-12 16:59:58 +01001995 */
1996int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735cb2012-03-13 07:55:10 +01001997 struct hda_vmaster_mute_hook *hook,
1998 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01001999{
2000 struct snd_kcontrol *kctl;
2001
2002 if (!hook->hook || !hook->sw_kctl)
2003 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002004 hook->codec = codec;
2005 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaiee52e562015-04-27 10:36:11 +02002006 snd_ctl_add_vmaster_hook(hook->sw_kctl, vmaster_hook, hook);
Takashi Iwaif29735cb2012-03-13 07:55:10 +01002007 if (!expose_enum_ctl)
2008 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002009 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2010 if (!kctl)
2011 return -ENOMEM;
2012 return snd_hda_ctl_add(codec, 0, kctl);
2013}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002014EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002015
Takashi Iwai95a962c2014-10-29 16:03:58 +01002016/**
2017 * snd_hda_sync_vmaster_hook - Sync vmaster hook
2018 * @hook: the vmaster hook
2019 *
2020 * Call the hook with the current value for synchronization.
2021 * Should be called in init callback.
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002022 */
2023void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2024{
2025 if (!hook->hook || !hook->codec)
2026 return;
Takashi Iwai594813f2013-04-17 18:16:05 +02002027 /* don't call vmaster hook in the destructor since it might have
2028 * been already destroyed
2029 */
2030 if (hook->codec->bus->shutdown)
2031 return;
Takashi Iwaiee52e562015-04-27 10:36:11 +02002032 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002033}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002034EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002035
2036
Takashi Iwaid5191e52009-11-16 14:58:17 +01002037/**
2038 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002039 * @kcontrol: referred ctl element
2040 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002041 *
2042 * The control element is supposed to have the private_value field
2043 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2044 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002045int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2046 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047{
2048 int chs = get_amp_channels(kcontrol);
2049
2050 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2051 uinfo->count = chs == 3 ? 2 : 1;
2052 uinfo->value.integer.min = 0;
2053 uinfo->value.integer.max = 1;
2054 return 0;
2055}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002056EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057
Takashi Iwaid5191e52009-11-16 14:58:17 +01002058/**
2059 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002060 * @kcontrol: ctl element
2061 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002062 *
2063 * The control element is supposed to have the private_value field
2064 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2065 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002066int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2067 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068{
2069 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2070 hda_nid_t nid = get_amp_nid(kcontrol);
2071 int chs = get_amp_channels(kcontrol);
2072 int dir = get_amp_direction(kcontrol);
2073 int idx = get_amp_index(kcontrol);
2074 long *valp = ucontrol->value.integer.value;
2075
2076 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002077 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002078 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002080 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002081 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 return 0;
2083}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002084EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
Takashi Iwaid5191e52009-11-16 14:58:17 +01002086/**
2087 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002088 * @kcontrol: ctl element
2089 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002090 *
2091 * The control element is supposed to have the private_value field
2092 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2093 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002094int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2095 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096{
2097 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2098 hda_nid_t nid = get_amp_nid(kcontrol);
2099 int chs = get_amp_channels(kcontrol);
2100 int dir = get_amp_direction(kcontrol);
2101 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 long *valp = ucontrol->value.integer.value;
2103 int change = 0;
2104
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002105 if (chs & 1) {
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01002106 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
2107 HDA_AMP_MUTE,
2108 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002109 valp++;
2110 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002111 if (chs & 2)
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01002112 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
2113 HDA_AMP_MUTE,
2114 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002115 hda_call_check_power_status(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 return change;
2117}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002118EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
2120/*
Takashi Iwai985be542005-11-02 18:26:49 +01002121 * bound volume controls
2122 *
2123 * bind multiple volumes (# indices, from 0)
2124 */
2125
2126#define AMP_VAL_IDX_SHIFT 19
2127#define AMP_VAL_IDX_MASK (0x0f<<19)
2128
Takashi Iwaid5191e52009-11-16 14:58:17 +01002129/**
2130 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002131 * @kcontrol: ctl element
2132 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002133 *
2134 * The control element is supposed to have the private_value field
2135 * set up via HDA_BIND_MUTE*() macros.
2136 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002137int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2138 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002139{
2140 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2141 unsigned long pval;
2142 int err;
2143
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002144 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002145 pval = kcontrol->private_value;
2146 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2147 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2148 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002149 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002150 return err;
2151}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002152EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002153
Takashi Iwaid5191e52009-11-16 14:58:17 +01002154/**
2155 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002156 * @kcontrol: ctl element
2157 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002158 *
2159 * The control element is supposed to have the private_value field
2160 * set up via HDA_BIND_MUTE*() macros.
2161 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002162int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2163 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002164{
2165 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2166 unsigned long pval;
2167 int i, indices, err = 0, change = 0;
2168
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002169 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002170 pval = kcontrol->private_value;
2171 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2172 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002173 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2174 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002175 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2176 if (err < 0)
2177 break;
2178 change |= err;
2179 }
2180 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002181 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002182 return err < 0 ? err : change;
2183}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002184EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002185
Takashi Iwaid5191e52009-11-16 14:58:17 +01002186/**
2187 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002188 * @kcontrol: referred ctl element
2189 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002190 *
2191 * The control element is supposed to have the private_value field
2192 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002193 */
2194int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2195 struct snd_ctl_elem_info *uinfo)
2196{
2197 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2198 struct hda_bind_ctls *c;
2199 int err;
2200
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002201 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002202 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002203 kcontrol->private_value = *c->values;
2204 err = c->ops->info(kcontrol, uinfo);
2205 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002206 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002207 return err;
2208}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002209EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002210
Takashi Iwaid5191e52009-11-16 14:58:17 +01002211/**
2212 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002213 * @kcontrol: ctl element
2214 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002215 *
2216 * The control element is supposed to have the private_value field
2217 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2218 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002219int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2220 struct snd_ctl_elem_value *ucontrol)
2221{
2222 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2223 struct hda_bind_ctls *c;
2224 int err;
2225
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002226 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002227 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002228 kcontrol->private_value = *c->values;
2229 err = c->ops->get(kcontrol, ucontrol);
2230 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002231 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002232 return err;
2233}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002234EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002235
Takashi Iwaid5191e52009-11-16 14:58:17 +01002236/**
2237 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002238 * @kcontrol: ctl element
2239 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002240 *
2241 * The control element is supposed to have the private_value field
2242 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2243 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002244int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2245 struct snd_ctl_elem_value *ucontrol)
2246{
2247 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2248 struct hda_bind_ctls *c;
2249 unsigned long *vals;
2250 int err = 0, change = 0;
2251
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002252 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002253 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002254 for (vals = c->values; *vals; vals++) {
2255 kcontrol->private_value = *vals;
2256 err = c->ops->put(kcontrol, ucontrol);
2257 if (err < 0)
2258 break;
2259 change |= err;
2260 }
2261 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002262 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002263 return err < 0 ? err : change;
2264}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002265EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002266
Takashi Iwaid5191e52009-11-16 14:58:17 +01002267/**
2268 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002269 * @kcontrol: ctl element
2270 * @op_flag: operation flag
2271 * @size: byte size of input TLV
2272 * @tlv: TLV data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002273 *
2274 * The control element is supposed to have the private_value field
2275 * set up via HDA_BIND_VOL() macro.
2276 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002277int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2278 unsigned int size, unsigned int __user *tlv)
2279{
2280 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2281 struct hda_bind_ctls *c;
2282 int err;
2283
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002284 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002285 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002286 kcontrol->private_value = *c->values;
2287 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2288 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002289 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002290 return err;
2291}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002292EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002293
2294struct hda_ctl_ops snd_hda_bind_vol = {
2295 .info = snd_hda_mixer_amp_volume_info,
2296 .get = snd_hda_mixer_amp_volume_get,
2297 .put = snd_hda_mixer_amp_volume_put,
2298 .tlv = snd_hda_mixer_amp_tlv
2299};
Takashi Iwai2698ea92013-12-18 07:45:52 +01002300EXPORT_SYMBOL_GPL(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002301
2302struct hda_ctl_ops snd_hda_bind_sw = {
2303 .info = snd_hda_mixer_amp_switch_info,
2304 .get = snd_hda_mixer_amp_switch_get,
2305 .put = snd_hda_mixer_amp_switch_put,
2306 .tlv = snd_hda_mixer_amp_tlv
2307};
Takashi Iwai2698ea92013-12-18 07:45:52 +01002308EXPORT_SYMBOL_GPL(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002309
2310/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 * SPDIF out controls
2312 */
2313
Takashi Iwai0ba21762007-04-16 11:29:14 +02002314static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2315 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316{
2317 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2318 uinfo->count = 1;
2319 return 0;
2320}
2321
Takashi Iwai0ba21762007-04-16 11:29:14 +02002322static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2323 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324{
2325 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2326 IEC958_AES0_NONAUDIO |
2327 IEC958_AES0_CON_EMPHASIS_5015 |
2328 IEC958_AES0_CON_NOT_COPYRIGHT;
2329 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2330 IEC958_AES1_CON_ORIGINAL;
2331 return 0;
2332}
2333
Takashi Iwai0ba21762007-04-16 11:29:14 +02002334static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2335 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336{
2337 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2338 IEC958_AES0_NONAUDIO |
2339 IEC958_AES0_PRO_EMPHASIS_5015;
2340 return 0;
2341}
2342
Takashi Iwai0ba21762007-04-16 11:29:14 +02002343static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2344 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345{
2346 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06002347 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002348 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002350 mutex_lock(&codec->spdif_mutex);
2351 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06002352 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2353 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2354 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2355 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002356 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357
2358 return 0;
2359}
2360
2361/* convert from SPDIF status bits to HDA SPDIF bits
2362 * bit 0 (DigEn) is always set zero (to be filled later)
2363 */
2364static unsigned short convert_from_spdif_status(unsigned int sbits)
2365{
2366 unsigned short val = 0;
2367
2368 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002369 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002371 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002373 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2374 IEC958_AES0_PRO_EMPHASIS_5015)
2375 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002377 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2378 IEC958_AES0_CON_EMPHASIS_5015)
2379 val |= AC_DIG1_EMPHASIS;
2380 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2381 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002383 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2385 }
2386 return val;
2387}
2388
2389/* convert to SPDIF status bits from HDA SPDIF bits
2390 */
2391static unsigned int convert_to_spdif_status(unsigned short val)
2392{
2393 unsigned int sbits = 0;
2394
Takashi Iwai0ba21762007-04-16 11:29:14 +02002395 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002397 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 sbits |= IEC958_AES0_PROFESSIONAL;
2399 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwaia686fd12013-03-20 15:42:00 +01002400 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2402 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002403 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002405 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002407 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2409 sbits |= val & (0x7f << 8);
2410 }
2411 return sbits;
2412}
2413
Takashi Iwai2f728532008-09-25 16:32:41 +02002414/* set digital convert verbs both for the given NID and its slaves */
2415static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaia551d912015-02-26 12:34:49 +01002416 int mask, int val)
Takashi Iwai2f728532008-09-25 16:32:41 +02002417{
Takashi Iwaidda14412011-05-02 11:29:30 +02002418 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02002419
Takashi Iwaia551d912015-02-26 12:34:49 +01002420 snd_hdac_regmap_update(&codec->core, nid, AC_VERB_SET_DIGI_CONVERT_1,
2421 mask, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002422 d = codec->slave_dig_outs;
2423 if (!d)
2424 return;
2425 for (; *d; d++)
Takashi Iwai7d4b5e92015-04-16 15:14:53 +02002426 snd_hdac_regmap_update(&codec->core, *d,
Takashi Iwaia551d912015-02-26 12:34:49 +01002427 AC_VERB_SET_DIGI_CONVERT_1, mask, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002428}
2429
2430static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2431 int dig1, int dig2)
2432{
Takashi Iwaia551d912015-02-26 12:34:49 +01002433 unsigned int mask = 0;
2434 unsigned int val = 0;
2435
2436 if (dig1 != -1) {
2437 mask |= 0xff;
2438 val = dig1;
2439 }
2440 if (dig2 != -1) {
2441 mask |= 0xff00;
2442 val |= dig2 << 8;
2443 }
2444 set_dig_out(codec, nid, mask, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002445}
2446
Takashi Iwai0ba21762007-04-16 11:29:14 +02002447static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2448 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449{
2450 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06002451 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002452 struct hda_spdif_out *spdif;
2453 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 unsigned short val;
2455 int change;
2456
Ingo Molnar62932df2006-01-16 16:34:20 +01002457 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002458 spdif = snd_array_elem(&codec->spdif_out, idx);
2459 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06002460 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2462 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2463 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c935972011-06-01 11:14:17 -06002464 val = convert_from_spdif_status(spdif->status);
2465 val |= spdif->ctls & 1;
2466 change = spdif->ctls != val;
2467 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06002468 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02002469 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01002470 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 return change;
2472}
2473
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002474#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
Takashi Iwai0ba21762007-04-16 11:29:14 +02002476static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2477 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478{
2479 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06002480 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002481 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002483 mutex_lock(&codec->spdif_mutex);
2484 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06002485 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002486 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 return 0;
2488}
2489
Stephen Warren74b654c2011-06-01 11:14:18 -06002490static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
2491 int dig1, int dig2)
2492{
2493 set_dig_out_convert(codec, nid, dig1, dig2);
2494 /* unmute amp switch (if any) */
2495 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
2496 (dig1 & AC_DIG1_ENABLE))
2497 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2498 HDA_AMP_MUTE, 0);
2499}
2500
Takashi Iwai0ba21762007-04-16 11:29:14 +02002501static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2502 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503{
2504 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06002505 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002506 struct hda_spdif_out *spdif;
2507 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 unsigned short val;
2509 int change;
2510
Ingo Molnar62932df2006-01-16 16:34:20 +01002511 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002512 spdif = snd_array_elem(&codec->spdif_out, idx);
2513 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06002514 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02002516 val |= AC_DIG1_ENABLE;
Stephen Warren7c935972011-06-01 11:14:17 -06002517 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06002518 spdif->ctls = val;
2519 if (change && nid != (u16)-1)
2520 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01002521 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 return change;
2523}
2524
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002525static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 {
2527 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2528 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002529 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 .info = snd_hda_spdif_mask_info,
2531 .get = snd_hda_spdif_cmask_get,
2532 },
2533 {
2534 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2535 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002536 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 .info = snd_hda_spdif_mask_info,
2538 .get = snd_hda_spdif_pmask_get,
2539 },
2540 {
2541 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002542 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 .info = snd_hda_spdif_mask_info,
2544 .get = snd_hda_spdif_default_get,
2545 .put = snd_hda_spdif_default_put,
2546 },
2547 {
2548 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002549 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 .info = snd_hda_spdif_out_switch_info,
2551 .get = snd_hda_spdif_out_switch_get,
2552 .put = snd_hda_spdif_out_switch_put,
2553 },
2554 { } /* end */
2555};
2556
2557/**
Takashi Iwaidcda5802012-10-12 17:24:51 +02002558 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 * @codec: the HDA codec
Takashi Iwaidcda5802012-10-12 17:24:51 +02002560 * @associated_nid: NID that new ctls associated with
2561 * @cvt_nid: converter NID
2562 * @type: HDA_PCM_TYPE_*
2563 * Creates controls related with the digital output.
2564 * Called from each patch supporting the digital out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 *
2566 * Returns 0 if successful, or a negative error code.
2567 */
Takashi Iwaidcda5802012-10-12 17:24:51 +02002568int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
2569 hda_nid_t associated_nid,
2570 hda_nid_t cvt_nid,
2571 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572{
2573 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002574 struct snd_kcontrol *kctl;
2575 struct snd_kcontrol_new *dig_mix;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002576 int idx = 0;
Takashi Iwaia551d912015-02-26 12:34:49 +01002577 int val = 0;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002578 const int spdif_index = 16;
Stephen Warren7c935972011-06-01 11:14:17 -06002579 struct hda_spdif_out *spdif;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002580 struct hda_bus *bus = codec->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002582 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02002583 type == HDA_PCM_TYPE_SPDIF) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002584 idx = spdif_index;
2585 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02002586 type == HDA_PCM_TYPE_HDMI) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002587 /* suppose a single SPDIF device */
2588 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2589 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
2590 if (!kctl)
2591 break;
2592 kctl->id.index = spdif_index;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002593 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002594 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002595 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002596 if (!bus->primary_dig_out_type)
2597 bus->primary_dig_out_type = type;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002598
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002599 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
Takashi Iwai1afe2062010-12-23 10:17:52 +01002600 if (idx < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002601 codec_err(codec, "too many IEC958 outputs\n");
Takashi Iwai09f99702008-02-04 12:31:13 +01002602 return -EBUSY;
2603 }
Stephen Warren7c935972011-06-01 11:14:17 -06002604 spdif = snd_array_new(&codec->spdif_out);
Mengdong Lin25336e82013-03-07 14:10:25 -05002605 if (!spdif)
2606 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2608 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01002609 if (!kctl)
2610 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01002611 kctl->id.index = idx;
Stephen Warren7c935972011-06-01 11:14:17 -06002612 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06002613 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002614 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 return err;
2616 }
Stephen Warren74b654c2011-06-01 11:14:18 -06002617 spdif->nid = cvt_nid;
Takashi Iwaia551d912015-02-26 12:34:49 +01002618 snd_hdac_regmap_read(&codec->core, cvt_nid,
2619 AC_VERB_GET_DIGI_CONVERT_1, &val);
2620 spdif->ctls = val;
Stephen Warren7c935972011-06-01 11:14:17 -06002621 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 return 0;
2623}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002624EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625
Takashi Iwai95a962c2014-10-29 16:03:58 +01002626/**
2627 * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID
2628 * @codec: the HDA codec
2629 * @nid: widget NID
2630 *
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002631 * call within spdif_mutex lock
2632 */
Stephen Warren7c935972011-06-01 11:14:17 -06002633struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
2634 hda_nid_t nid)
2635{
2636 int i;
2637 for (i = 0; i < codec->spdif_out.used; i++) {
2638 struct hda_spdif_out *spdif =
2639 snd_array_elem(&codec->spdif_out, i);
2640 if (spdif->nid == nid)
2641 return spdif;
2642 }
2643 return NULL;
2644}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002645EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid);
Stephen Warren7c935972011-06-01 11:14:17 -06002646
Takashi Iwai95a962c2014-10-29 16:03:58 +01002647/**
2648 * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl
2649 * @codec: the HDA codec
2650 * @idx: the SPDIF ctl index
2651 *
2652 * Unassign the widget from the given SPDIF control.
2653 */
Stephen Warren74b654c2011-06-01 11:14:18 -06002654void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
2655{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002656 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06002657
2658 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002659 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06002660 spdif->nid = (u16)-1;
2661 mutex_unlock(&codec->spdif_mutex);
2662}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002663EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign);
Stephen Warren74b654c2011-06-01 11:14:18 -06002664
Takashi Iwai95a962c2014-10-29 16:03:58 +01002665/**
2666 * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID
2667 * @codec: the HDA codec
2668 * @idx: the SPDIF ctl idx
2669 * @nid: widget NID
2670 *
2671 * Assign the widget to the SPDIF control with the given index.
2672 */
Stephen Warren74b654c2011-06-01 11:14:18 -06002673void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
2674{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002675 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06002676 unsigned short val;
2677
2678 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002679 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06002680 if (spdif->nid != nid) {
2681 spdif->nid = nid;
2682 val = spdif->ctls;
2683 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
2684 }
2685 mutex_unlock(&codec->spdif_mutex);
2686}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002687EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign);
Stephen Warren74b654c2011-06-01 11:14:18 -06002688
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689/*
Takashi Iwai9a081602008-02-12 18:37:26 +01002690 * SPDIF sharing with analog output
2691 */
2692static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2693 struct snd_ctl_elem_value *ucontrol)
2694{
2695 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2696 ucontrol->value.integer.value[0] = mout->share_spdif;
2697 return 0;
2698}
2699
2700static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2701 struct snd_ctl_elem_value *ucontrol)
2702{
2703 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2704 mout->share_spdif = !!ucontrol->value.integer.value[0];
2705 return 0;
2706}
2707
2708static struct snd_kcontrol_new spdif_share_sw = {
2709 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2710 .name = "IEC958 Default PCM Playback Switch",
2711 .info = snd_ctl_boolean_mono_info,
2712 .get = spdif_share_sw_get,
2713 .put = spdif_share_sw_put,
2714};
2715
Takashi Iwaid5191e52009-11-16 14:58:17 +01002716/**
2717 * snd_hda_create_spdif_share_sw - create Default PCM switch
2718 * @codec: the HDA codec
2719 * @mout: multi-out instance
2720 */
Takashi Iwai9a081602008-02-12 18:37:26 +01002721int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2722 struct hda_multi_out *mout)
2723{
Mengdong Lin4c7a5482013-03-07 14:11:05 -05002724 struct snd_kcontrol *kctl;
2725
Takashi Iwai9a081602008-02-12 18:37:26 +01002726 if (!mout->dig_out_nid)
2727 return 0;
Mengdong Lin4c7a5482013-03-07 14:11:05 -05002728
2729 kctl = snd_ctl_new1(&spdif_share_sw, mout);
2730 if (!kctl)
2731 return -ENOMEM;
Takashi Iwai9a081602008-02-12 18:37:26 +01002732 /* ATTENTION: here mout is passed as private_data, instead of codec */
Mengdong Lin4c7a5482013-03-07 14:11:05 -05002733 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
Takashi Iwai9a081602008-02-12 18:37:26 +01002734}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002735EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01002736
2737/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 * SPDIF input
2739 */
2740
2741#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2742
Takashi Iwai0ba21762007-04-16 11:29:14 +02002743static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
2744 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745{
2746 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2747
2748 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
2749 return 0;
2750}
2751
Takashi Iwai0ba21762007-04-16 11:29:14 +02002752static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
2753 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754{
2755 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2756 hda_nid_t nid = kcontrol->private_value;
2757 unsigned int val = !!ucontrol->value.integer.value[0];
2758 int change;
2759
Ingo Molnar62932df2006-01-16 16:34:20 +01002760 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002762 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 codec->spdif_in_enable = val;
Takashi Iwaia551d912015-02-26 12:34:49 +01002764 snd_hdac_regmap_write(&codec->core, nid,
2765 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002767 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 return change;
2769}
2770
Takashi Iwai0ba21762007-04-16 11:29:14 +02002771static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
2772 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773{
2774 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2775 hda_nid_t nid = kcontrol->private_value;
Takashi Iwaia551d912015-02-26 12:34:49 +01002776 unsigned int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 unsigned int sbits;
2778
Takashi Iwaia551d912015-02-26 12:34:49 +01002779 snd_hdac_regmap_read(&codec->core, nid,
2780 AC_VERB_GET_DIGI_CONVERT_1, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 sbits = convert_to_spdif_status(val);
2782 ucontrol->value.iec958.status[0] = sbits;
2783 ucontrol->value.iec958.status[1] = sbits >> 8;
2784 ucontrol->value.iec958.status[2] = sbits >> 16;
2785 ucontrol->value.iec958.status[3] = sbits >> 24;
2786 return 0;
2787}
2788
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002789static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 {
2791 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002792 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 .info = snd_hda_spdif_in_switch_info,
2794 .get = snd_hda_spdif_in_switch_get,
2795 .put = snd_hda_spdif_in_switch_put,
2796 },
2797 {
2798 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2799 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002800 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 .info = snd_hda_spdif_mask_info,
2802 .get = snd_hda_spdif_in_status_get,
2803 },
2804 { } /* end */
2805};
2806
2807/**
2808 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
2809 * @codec: the HDA codec
2810 * @nid: audio in widget NID
2811 *
2812 * Creates controls related with the SPDIF input.
2813 * Called from each patch supporting the SPDIF in.
2814 *
2815 * Returns 0 if successful, or a negative error code.
2816 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002817int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818{
2819 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002820 struct snd_kcontrol *kctl;
2821 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01002822 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
Takashi Iwaidcda5802012-10-12 17:24:51 +02002824 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01002825 if (idx < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002826 codec_err(codec, "too many IEC958 inputs\n");
Takashi Iwai09f99702008-02-04 12:31:13 +01002827 return -EBUSY;
2828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
2830 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01002831 if (!kctl)
2832 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002834 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002835 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 return err;
2837 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002838 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01002839 snd_hda_codec_read(codec, nid, 0,
2840 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02002841 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 return 0;
2843}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002844EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845
Takashi Iwai95a962c2014-10-29 16:03:58 +01002846/**
2847 * snd_hda_codec_set_power_to_all - Set the power state to all widgets
2848 * @codec: the HDA codec
2849 * @fg: function group (not used now)
2850 * @power_state: the power state to set (AC_PWRST_*)
2851 *
2852 * Set the given power state to all widgets that have the power control.
2853 * If the codec has power_filter set, it evaluates the power state and
2854 * filter out if it's unchanged as D3.
2855 */
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002856void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
Takashi Iwai9419ab62013-01-24 17:23:35 +01002857 unsigned int power_state)
Takashi Iwai54d17402005-11-21 16:33:22 +01002858{
Takashi Iwai7639a062015-03-03 10:07:24 +01002859 hda_nid_t nid;
Takashi Iwai54d17402005-11-21 16:33:22 +01002860
Takashi Iwai7639a062015-03-03 10:07:24 +01002861 for_each_hda_codec_node(nid, codec) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01002862 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9419ab62013-01-24 17:23:35 +01002863 unsigned int state = power_state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002864 if (!(wcaps & AC_WCAP_POWER))
2865 continue;
Takashi Iwai9419ab62013-01-24 17:23:35 +01002866 if (codec->power_filter) {
2867 state = codec->power_filter(codec, nid, power_state);
2868 if (state != power_state && power_state == AC_PWRST_D3)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002869 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02002870 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002871 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai9419ab62013-01-24 17:23:35 +01002872 state);
Takashi Iwai54d17402005-11-21 16:33:22 +01002873 }
Takashi Iwai54d17402005-11-21 16:33:22 +01002874}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002875EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002876
2877/*
Takashi Iwai432c6412012-08-28 09:59:20 -07002878 * wait until the state is reached, returns the current state
2879 */
2880static unsigned int hda_sync_power_state(struct hda_codec *codec,
2881 hda_nid_t fg,
2882 unsigned int power_state)
2883{
2884 unsigned long end_time = jiffies + msecs_to_jiffies(500);
2885 unsigned int state, actual_state;
2886
2887 for (;;) {
2888 state = snd_hda_codec_read(codec, fg, 0,
2889 AC_VERB_GET_POWER_STATE, 0);
2890 if (state & AC_PWRST_ERROR)
2891 break;
2892 actual_state = (state >> 4) & 0x0f;
2893 if (actual_state == power_state)
2894 break;
2895 if (time_after_eq(jiffies, end_time))
2896 break;
2897 /* wait until the codec reachs to the target state */
2898 msleep(1);
2899 }
2900 return state;
2901}
2902
Takashi Iwai95a962c2014-10-29 16:03:58 +01002903/**
2904 * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD
2905 * @codec: the HDA codec
2906 * @nid: widget NID
2907 * @power_state: power state to evalue
2908 *
2909 * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set.
2910 * This can be used a codec power_filter callback.
2911 */
Takashi Iwaiba615b82013-03-13 14:47:21 +01002912unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
2913 hda_nid_t nid,
2914 unsigned int power_state)
Takashi Iwai9419ab62013-01-24 17:23:35 +01002915{
Takashi Iwai7639a062015-03-03 10:07:24 +01002916 if (nid == codec->core.afg || nid == codec->core.mfg)
Takashi Iwaidfc6e462014-01-13 16:09:57 +01002917 return power_state;
Takashi Iwai9419ab62013-01-24 17:23:35 +01002918 if (power_state == AC_PWRST_D3 &&
2919 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
2920 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
2921 int eapd = snd_hda_codec_read(codec, nid, 0,
2922 AC_VERB_GET_EAPD_BTLENABLE, 0);
2923 if (eapd & 0x02)
2924 return AC_PWRST_D0;
2925 }
2926 return power_state;
2927}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002928EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter);
Takashi Iwai9419ab62013-01-24 17:23:35 +01002929
Takashi Iwai432c6412012-08-28 09:59:20 -07002930/*
Takashi Iwai08fa20a2012-08-31 07:46:56 -07002931 * set power state of the codec, and return the power state
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002932 */
Takashi Iwaid8193872012-08-31 07:54:38 -07002933static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwai08fa20a2012-08-31 07:46:56 -07002934 unsigned int power_state)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002935{
Takashi Iwai7639a062015-03-03 10:07:24 +01002936 hda_nid_t fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
Wang Xingchao09617ce2012-06-08 10:26:08 +08002937 int count;
2938 unsigned int state;
Takashi Iwai63e51fd72013-06-06 14:20:19 +02002939 int flags = 0;
Wang Xingchao09617ce2012-06-08 10:26:08 +08002940
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002941 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08002942 if (power_state == AC_PWRST_D3) {
Mengdong Lin7f132922013-11-29 01:48:45 -05002943 if (codec->depop_delay < 0)
Takashi Iwai7639a062015-03-03 10:07:24 +01002944 msleep(codec_has_epss(codec) ? 10 : 100);
Mengdong Lin7f132922013-11-29 01:48:45 -05002945 else if (codec->depop_delay > 0)
2946 msleep(codec->depop_delay);
Takashi Iwai63e51fd72013-06-06 14:20:19 +02002947 flags = HDA_RW_NO_RESPONSE_FALLBACK;
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08002948 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08002949
2950 /* repeat power states setting at most 10 times*/
2951 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07002952 if (codec->patch_ops.set_power_state)
2953 codec->patch_ops.set_power_state(codec, fg,
2954 power_state);
2955 else {
Takashi Iwaidfc6e462014-01-13 16:09:57 +01002956 state = power_state;
2957 if (codec->power_filter)
2958 state = codec->power_filter(codec, fg, state);
2959 if (state == power_state || power_state != AC_PWRST_D3)
2960 snd_hda_codec_read(codec, fg, flags,
2961 AC_VERB_SET_POWER_STATE,
2962 state);
Takashi Iwai9419ab62013-01-24 17:23:35 +01002963 snd_hda_codec_set_power_to_all(codec, fg, power_state);
Takashi Iwai432c6412012-08-28 09:59:20 -07002964 }
2965 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08002966 if (!(state & AC_PWRST_ERROR))
2967 break;
2968 }
Mengdong Linb8dfc4622012-08-23 17:32:30 +08002969
Takashi Iwai08fa20a2012-08-31 07:46:56 -07002970 return state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002971}
Takashi Iwai54d17402005-11-21 16:33:22 +01002972
Takashi Iwaib9c590b2013-01-24 17:27:32 +01002973/* sync power states of all widgets;
2974 * this is called at the end of codec parsing
2975 */
2976static void sync_power_up_states(struct hda_codec *codec)
2977{
Takashi Iwai7639a062015-03-03 10:07:24 +01002978 hda_nid_t nid;
Takashi Iwaib9c590b2013-01-24 17:27:32 +01002979
Takashi Iwaiba615b82013-03-13 14:47:21 +01002980 /* don't care if no filter is used */
2981 if (!codec->power_filter)
Takashi Iwaib9c590b2013-01-24 17:27:32 +01002982 return;
2983
Takashi Iwai7639a062015-03-03 10:07:24 +01002984 for_each_hda_codec_node(nid, codec) {
Takashi Iwaib9c590b2013-01-24 17:27:32 +01002985 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9040d102013-01-24 17:47:17 +01002986 unsigned int target;
Takashi Iwaib9c590b2013-01-24 17:27:32 +01002987 if (!(wcaps & AC_WCAP_POWER))
2988 continue;
2989 target = codec->power_filter(codec, nid, AC_PWRST_D0);
2990 if (target == AC_PWRST_D0)
2991 continue;
Takashi Iwai9040d102013-01-24 17:47:17 +01002992 if (!snd_hda_check_power_state(codec, nid, target))
Takashi Iwaib9c590b2013-01-24 17:27:32 +01002993 snd_hda_codec_write(codec, nid, 0,
2994 AC_VERB_SET_POWER_STATE, target);
2995 }
2996}
2997
Takashi Iwai648a8d22014-02-25 10:38:13 +01002998#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai11aeff02008-07-30 15:01:46 +02002999/* execute additional init verbs */
3000static void hda_exec_init_verbs(struct hda_codec *codec)
3001{
3002 if (codec->init_verbs.list)
3003 snd_hda_sequence_write(codec, codec->init_verbs.list);
3004}
3005#else
3006static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3007#endif
3008
Takashi Iwai2a439522011-07-26 09:52:50 +02003009#ifdef CONFIG_PM
Takashi Iwaicc72da72015-02-19 16:00:22 +01003010/* update the power on/off account with the current jiffies */
3011static void update_power_acct(struct hda_codec *codec, bool on)
3012{
3013 unsigned long delta = jiffies - codec->power_jiffies;
3014
3015 if (on)
3016 codec->power_on_acct += delta;
3017 else
3018 codec->power_off_acct += delta;
3019 codec->power_jiffies += delta;
3020}
3021
3022void snd_hda_update_power_acct(struct hda_codec *codec)
3023{
3024 update_power_acct(codec, hda_codec_is_power_on(codec));
3025}
3026
Takashi Iwaicb53c622007-08-10 17:21:45 +02003027/*
3028 * call suspend and power-down; used both from PM and power-save
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003029 * this function returns the power state in the end
Takashi Iwaicb53c622007-08-10 17:21:45 +02003030 */
Takashi Iwaicc72da72015-02-19 16:00:22 +01003031static unsigned int hda_call_codec_suspend(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003032{
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003033 unsigned int state;
3034
Takashi Iwai7639a062015-03-03 10:07:24 +01003035 atomic_inc(&codec->core.in_pm);
Takashi Iwai989c3182012-11-19 14:14:58 +01003036
Takashi Iwaicb53c622007-08-10 17:21:45 +02003037 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02003038 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02003039 hda_cleanup_all_streams(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003040 state = hda_set_power_state(codec, AC_PWRST_D3);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003041 update_power_acct(codec, true);
Takashi Iwai7639a062015-03-03 10:07:24 +01003042 atomic_dec(&codec->core.in_pm);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003043 return state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003044}
3045
3046/*
3047 * kick up codec; used both from PM and power-save
3048 */
3049static void hda_call_codec_resume(struct hda_codec *codec)
3050{
Takashi Iwai7639a062015-03-03 10:07:24 +01003051 atomic_inc(&codec->core.in_pm);
Takashi Iwai989c3182012-11-19 14:14:58 +01003052
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01003053 if (codec->core.regmap)
3054 regcache_mark_dirty(codec->core.regmap);
3055
Takashi Iwaicc72da72015-02-19 16:00:22 +01003056 codec->power_jiffies = jiffies;
Takashi Iwaicc72da72015-02-19 16:00:22 +01003057
Takashi Iwaid8193872012-08-31 07:54:38 -07003058 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003059 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003060 hda_exec_init_verbs(codec);
Takashi Iwai31614bb2013-01-23 15:58:40 +01003061 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003062 if (codec->patch_ops.resume)
3063 codec->patch_ops.resume(codec);
3064 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003065 if (codec->patch_ops.init)
3066 codec->patch_ops.init(codec);
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01003067 if (codec->core.regmap)
3068 regcache_sync(codec->core.regmap);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003069 }
David Henningsson26a6cb62012-10-09 15:04:21 +02003070
3071 if (codec->jackpoll_interval)
3072 hda_jackpoll_work(&codec->jackpoll_work.work);
Takashi Iwai31614bb2013-01-23 15:58:40 +01003073 else
David Henningsson26a6cb62012-10-09 15:04:21 +02003074 snd_hda_jack_report_sync(codec);
Takashi Iwai7639a062015-03-03 10:07:24 +01003075 atomic_dec(&codec->core.in_pm);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003076}
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003077
Takashi Iwaicc72da72015-02-19 16:00:22 +01003078static int hda_codec_runtime_suspend(struct device *dev)
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003079{
3080 struct hda_codec *codec = dev_to_hda_codec(dev);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003081 struct hda_pcm *pcm;
Takashi Iwaicc72da72015-02-19 16:00:22 +01003082 unsigned int state;
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003083
3084 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003085 list_for_each_entry(pcm, &codec->pcm_list_head, list)
3086 snd_pcm_suspend_all(pcm->pcm);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003087 state = hda_call_codec_suspend(codec);
Takashi Iwai7639a062015-03-03 10:07:24 +01003088 if (codec_has_clkstop(codec) && codec_has_epss(codec) &&
3089 (state & AC_PWRST_CLK_STOP_OK))
3090 snd_hdac_codec_link_down(&codec->core);
Mengdong Lina5e7e072015-04-29 17:43:20 +08003091 snd_hdac_link_power(&codec->core, false);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003092 return 0;
3093}
3094
Takashi Iwaicc72da72015-02-19 16:00:22 +01003095static int hda_codec_runtime_resume(struct device *dev)
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003096{
Takashi Iwai55ed9cd2015-02-19 17:35:32 +01003097 struct hda_codec *codec = dev_to_hda_codec(dev);
3098
Mengdong Lina5e7e072015-04-29 17:43:20 +08003099 snd_hdac_link_power(&codec->core, true);
Takashi Iwai7639a062015-03-03 10:07:24 +01003100 snd_hdac_codec_link_up(&codec->core);
Takashi Iwai55ed9cd2015-02-19 17:35:32 +01003101 hda_call_codec_resume(codec);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003102 pm_runtime_mark_last_busy(dev);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003103 return 0;
3104}
Takashi Iwai2a439522011-07-26 09:52:50 +02003105#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003106
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003107/* referred in hda_bind.c */
3108const struct dev_pm_ops hda_codec_driver_pm = {
Takashi Iwaicc72da72015-02-19 16:00:22 +01003109 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
3110 pm_runtime_force_resume)
3111 SET_RUNTIME_PM_OPS(hda_codec_runtime_suspend, hda_codec_runtime_resume,
3112 NULL)
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003113};
Takashi Iwai54d17402005-11-21 16:33:22 +01003114
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003115/*
3116 * add standard channel maps if not specified
3117 */
3118static int add_std_chmaps(struct hda_codec *codec)
3119{
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003120 struct hda_pcm *pcm;
3121 int str, err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003122
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003123 list_for_each_entry(pcm, &codec->pcm_list_head, list) {
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003124 for (str = 0; str < 2; str++) {
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003125 struct hda_pcm_stream *hinfo = &pcm->stream[str];
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003126 struct snd_pcm_chmap *chmap;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003127 const struct snd_pcm_chmap_elem *elem;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003128
Takashi Iwaib25cf302015-08-26 14:21:14 +02003129 if (!pcm->pcm || pcm->own_chmap || !hinfo->substreams)
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003130 continue;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003131 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003132 err = snd_pcm_add_chmap_ctls(pcm->pcm, str, elem,
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003133 hinfo->channels_max,
3134 0, &chmap);
3135 if (err < 0)
3136 return err;
3137 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
3138 }
3139 }
3140 return 0;
3141}
3142
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003143/* default channel maps for 2.1 speakers;
3144 * since HD-audio supports only stereo, odd number channels are omitted
3145 */
3146const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
3147 { .channels = 2,
3148 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
3149 { .channels = 4,
3150 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
3151 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
3152 { }
3153};
3154EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
3155
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003156int snd_hda_codec_build_controls(struct hda_codec *codec)
3157{
3158 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003159 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003160 /* continue to initialize... */
3161 if (codec->patch_ops.init)
3162 err = codec->patch_ops.init(codec);
3163 if (!err && codec->patch_ops.build_controls)
3164 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003165 if (err < 0)
3166 return err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003167
3168 /* we create chmaps here instead of build_pcms */
3169 err = add_std_chmaps(codec);
3170 if (err < 0)
3171 return err;
3172
David Henningsson26a6cb62012-10-09 15:04:21 +02003173 if (codec->jackpoll_interval)
3174 hda_jackpoll_work(&codec->jackpoll_work.work);
3175 else
3176 snd_hda_jack_report_sync(codec); /* call at the last init point */
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003177 sync_power_up_states(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 return 0;
3179}
3180
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 * PCM stuff
3183 */
3184static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3185 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003186 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187{
3188 return 0;
3189}
3190
3191static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3192 struct hda_codec *codec,
3193 unsigned int stream_tag,
3194 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003195 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196{
3197 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3198 return 0;
3199}
3200
3201static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3202 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003203 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204{
Takashi Iwai888afa12008-03-18 09:57:50 +01003205 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 return 0;
3207}
3208
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003209static int set_pcm_default_values(struct hda_codec *codec,
3210 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003212 int err;
3213
Takashi Iwai0ba21762007-04-16 11:29:14 +02003214 /* query support PCM information from the given NID */
3215 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003216 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02003217 info->rates ? NULL : &info->rates,
3218 info->formats ? NULL : &info->formats,
3219 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003220 if (err < 0)
3221 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 }
3223 if (info->ops.open == NULL)
3224 info->ops.open = hda_pcm_default_open_close;
3225 if (info->ops.close == NULL)
3226 info->ops.close = hda_pcm_default_open_close;
3227 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003228 if (snd_BUG_ON(!info->nid))
3229 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 info->ops.prepare = hda_pcm_default_prepare;
3231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003233 if (snd_BUG_ON(!info->nid))
3234 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 info->ops.cleanup = hda_pcm_default_cleanup;
3236 }
3237 return 0;
3238}
3239
Takashi Iwaieb541332010-08-06 13:48:11 +02003240/*
3241 * codec prepare/cleanup entries
3242 */
Takashi Iwai95a962c2014-10-29 16:03:58 +01003243/**
3244 * snd_hda_codec_prepare - Prepare a stream
3245 * @codec: the HDA codec
3246 * @hinfo: PCM information
3247 * @stream: stream tag to assign
3248 * @format: format id to assign
3249 * @substream: PCM substream to assign
3250 *
3251 * Calls the prepare callback set by the codec with the given arguments.
3252 * Clean up the inactive streams when successful.
3253 */
Takashi Iwaieb541332010-08-06 13:48:11 +02003254int snd_hda_codec_prepare(struct hda_codec *codec,
3255 struct hda_pcm_stream *hinfo,
3256 unsigned int stream,
3257 unsigned int format,
3258 struct snd_pcm_substream *substream)
3259{
3260 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003261 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwai61ca4102015-02-27 17:57:55 +01003262 if (hinfo->ops.prepare)
3263 ret = hinfo->ops.prepare(hinfo, codec, stream, format,
3264 substream);
3265 else
3266 ret = -ENODEV;
Takashi Iwaieb541332010-08-06 13:48:11 +02003267 if (ret >= 0)
3268 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003269 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003270 return ret;
3271}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003272EXPORT_SYMBOL_GPL(snd_hda_codec_prepare);
Takashi Iwaieb541332010-08-06 13:48:11 +02003273
Takashi Iwai95a962c2014-10-29 16:03:58 +01003274/**
3275 * snd_hda_codec_cleanup - Prepare a stream
3276 * @codec: the HDA codec
3277 * @hinfo: PCM information
3278 * @substream: PCM substream
3279 *
3280 * Calls the cleanup callback set by the codec with the given arguments.
3281 */
Takashi Iwaieb541332010-08-06 13:48:11 +02003282void snd_hda_codec_cleanup(struct hda_codec *codec,
3283 struct hda_pcm_stream *hinfo,
3284 struct snd_pcm_substream *substream)
3285{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003286 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwai61ca4102015-02-27 17:57:55 +01003287 if (hinfo->ops.cleanup)
3288 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003289 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003290}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003291EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup);
Takashi Iwaieb541332010-08-06 13:48:11 +02003292
Takashi Iwaid5191e52009-11-16 14:58:17 +01003293/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01003294const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3295 "Audio", "SPDIF", "HDMI", "Modem"
3296};
3297
Takashi Iwai176d5332008-07-30 15:01:44 +02003298/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003299 * get the empty PCM device number to assign
3300 */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003301static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003302{
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003303 /* audio device indices; not linear to keep compatibility */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003304 /* assigned to static slots up to dev#10; if more needed, assign
3305 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
3306 */
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003307 static int audio_idx[HDA_PCM_NTYPES][5] = {
3308 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3309 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01003310 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003311 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003312 };
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003313 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003314
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003315 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003316 dev_err(bus->card->dev, "Invalid PCM type %d\n", type);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003317 return -EINVAL;
3318 }
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003319
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003320 for (i = 0; audio_idx[type][i] >= 0; i++) {
3321#ifndef CONFIG_SND_DYNAMIC_MINORS
3322 if (audio_idx[type][i] >= 8)
3323 break;
3324#endif
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003325 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3326 return audio_idx[type][i];
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003327 }
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003328
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003329#ifdef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai01b65bf2011-11-24 14:31:46 +01003330 /* non-fixed slots starting from 10 */
3331 for (i = 10; i < 32; i++) {
3332 if (!test_and_set_bit(i, bus->pcm_dev_bits))
3333 return i;
3334 }
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003335#endif
Takashi Iwai01b65bf2011-11-24 14:31:46 +01003336
Takashi Iwai4e76a882014-02-25 12:21:03 +01003337 dev_warn(bus->card->dev, "Too many %s devices\n",
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003338 snd_hda_pcm_type_name[type]);
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003339#ifndef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai4e76a882014-02-25 12:21:03 +01003340 dev_warn(bus->card->dev,
3341 "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003342#endif
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003343 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003344}
3345
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003346/* call build_pcms ops of the given codec and set up the default parameters */
3347int snd_hda_codec_parse_pcms(struct hda_codec *codec)
Takashi Iwai176d5332008-07-30 15:01:44 +02003348{
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003349 struct hda_pcm *cpcm;
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003350 int err;
Takashi Iwai176d5332008-07-30 15:01:44 +02003351
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003352 if (!list_empty(&codec->pcm_list_head))
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003353 return 0; /* already parsed */
3354
3355 if (!codec->patch_ops.build_pcms)
3356 return 0;
3357
3358 err = codec->patch_ops.build_pcms(codec);
3359 if (err < 0) {
3360 codec_err(codec, "cannot build PCMs for #%d (error %d)\n",
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003361 codec->core.addr, err);
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003362 return err;
3363 }
3364
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003365 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003366 int stream;
3367
3368 for (stream = 0; stream < 2; stream++) {
3369 struct hda_pcm_stream *info = &cpcm->stream[stream];
3370
3371 if (!info->substreams)
3372 continue;
Takashi Iwai176d5332008-07-30 15:01:44 +02003373 err = set_pcm_default_values(codec, info);
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003374 if (err < 0) {
3375 codec_warn(codec,
3376 "fail to setup default for PCM %s\n",
3377 cpcm->name);
Takashi Iwai176d5332008-07-30 15:01:44 +02003378 return err;
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003379 }
Takashi Iwai176d5332008-07-30 15:01:44 +02003380 }
3381 }
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003382
3383 return 0;
Takashi Iwai176d5332008-07-30 15:01:44 +02003384}
3385
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003386/* assign all PCMs of the given codec */
3387int snd_hda_codec_build_pcms(struct hda_codec *codec)
3388{
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003389 struct hda_bus *bus = codec->bus;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003390 struct hda_pcm *cpcm;
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003391 int dev, err;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003392
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003393 err = snd_hda_codec_parse_pcms(codec);
Takashi Iwaid2896192015-10-20 16:23:55 +02003394 if (err < 0)
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003395 return err;
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003396
3397 /* attach a new PCM streams */
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003398 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003399 if (cpcm->pcm)
3400 continue; /* already attached */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003401 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01003402 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003403
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003404 dev = get_empty_pcm_device(bus, cpcm->pcm_type);
3405 if (dev < 0)
3406 continue; /* no fatal error */
3407 cpcm->device = dev;
Takashi Iwai0a505752015-04-16 23:25:02 +02003408 err = snd_hda_attach_pcm_stream(bus, codec, cpcm);
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003409 if (err < 0) {
3410 codec_err(codec,
3411 "cannot attach PCM stream %d for codec #%d\n",
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003412 dev, codec->core.addr);
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003413 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003414 }
3415 }
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003416
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003417 return 0;
3418}
3419
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 * snd_hda_add_new_ctls - create controls from the array
3422 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003423 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 *
3425 * This helper function creates and add new controls in the given array.
3426 * The array must be terminated with an empty entry as terminator.
3427 *
3428 * Returns 0 if successful, or a negative error code.
3429 */
Takashi Iwai031024e2011-05-02 11:29:30 +02003430int snd_hda_add_new_ctls(struct hda_codec *codec,
3431 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01003433 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434
3435 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01003436 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01003437 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003438 if (knew->iface == -1) /* skip this codec private value */
3439 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01003440 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01003441 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003442 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01003443 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01003444 if (addr > 0)
3445 kctl->id.device = addr;
3446 if (idx > 0)
3447 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003448 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003449 if (!err)
3450 break;
3451 /* try first with another device index corresponding to
3452 * the codec addr; if it still fails (or it's the
3453 * primary codec), then try another control index
3454 */
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003455 if (!addr && codec->core.addr)
3456 addr = codec->core.addr;
Takashi Iwai1afe2062010-12-23 10:17:52 +01003457 else if (!idx && !knew->index) {
3458 idx = find_empty_mixer_ctl_idx(codec,
Takashi Iwaidcda5802012-10-12 17:24:51 +02003459 knew->name, 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003460 if (idx <= 0)
3461 return err;
3462 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01003463 return err;
3464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 }
3466 return 0;
3467}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003468EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469
Takashi Iwai83012a72012-08-24 18:38:08 +02003470#ifdef CONFIG_PM
Takashi Iwaibb573922015-02-20 09:26:04 +01003471static void codec_set_power_save(struct hda_codec *codec, int delay)
Takashi Iwaicc72da72015-02-19 16:00:22 +01003472{
3473 struct device *dev = hda_codec_dev(codec);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003474
Lu, Han2377c3c2015-06-09 16:50:38 +08003475 if (delay == 0 && codec->auto_runtime_pm)
3476 delay = 3000;
3477
Takashi Iwaicc72da72015-02-19 16:00:22 +01003478 if (delay > 0) {
3479 pm_runtime_set_autosuspend_delay(dev, delay);
3480 pm_runtime_use_autosuspend(dev);
3481 pm_runtime_allow(dev);
3482 if (!pm_runtime_suspended(dev))
3483 pm_runtime_mark_last_busy(dev);
3484 } else {
3485 pm_runtime_dont_use_autosuspend(dev);
3486 pm_runtime_forbid(dev);
3487 }
3488}
Takashi Iwaibb573922015-02-20 09:26:04 +01003489
3490/**
3491 * snd_hda_set_power_save - reprogram autosuspend for the given delay
3492 * @bus: HD-audio bus
3493 * @delay: autosuspend delay in msec, 0 = off
3494 *
3495 * Synchronize the runtime PM autosuspend state from the power_save option.
3496 */
3497void snd_hda_set_power_save(struct hda_bus *bus, int delay)
3498{
3499 struct hda_codec *c;
3500
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003501 list_for_each_codec(c, bus)
Takashi Iwaibb573922015-02-20 09:26:04 +01003502 codec_set_power_save(c, delay);
3503}
3504EXPORT_SYMBOL_GPL(snd_hda_set_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003505
Takashi Iwaid5191e52009-11-16 14:58:17 +01003506/**
3507 * snd_hda_check_amp_list_power - Check the amp list and update the power
3508 * @codec: HD-audio codec
3509 * @check: the object containing an AMP list and the status
3510 * @nid: NID to check / update
3511 *
3512 * Check whether the given NID is in the amp list. If it's in the list,
3513 * check the current AMP status, and update the the power-status according
3514 * to the mute status.
3515 *
3516 * This function is supposed to be set or called from the check_power_status
3517 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003518 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003519int snd_hda_check_amp_list_power(struct hda_codec *codec,
3520 struct hda_loopback_check *check,
3521 hda_nid_t nid)
3522{
Takashi Iwai031024e2011-05-02 11:29:30 +02003523 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003524 int ch, v;
3525
3526 if (!check->amplist)
3527 return 0;
3528 for (p = check->amplist; p->nid; p++) {
3529 if (p->nid == nid)
3530 break;
3531 }
3532 if (!p->nid)
3533 return 0; /* nothing changed */
3534
3535 for (p = check->amplist; p->nid; p++) {
3536 for (ch = 0; ch < 2; ch++) {
3537 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
3538 p->idx);
3539 if (!(v & HDA_AMP_MUTE) && v > 0) {
3540 if (!check->power_on) {
3541 check->power_on = 1;
Takashi Iwai664c7152015-04-08 11:43:14 +02003542 snd_hda_power_up_pm(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003543 }
3544 return 1;
3545 }
3546 }
3547 }
3548 if (check->power_on) {
3549 check->power_on = 0;
Takashi Iwai664c7152015-04-08 11:43:14 +02003550 snd_hda_power_down_pm(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003551 }
3552 return 0;
3553}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003554EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003555#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003557/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 * input MUX helper
3559 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01003560
3561/**
3562 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003563 * @imux: imux helper object
3564 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003565 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003566int snd_hda_input_mux_info(const struct hda_input_mux *imux,
3567 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568{
3569 unsigned int index;
3570
3571 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3572 uinfo->count = 1;
3573 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02003574 if (!imux->num_items)
3575 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576 index = uinfo->value.enumerated.item;
3577 if (index >= imux->num_items)
3578 index = imux->num_items - 1;
3579 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
3580 return 0;
3581}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003582EXPORT_SYMBOL_GPL(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583
Takashi Iwaid5191e52009-11-16 14:58:17 +01003584/**
3585 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003586 * @codec: the HDA codec
3587 * @imux: imux helper object
3588 * @ucontrol: pointer to get/store the data
3589 * @nid: input mux NID
3590 * @cur_val: pointer to get/store the current imux value
Takashi Iwaid5191e52009-11-16 14:58:17 +01003591 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003592int snd_hda_input_mux_put(struct hda_codec *codec,
3593 const struct hda_input_mux *imux,
3594 struct snd_ctl_elem_value *ucontrol,
3595 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 unsigned int *cur_val)
3597{
3598 unsigned int idx;
3599
Takashi Iwai5513b0c2007-10-09 11:58:41 +02003600 if (!imux->num_items)
3601 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 idx = ucontrol->value.enumerated.item[0];
3603 if (idx >= imux->num_items)
3604 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003605 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003607 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
3608 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 *cur_val = idx;
3610 return 1;
3611}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003612EXPORT_SYMBOL_GPL(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613
3614
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003615/**
3616 * snd_hda_enum_helper_info - Helper for simple enum ctls
3617 * @kcontrol: ctl element
3618 * @uinfo: pointer to get/store the data
3619 * @num_items: number of enum items
3620 * @texts: enum item string array
3621 *
Takashi Iwaidda415d2012-11-30 18:34:38 +01003622 * process kcontrol info callback of a simple string enum array
3623 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
3624 */
3625int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
3626 struct snd_ctl_elem_info *uinfo,
3627 int num_items, const char * const *texts)
3628{
3629 static const char * const texts_default[] = {
3630 "Disabled", "Enabled"
3631 };
3632
3633 if (!texts || !num_items) {
3634 num_items = 2;
3635 texts = texts_default;
3636 }
3637
Takashi Iwai3ff72212014-10-20 18:17:28 +02003638 return snd_ctl_enum_info(uinfo, 1, num_items, texts);
Takashi Iwaidda415d2012-11-30 18:34:38 +01003639}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003640EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info);
Takashi Iwaidda415d2012-11-30 18:34:38 +01003641
3642/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 * Multi-channel / digital-out PCM helper functions
3644 */
3645
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003646/* setup SPDIF output stream */
3647static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
3648 unsigned int stream_tag, unsigned int format)
3649{
Laurence Darby3bef1c32012-11-03 17:00:06 +00003650 struct hda_spdif_out *spdif;
3651 unsigned int curr_fmt;
3652 bool reset;
Stephen Warren7c935972011-06-01 11:14:17 -06003653
Laurence Darby3bef1c32012-11-03 17:00:06 +00003654 spdif = snd_hda_spdif_out_of_nid(codec, nid);
Libin Yang960a5812016-06-16 11:13:25 +08003655 /* Add sanity check to pass klockwork check.
3656 * This should never happen.
3657 */
3658 if (WARN_ON(spdif == NULL))
3659 return;
3660
Laurence Darby3bef1c32012-11-03 17:00:06 +00003661 curr_fmt = snd_hda_codec_read(codec, nid, 0,
3662 AC_VERB_GET_STREAM_FORMAT, 0);
3663 reset = codec->spdif_status_reset &&
3664 (spdif->ctls & AC_DIG1_ENABLE) &&
3665 curr_fmt != format;
3666
3667 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
3668 updated */
3669 if (reset)
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003670 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06003671 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02003672 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003673 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02003674 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02003675 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02003676 for (d = codec->slave_dig_outs; *d; d++)
3677 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
3678 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04003679 }
Takashi Iwai2f728532008-09-25 16:32:41 +02003680 /* turn on again (if needed) */
Laurence Darby3bef1c32012-11-03 17:00:06 +00003681 if (reset)
Takashi Iwai2f728532008-09-25 16:32:41 +02003682 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06003683 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02003684}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04003685
Takashi Iwai2f728532008-09-25 16:32:41 +02003686static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
3687{
3688 snd_hda_codec_cleanup_stream(codec, nid);
3689 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02003690 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02003691 for (d = codec->slave_dig_outs; *d; d++)
3692 snd_hda_codec_cleanup_stream(codec, *d);
3693 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003694}
3695
Takashi Iwaid5191e52009-11-16 14:58:17 +01003696/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003697 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003698 * @codec: the HDA codec
3699 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003701int snd_hda_multi_out_dig_open(struct hda_codec *codec,
3702 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703{
Ingo Molnar62932df2006-01-16 16:34:20 +01003704 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02003705 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
3706 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02003707 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01003709 mutex_unlock(&codec->spdif_mutex);
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_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713
Takashi Iwaid5191e52009-11-16 14:58:17 +01003714/**
3715 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003716 * @codec: the HDA codec
3717 * @mout: hda_multi_out object
3718 * @stream_tag: stream tag to assign
3719 * @format: format id to assign
3720 * @substream: PCM substream to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01003721 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003722int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
3723 struct hda_multi_out *mout,
3724 unsigned int stream_tag,
3725 unsigned int format,
3726 struct snd_pcm_substream *substream)
3727{
3728 mutex_lock(&codec->spdif_mutex);
3729 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
3730 mutex_unlock(&codec->spdif_mutex);
3731 return 0;
3732}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003733EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003734
Takashi Iwaid5191e52009-11-16 14:58:17 +01003735/**
3736 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003737 * @codec: the HDA codec
3738 * @mout: hda_multi_out object
Takashi Iwaid5191e52009-11-16 14:58:17 +01003739 */
Takashi Iwai9411e212009-02-13 11:32:28 +01003740int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
3741 struct hda_multi_out *mout)
3742{
3743 mutex_lock(&codec->spdif_mutex);
3744 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3745 mutex_unlock(&codec->spdif_mutex);
3746 return 0;
3747}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003748EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup);
Takashi Iwai9411e212009-02-13 11:32:28 +01003749
Takashi Iwaid5191e52009-11-16 14:58:17 +01003750/**
3751 * snd_hda_multi_out_dig_close - release the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003752 * @codec: the HDA codec
3753 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003755int snd_hda_multi_out_dig_close(struct hda_codec *codec,
3756 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757{
Ingo Molnar62932df2006-01-16 16:34:20 +01003758 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01003760 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 return 0;
3762}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003763EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764
Takashi Iwaid5191e52009-11-16 14:58:17 +01003765/**
3766 * snd_hda_multi_out_analog_open - open analog outputs
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003767 * @codec: the HDA codec
3768 * @mout: hda_multi_out object
3769 * @substream: PCM substream to assign
3770 * @hinfo: PCM information to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01003771 *
3772 * Open analog outputs and set up the hw-constraints.
3773 * If the digital outputs can be opened as slave, open the digital
3774 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003776int snd_hda_multi_out_analog_open(struct hda_codec *codec,
3777 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01003778 struct snd_pcm_substream *substream,
3779 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780{
Takashi Iwai9a081602008-02-12 18:37:26 +01003781 struct snd_pcm_runtime *runtime = substream->runtime;
3782 runtime->hw.channels_max = mout->max_channels;
3783 if (mout->dig_out_nid) {
3784 if (!mout->analog_rates) {
3785 mout->analog_rates = hinfo->rates;
3786 mout->analog_formats = hinfo->formats;
3787 mout->analog_maxbps = hinfo->maxbps;
3788 } else {
3789 runtime->hw.rates = mout->analog_rates;
3790 runtime->hw.formats = mout->analog_formats;
3791 hinfo->maxbps = mout->analog_maxbps;
3792 }
3793 if (!mout->spdif_rates) {
3794 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
3795 &mout->spdif_rates,
3796 &mout->spdif_formats,
3797 &mout->spdif_maxbps);
3798 }
3799 mutex_lock(&codec->spdif_mutex);
3800 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02003801 if ((runtime->hw.rates & mout->spdif_rates) &&
3802 (runtime->hw.formats & mout->spdif_formats)) {
3803 runtime->hw.rates &= mout->spdif_rates;
3804 runtime->hw.formats &= mout->spdif_formats;
3805 if (mout->spdif_maxbps < hinfo->maxbps)
3806 hinfo->maxbps = mout->spdif_maxbps;
3807 } else {
3808 mout->share_spdif = 0;
3809 /* FIXME: need notify? */
3810 }
Takashi Iwai9a081602008-02-12 18:37:26 +01003811 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02003812 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01003813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 return snd_pcm_hw_constraint_step(substream->runtime, 0,
3815 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
3816}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003817EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818
Takashi Iwaid5191e52009-11-16 14:58:17 +01003819/**
3820 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003821 * @codec: the HDA codec
3822 * @mout: hda_multi_out object
3823 * @stream_tag: stream tag to assign
3824 * @format: format id to assign
3825 * @substream: PCM substream to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01003826 *
3827 * Set up the i/o for analog out.
3828 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003830int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
3831 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 unsigned int stream_tag,
3833 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003834 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835{
Takashi Iwaidda14412011-05-02 11:29:30 +02003836 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003838 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 int i;
3840
Ingo Molnar62932df2006-01-16 16:34:20 +01003841 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003842 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01003843 if (mout->dig_out_nid && mout->share_spdif &&
3844 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Libin Yang960a5812016-06-16 11:13:25 +08003845 if (chs == 2 && spdif != NULL &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02003846 snd_hda_is_supported_format(codec, mout->dig_out_nid,
3847 format) &&
Stephen Warren7c935972011-06-01 11:14:17 -06003848 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003850 setup_dig_out_stream(codec, mout->dig_out_nid,
3851 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 } else {
3853 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02003854 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 }
3856 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003857 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858
3859 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003860 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
3861 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02003862 if (!mout->no_share_stream &&
3863 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003865 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
3866 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01003867 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02003868 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
3869 if (!mout->no_share_stream && mout->hp_out_nid[i])
3870 snd_hda_codec_setup_stream(codec,
3871 mout->hp_out_nid[i],
3872 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01003873
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874 /* surrounds */
3875 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02003876 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003877 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3878 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02003879 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003880 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3881 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882 }
David Henningssoncd4035e2013-10-10 09:01:25 +02003883
3884 /* extra surrounds */
3885 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
3886 int ch = 0;
3887 if (!mout->extra_out_nid[i])
3888 break;
3889 if (chs >= (i + 1) * 2)
3890 ch = i * 2;
3891 else if (!mout->no_share_stream)
3892 break;
3893 snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
3894 stream_tag, ch, format);
3895 }
3896
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897 return 0;
3898}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003899EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900
Takashi Iwaid5191e52009-11-16 14:58:17 +01003901/**
3902 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003903 * @codec: the HDA codec
3904 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003906int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
3907 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908{
Takashi Iwaidda14412011-05-02 11:29:30 +02003909 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 int i;
3911
3912 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01003913 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01003915 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02003916 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
3917 if (mout->hp_out_nid[i])
3918 snd_hda_codec_cleanup_stream(codec,
3919 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01003920 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
3921 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01003922 snd_hda_codec_cleanup_stream(codec,
3923 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01003924 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02003926 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 mout->dig_out_used = 0;
3928 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003929 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 return 0;
3931}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003932EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933
Takashi Iwai47408602012-04-20 13:06:53 +02003934/**
3935 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003936 * @codec: the HDA codec
3937 * @pin: referred pin NID
Takashi Iwai47408602012-04-20 13:06:53 +02003938 *
3939 * Guess the suitable VREF pin bits to be set as the pin-control value.
3940 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
3941 */
3942unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
3943{
3944 unsigned int pincap;
3945 unsigned int oldval;
3946 oldval = snd_hda_codec_read(codec, pin, 0,
3947 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3948 pincap = snd_hda_query_pin_caps(codec, pin);
3949 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
3950 /* Exception: if the default pin setup is vref50, we give it priority */
3951 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
3952 return AC_PINCTL_VREF_80;
3953 else if (pincap & AC_PINCAP_VREF_50)
3954 return AC_PINCTL_VREF_50;
3955 else if (pincap & AC_PINCAP_VREF_100)
3956 return AC_PINCTL_VREF_100;
3957 else if (pincap & AC_PINCAP_VREF_GRD)
3958 return AC_PINCTL_VREF_GRD;
3959 return AC_PINCTL_VREF_HIZ;
3960}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003961EXPORT_SYMBOL_GPL(snd_hda_get_default_vref);
Takashi Iwai47408602012-04-20 13:06:53 +02003962
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003963/**
3964 * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap
3965 * @codec: the HDA codec
3966 * @pin: referred pin NID
3967 * @val: pin ctl value to audit
3968 */
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01003969unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
3970 hda_nid_t pin, unsigned int val)
3971{
3972 static unsigned int cap_lists[][2] = {
3973 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
3974 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
3975 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
3976 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
3977 };
3978 unsigned int cap;
3979
3980 if (!val)
3981 return 0;
3982 cap = snd_hda_query_pin_caps(codec, pin);
3983 if (!cap)
3984 return val; /* don't know what to do... */
3985
3986 if (val & AC_PINCTL_OUT_EN) {
3987 if (!(cap & AC_PINCAP_OUT))
3988 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
3989 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
3990 val &= ~AC_PINCTL_HP_EN;
3991 }
3992
3993 if (val & AC_PINCTL_IN_EN) {
3994 if (!(cap & AC_PINCAP_IN))
3995 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
3996 else {
3997 unsigned int vcap, vref;
3998 int i;
3999 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
4000 vref = val & AC_PINCTL_VREFEN;
4001 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
4002 if (vref == cap_lists[i][0] &&
4003 !(vcap & cap_lists[i][1])) {
4004 if (i == ARRAY_SIZE(cap_lists) - 1)
4005 vref = AC_PINCTL_VREF_HIZ;
4006 else
4007 vref = cap_lists[i + 1][0];
4008 }
4009 }
4010 val &= ~AC_PINCTL_VREFEN;
4011 val |= vref;
4012 }
4013 }
4014
4015 return val;
4016}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004017EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl);
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01004018
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004019/**
4020 * _snd_hda_pin_ctl - Helper to set pin ctl value
4021 * @codec: the HDA codec
4022 * @pin: referred pin NID
4023 * @val: pin control value to set
4024 * @cached: access over codec pinctl cache or direct write
4025 *
4026 * This function is a helper to set a pin ctl value more safely.
4027 * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the
4028 * value in pin target array via snd_hda_codec_set_pin_target(), then
4029 * actually writes the value via either snd_hda_codec_update_cache() or
4030 * snd_hda_codec_write() depending on @cached flag.
4031 */
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02004032int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
4033 unsigned int val, bool cached)
4034{
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01004035 val = snd_hda_correct_pin_ctl(codec, pin, val);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01004036 snd_hda_codec_set_pin_target(codec, pin, val);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02004037 if (cached)
4038 return snd_hda_codec_update_cache(codec, pin, 0,
4039 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
4040 else
4041 return snd_hda_codec_write(codec, pin, 0,
4042 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
4043}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004044EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02004045
Takashi Iwai990061c2010-09-09 22:08:44 +02004046/**
4047 * snd_hda_add_imux_item - Add an item to input_mux
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004048 * @codec: the HDA codec
4049 * @imux: imux helper object
4050 * @label: the name of imux item to assign
4051 * @index: index number of imux item to assign
4052 * @type_idx: pointer to store the resultant label index
Takashi Iwai990061c2010-09-09 22:08:44 +02004053 *
4054 * When the same label is used already in the existing items, the number
4055 * suffix is appended to the label. This label index number is stored
4056 * to type_idx when non-NULL pointer is given.
4057 */
Takashi Iwai6194b992014-06-06 18:12:16 +02004058int snd_hda_add_imux_item(struct hda_codec *codec,
4059 struct hda_input_mux *imux, const char *label,
Takashi Iwai10a20af2010-09-09 16:28:02 +02004060 int index, int *type_idx)
4061{
4062 int i, label_idx = 0;
4063 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
Takashi Iwai6194b992014-06-06 18:12:16 +02004064 codec_err(codec, "hda_codec: Too many imux items!\n");
Takashi Iwai10a20af2010-09-09 16:28:02 +02004065 return -EINVAL;
4066 }
4067 for (i = 0; i < imux->num_items; i++) {
4068 if (!strncmp(label, imux->items[i].label, strlen(label)))
4069 label_idx++;
4070 }
4071 if (type_idx)
4072 *type_idx = label_idx;
4073 if (label_idx > 0)
4074 snprintf(imux->items[imux->num_items].label,
4075 sizeof(imux->items[imux->num_items].label),
4076 "%s %d", label, label_idx);
4077 else
4078 strlcpy(imux->items[imux->num_items].label, label,
4079 sizeof(imux->items[imux->num_items].label));
4080 imux->items[imux->num_items].index = index;
4081 imux->num_items++;
4082 return 0;
4083}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004084EXPORT_SYMBOL_GPL(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02004085
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086/**
Takashi Iwai0a505752015-04-16 23:25:02 +02004087 * snd_hda_bus_reset_codecs - Reset the bus
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004088 * @bus: HD-audio bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089 */
Takashi Iwai0a505752015-04-16 23:25:02 +02004090void snd_hda_bus_reset_codecs(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004092 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093
Takashi Iwaid068ebc2015-03-02 23:22:59 +01004094 list_for_each_codec(codec, bus) {
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004095 /* FIXME: maybe a better way needed for forced reset */
David Henningsson26a6cb62012-10-09 15:04:21 +02004096 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004097#ifdef CONFIG_PM
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05004098 if (hda_codec_is_power_on(codec)) {
Takashi Iwaicc72da72015-02-19 16:00:22 +01004099 hda_call_codec_suspend(codec);
Mengdong Lin12edb892013-11-26 23:32:23 -05004100 hda_call_codec_resume(codec);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004101 }
4102#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104}
Takashi Iwaib2e18592008-07-30 15:01:44 +02004105
Takashi Iwaid5191e52009-11-16 14:58:17 +01004106/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004107 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
4108 * @pcm: PCM caps bits
4109 * @buf: the string buffer to write
4110 * @buflen: the max buffer length
4111 *
4112 * used by hda_proc.c and hda_eld.c
4113 */
Takashi Iwaib2022262008-11-21 21:24:03 +01004114void snd_print_pcm_bits(int pcm, char *buf, int buflen)
4115{
4116 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
4117 int i, j;
4118
4119 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
4120 if (pcm & (AC_SUPPCM_BITS_8 << i))
4121 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
4122
4123 buf[j] = '\0'; /* necessary when j == 0 */
4124}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004125EXPORT_SYMBOL_GPL(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004126
4127MODULE_DESCRIPTION("HDA codec core");
4128MODULE_LICENSE("GPL");