blob: 37f43a1b34ef1f5f48682230055c0f01d9a07449 [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/**
94 * snd_hda_codec_read - send a command and get the response
95 * @codec: the HDA codec
96 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +020097 * @flags: optional bit flags
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 * @verb: the verb to send
99 * @parm: the parameter for the verb
100 *
101 * Send a single command and read the corresponding response.
102 *
103 * Returns the obtained response value, or -1 for an error.
104 */
Takashi Iwai0ba21762007-04-16 11:29:14 +0200105unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200106 int flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 unsigned int verb, unsigned int parm)
108{
Takashi Iwai7639a062015-03-03 10:07:24 +0100109 unsigned int cmd = snd_hdac_make_cmd(&codec->core, nid, verb, parm);
Takashi Iwaiaa2936f2009-05-26 16:07:57 +0200110 unsigned int res;
Takashi Iwai058524482015-03-03 15:40:08 +0100111 if (snd_hdac_exec_verb(&codec->core, cmd, flags, &res))
Greg Thelen9857edf2011-06-13 07:45:45 -0700112 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 return res;
114}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100115EXPORT_SYMBOL_GPL(snd_hda_codec_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117/**
118 * snd_hda_codec_write - send a single command without waiting for response
119 * @codec: the HDA codec
120 * @nid: NID to send the command
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200121 * @flags: optional bit flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 * @verb: the verb to send
123 * @parm: the parameter for the verb
124 *
125 * Send a single command without waiting for response.
126 *
127 * Returns 0 if successful, or a negative error code.
128 */
Takashi Iwaie7ecc272013-06-06 14:00:23 +0200129int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int flags,
130 unsigned int verb, unsigned int parm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
Takashi Iwai7639a062015-03-03 10:07:24 +0100132 unsigned int cmd = snd_hdac_make_cmd(&codec->core, nid, verb, parm);
Takashi Iwai058524482015-03-03 15:40:08 +0100133 return snd_hdac_exec_verb(&codec->core, cmd, flags, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100135EXPORT_SYMBOL_GPL(snd_hda_codec_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137/**
138 * snd_hda_sequence_write - sequence writes
139 * @codec: the HDA codec
140 * @seq: VERB array to send
141 *
142 * Send the commands sequentially from the given array.
143 * The array must be terminated with NID=0.
144 */
145void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
146{
147 for (; seq->nid; seq++)
148 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
149}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100150EXPORT_SYMBOL_GPL(snd_hda_sequence_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100152/* connection list element */
153struct hda_conn_list {
154 struct list_head list;
155 int len;
156 hda_nid_t nid;
157 hda_nid_t conns[0];
158};
159
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200160/* look up the cached results */
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100161static struct hda_conn_list *
162lookup_conn_list(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200163{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100164 struct hda_conn_list *p;
165 list_for_each_entry(p, &codec->conn_list, list) {
166 if (p->nid == nid)
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200167 return p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200168 }
169 return NULL;
170}
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200171
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100172static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
173 const hda_nid_t *list)
174{
175 struct hda_conn_list *p;
176
177 p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL);
178 if (!p)
179 return -ENOMEM;
180 p->len = len;
181 p->nid = nid;
182 memcpy(p->conns, list, len * sizeof(hda_nid_t));
183 list_add(&p->list, &codec->conn_list);
184 return 0;
185}
186
187static void remove_conn_list(struct hda_codec *codec)
188{
189 while (!list_empty(&codec->conn_list)) {
190 struct hda_conn_list *p;
191 p = list_first_entry(&codec->conn_list, typeof(*p), list);
192 list_del(&p->list);
193 kfree(p);
194 }
195}
196
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200197/* read the connection and add to the cache */
198static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200199{
Takashi Iwai4eea3092013-02-07 18:18:19 +0100200 hda_nid_t list[32];
201 hda_nid_t *result = list;
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200202 int len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200203
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200204 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
Takashi Iwai4eea3092013-02-07 18:18:19 +0100205 if (len == -ENOSPC) {
206 len = snd_hda_get_num_raw_conns(codec, nid);
207 result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL);
208 if (!result)
209 return -ENOMEM;
210 len = snd_hda_get_raw_connections(codec, nid, result, len);
211 }
212 if (len >= 0)
213 len = snd_hda_override_conn_list(codec, nid, len, result);
214 if (result != list)
215 kfree(result);
216 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200217}
Takashi Iwaidce20792011-06-24 14:10:28 +0200218
219/**
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100220 * snd_hda_get_conn_list - get connection list
221 * @codec: the HDA codec
222 * @nid: NID to parse
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100223 * @listp: the pointer to store NID list
224 *
225 * Parses the connection list of the given widget and stores the pointer
226 * to the list of NIDs.
227 *
228 * Returns the number of connections, or a negative error code.
229 *
230 * Note that the returned pointer isn't protected against the list
231 * modification. If snd_hda_override_conn_list() might be called
232 * concurrently, protect with a mutex appropriately.
233 */
234int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
235 const hda_nid_t **listp)
236{
237 bool added = false;
238
239 for (;;) {
240 int err;
241 const struct hda_conn_list *p;
242
243 /* if the connection-list is already cached, read it */
244 p = lookup_conn_list(codec, nid);
245 if (p) {
246 if (listp)
247 *listp = p->conns;
248 return p->len;
249 }
250 if (snd_BUG_ON(added))
251 return -EINVAL;
252
253 err = read_and_add_raw_conns(codec, nid);
254 if (err < 0)
255 return err;
256 added = true;
257 }
258}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100259EXPORT_SYMBOL_GPL(snd_hda_get_conn_list);
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100260
261/**
Takashi Iwaidce20792011-06-24 14:10:28 +0200262 * snd_hda_get_connections - copy connection list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 * @codec: the HDA codec
264 * @nid: NID to parse
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200265 * @conn_list: connection list array; when NULL, checks only the size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 * @max_conns: max. number of connections to store
267 *
268 * Parses the connection list of the given widget and stores the list
269 * of NIDs.
270 *
271 * Returns the number of connections, or a negative error code.
272 */
273int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200274 hda_nid_t *conn_list, int max_conns)
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200275{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100276 const hda_nid_t *list;
277 int len = snd_hda_get_conn_list(codec, nid, &list);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200278
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100279 if (len > 0 && conn_list) {
280 if (len > max_conns) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100281 codec_err(codec, "Too many connections %d for NID 0x%x\n",
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200282 len, nid);
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200283 return -EINVAL;
284 }
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100285 memcpy(conn_list, list, len * sizeof(hda_nid_t));
Takashi Iwaidce20792011-06-24 14:10:28 +0200286 }
Takashi Iwai09cf03b2012-05-19 17:21:25 +0200287
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100288 return len;
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200289}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100290EXPORT_SYMBOL_GPL(snd_hda_get_connections);
Takashi Iwaia12d3e12011-04-07 15:55:15 +0200291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292/**
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200293 * snd_hda_override_conn_list - add/modify the connection-list to cache
294 * @codec: the HDA codec
295 * @nid: NID to parse
296 * @len: number of connection list entries
297 * @list: the list of connection entries
298 *
299 * Add or modify the given connection-list to the cache. If the corresponding
300 * cache already exists, invalidate it and append a new one.
301 *
302 * Returns zero or a negative error code.
303 */
304int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
305 const hda_nid_t *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100307 struct hda_conn_list *p;
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200308
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100309 p = lookup_conn_list(codec, nid);
310 if (p) {
311 list_del(&p->list);
312 kfree(p);
313 }
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200314
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100315 return add_conn_list(codec, nid, len, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100317EXPORT_SYMBOL_GPL(snd_hda_override_conn_list);
Takashi Iwaib2f934a2011-07-04 16:23:26 +0200318
319/**
Takashi Iwai8d087c72011-06-28 12:45:47 +0200320 * snd_hda_get_conn_index - get the connection index of the given NID
321 * @codec: the HDA codec
322 * @mux: NID containing the list
323 * @nid: NID to select
324 * @recursive: 1 when searching NID recursively, otherwise 0
325 *
326 * Parses the connection list of the widget @mux and checks whether the
327 * widget @nid is present. If it is, return the connection index.
328 * Otherwise it returns -1.
329 */
330int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
331 hda_nid_t nid, int recursive)
332{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100333 const hda_nid_t *conn;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200334 int i, nums;
335
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100336 nums = snd_hda_get_conn_list(codec, mux, &conn);
Takashi Iwai8d087c72011-06-28 12:45:47 +0200337 for (i = 0; i < nums; i++)
338 if (conn[i] == nid)
339 return i;
340 if (!recursive)
341 return -1;
Takashi Iwaid94ddd82012-12-20 14:42:42 +0100342 if (recursive > 10) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100343 codec_dbg(codec, "too deep connection for 0x%x\n", nid);
Takashi Iwai8d087c72011-06-28 12:45:47 +0200344 return -1;
345 }
346 recursive++;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200347 for (i = 0; i < nums; i++) {
348 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
349 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
350 continue;
Takashi Iwai8d087c72011-06-28 12:45:47 +0200351 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
352 return i;
Takashi Iwai99e14c92011-09-13 10:33:16 +0200353 }
Takashi Iwai8d087c72011-06-28 12:45:47 +0200354 return -1;
355}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100356EXPORT_SYMBOL_GPL(snd_hda_get_conn_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Mengdong Linf1aa0682013-08-26 21:35:21 -0400358
359/* return DEVLIST_LEN parameter of the given widget */
360static unsigned int get_num_devices(struct hda_codec *codec, hda_nid_t nid)
361{
362 unsigned int wcaps = get_wcaps(codec, nid);
363 unsigned int parm;
364
365 if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) ||
366 get_wcaps_type(wcaps) != AC_WID_PIN)
367 return 0;
368
Dave Airlie132bd962015-06-09 13:39:31 +1000369 parm = snd_hdac_read_parm_uncached(&codec->core, nid, AC_PAR_DEVLIST_LEN);
Takashi Iwai86548442015-06-09 07:22:26 +0200370 if (parm == -1)
Mengdong Linf1aa0682013-08-26 21:35:21 -0400371 parm = 0;
372 return parm & AC_DEV_LIST_LEN_MASK;
373}
374
375/**
376 * snd_hda_get_devices - copy device list without cache
377 * @codec: the HDA codec
378 * @nid: NID of the pin to parse
379 * @dev_list: device list array
380 * @max_devices: max. number of devices to store
381 *
382 * Copy the device list. This info is dynamic and so not cached.
383 * Currently called only from hda_proc.c, so not exported.
384 */
385int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid,
386 u8 *dev_list, int max_devices)
387{
388 unsigned int parm;
389 int i, dev_len, devices;
390
391 parm = get_num_devices(codec, nid);
392 if (!parm) /* not multi-stream capable */
393 return 0;
394
395 dev_len = parm + 1;
396 dev_len = dev_len < max_devices ? dev_len : max_devices;
397
398 devices = 0;
399 while (devices < dev_len) {
Takashi Iwaicad372f2015-03-25 17:57:00 +0100400 if (snd_hdac_read(&codec->core, nid,
401 AC_VERB_GET_DEVICE_LIST, devices, &parm))
402 break; /* error */
Mengdong Linf1aa0682013-08-26 21:35:21 -0400403
404 for (i = 0; i < 8; i++) {
405 dev_list[devices] = (u8)parm;
406 parm >>= 4;
407 devices++;
408 if (devices >= dev_len)
409 break;
410 }
411 }
412 return devices;
413}
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415/*
Takashi Iwai54d17402005-11-21 16:33:22 +0100416 * read widget caps for each widget and store in cache
417 */
418static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
419{
420 int i;
421 hda_nid_t nid;
422
Takashi Iwai7639a062015-03-03 10:07:24 +0100423 codec->wcaps = kmalloc(codec->core.num_nodes * 4, GFP_KERNEL);
Takashi Iwai0ba21762007-04-16 11:29:14 +0200424 if (!codec->wcaps)
Takashi Iwai54d17402005-11-21 16:33:22 +0100425 return -ENOMEM;
Takashi Iwai7639a062015-03-03 10:07:24 +0100426 nid = codec->core.start_nid;
427 for (i = 0; i < codec->core.num_nodes; i++, nid++)
Takashi Iwai9ba17b42015-03-03 23:29:47 +0100428 codec->wcaps[i] = snd_hdac_read_parm_uncached(&codec->core,
429 nid, AC_PAR_AUDIO_WIDGET_CAP);
Takashi Iwai54d17402005-11-21 16:33:22 +0100430 return 0;
431}
432
Takashi Iwai3be14142009-02-20 14:11:16 +0100433/* read all pin default configurations and save codec->init_pins */
434static int read_pin_defaults(struct hda_codec *codec)
435{
Takashi Iwai7639a062015-03-03 10:07:24 +0100436 hda_nid_t nid;
Takashi Iwai3be14142009-02-20 14:11:16 +0100437
Takashi Iwai7639a062015-03-03 10:07:24 +0100438 for_each_hda_codec_node(nid, codec) {
Takashi Iwai3be14142009-02-20 14:11:16 +0100439 struct hda_pincfg *pin;
440 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwaia22d5432009-07-27 12:54:26 +0200441 unsigned int wid_type = get_wcaps_type(wcaps);
Takashi Iwai3be14142009-02-20 14:11:16 +0100442 if (wid_type != AC_WID_PIN)
443 continue;
444 pin = snd_array_new(&codec->init_pins);
445 if (!pin)
446 return -ENOMEM;
447 pin->nid = nid;
448 pin->cfg = snd_hda_codec_read(codec, nid, 0,
449 AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200450 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
451 AC_VERB_GET_PIN_WIDGET_CONTROL,
452 0);
Takashi Iwai3be14142009-02-20 14:11:16 +0100453 }
454 return 0;
455}
456
457/* look up the given pin config list and return the item matching with NID */
458static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
459 struct snd_array *array,
460 hda_nid_t nid)
461{
462 int i;
463 for (i = 0; i < array->used; i++) {
464 struct hda_pincfg *pin = snd_array_elem(array, i);
465 if (pin->nid == nid)
466 return pin;
467 }
468 return NULL;
469}
470
Takashi Iwai3be14142009-02-20 14:11:16 +0100471/* set the current pin config value for the given NID.
472 * the value is cached, and read via snd_hda_codec_get_pincfg()
473 */
474int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
475 hda_nid_t nid, unsigned int cfg)
476{
477 struct hda_pincfg *pin;
478
Takashi Iwaid5657ec2013-04-18 09:59:28 +0200479 /* the check below may be invalid when pins are added by a fixup
480 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
481 * for now
482 */
483 /*
Takashi Iwaib82855a2009-12-27 11:24:56 +0100484 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
485 return -EINVAL;
Takashi Iwaid5657ec2013-04-18 09:59:28 +0200486 */
Takashi Iwaib82855a2009-12-27 11:24:56 +0100487
Takashi Iwai3be14142009-02-20 14:11:16 +0100488 pin = look_up_pincfg(codec, list, nid);
489 if (!pin) {
490 pin = snd_array_new(list);
491 if (!pin)
492 return -ENOMEM;
493 pin->nid = nid;
494 }
495 pin->cfg = cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100496 return 0;
497}
498
Takashi Iwaid5191e52009-11-16 14:58:17 +0100499/**
500 * snd_hda_codec_set_pincfg - Override a pin default configuration
501 * @codec: the HDA codec
502 * @nid: NID to set the pin config
503 * @cfg: the pin default config value
504 *
505 * Override a pin default configuration value in the cache.
506 * This value can be read by snd_hda_codec_get_pincfg() in a higher
507 * priority than the real hardware value.
508 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100509int snd_hda_codec_set_pincfg(struct hda_codec *codec,
510 hda_nid_t nid, unsigned int cfg)
511{
Takashi Iwai346ff702009-02-23 09:42:57 +0100512 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100513}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100514EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100515
Takashi Iwaid5191e52009-11-16 14:58:17 +0100516/**
517 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
518 * @codec: the HDA codec
519 * @nid: NID to get the pin config
520 *
521 * Get the current pin config value of the given pin NID.
522 * If the pincfg value is cached or overridden via sysfs or driver,
523 * returns the cached value.
524 */
Takashi Iwai3be14142009-02-20 14:11:16 +0100525unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
526{
527 struct hda_pincfg *pin;
528
Takashi Iwai648a8d22014-02-25 10:38:13 +0100529#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai09b70e82013-01-10 18:21:56 +0100530 {
531 unsigned int cfg = 0;
532 mutex_lock(&codec->user_mutex);
533 pin = look_up_pincfg(codec, &codec->user_pins, nid);
534 if (pin)
535 cfg = pin->cfg;
536 mutex_unlock(&codec->user_mutex);
537 if (cfg)
538 return cfg;
539 }
Takashi Iwai3be14142009-02-20 14:11:16 +0100540#endif
Takashi Iwai5e7b8e02009-02-23 09:45:59 +0100541 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
542 if (pin)
543 return pin->cfg;
Takashi Iwai3be14142009-02-20 14:11:16 +0100544 pin = look_up_pincfg(codec, &codec->init_pins, nid);
545 if (pin)
546 return pin->cfg;
547 return 0;
548}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100549EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg);
Takashi Iwai3be14142009-02-20 14:11:16 +0100550
Takashi Iwai95a962c2014-10-29 16:03:58 +0100551/**
552 * snd_hda_codec_set_pin_target - remember the current pinctl target value
553 * @codec: the HDA codec
554 * @nid: pin NID
555 * @val: assigned pinctl value
556 *
557 * This function stores the given value to a pinctl target value in the
558 * pincfg table. This isn't always as same as the actually written value
559 * but can be referred at any time via snd_hda_codec_get_pin_target().
560 */
Takashi Iwaid7fdc002013-01-10 08:38:04 +0100561int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
562 unsigned int val)
563{
564 struct hda_pincfg *pin;
565
566 pin = look_up_pincfg(codec, &codec->init_pins, nid);
567 if (!pin)
568 return -EINVAL;
569 pin->target = val;
570 return 0;
571}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100572EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target);
Takashi Iwaid7fdc002013-01-10 08:38:04 +0100573
Takashi Iwai95a962c2014-10-29 16:03:58 +0100574/**
575 * snd_hda_codec_get_pin_target - return the current pinctl target value
576 * @codec: the HDA codec
577 * @nid: pin NID
578 */
Takashi Iwaid7fdc002013-01-10 08:38:04 +0100579int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
580{
581 struct hda_pincfg *pin;
582
583 pin = look_up_pincfg(codec, &codec->init_pins, nid);
584 if (!pin)
585 return 0;
586 return pin->target;
587}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100588EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target);
Takashi Iwaid7fdc002013-01-10 08:38:04 +0100589
Takashi Iwai92ee6162009-12-27 11:18:59 +0100590/**
591 * snd_hda_shutup_pins - Shut up all pins
592 * @codec: the HDA codec
593 *
594 * Clear all pin controls to shup up before suspend for avoiding click noise.
595 * The controls aren't cached so that they can be resumed properly.
596 */
597void snd_hda_shutup_pins(struct hda_codec *codec)
598{
599 int i;
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200600 /* don't shut up pins when unloading the driver; otherwise it breaks
601 * the default pin setup at the next load of the driver
602 */
603 if (codec->bus->shutdown)
604 return;
Takashi Iwai92ee6162009-12-27 11:18:59 +0100605 for (i = 0; i < codec->init_pins.used; i++) {
606 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
607 /* use read here for syncing after issuing each verb */
608 snd_hda_codec_read(codec, pin->nid, 0,
609 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
610 }
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200611 codec->pins_shutup = 1;
Takashi Iwai92ee6162009-12-27 11:18:59 +0100612}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100613EXPORT_SYMBOL_GPL(snd_hda_shutup_pins);
Takashi Iwai92ee6162009-12-27 11:18:59 +0100614
Takashi Iwai2a439522011-07-26 09:52:50 +0200615#ifdef CONFIG_PM
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200616/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
617static void restore_shutup_pins(struct hda_codec *codec)
618{
619 int i;
620 if (!codec->pins_shutup)
621 return;
622 if (codec->bus->shutdown)
623 return;
624 for (i = 0; i < codec->init_pins.used; i++) {
625 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
626 snd_hda_codec_write(codec, pin->nid, 0,
627 AC_VERB_SET_PIN_WIDGET_CONTROL,
628 pin->ctrl);
629 }
630 codec->pins_shutup = 0;
631}
Mike Waychison1c7276c2011-04-20 12:04:36 -0700632#endif
Takashi Iwaiac0547d2010-07-05 16:50:13 +0200633
David Henningsson26a6cb62012-10-09 15:04:21 +0200634static void hda_jackpoll_work(struct work_struct *work)
635{
636 struct hda_codec *codec =
637 container_of(work, struct hda_codec, jackpoll_work.work);
David Henningsson26a6cb62012-10-09 15:04:21 +0200638
639 snd_hda_jack_set_dirty_all(codec);
640 snd_hda_jack_poll_all(codec);
Wang Xingchao18e60622013-07-25 23:34:45 -0400641
642 if (!codec->jackpoll_interval)
643 return;
644
Takashi Iwai2f35c632015-02-27 22:43:26 +0100645 schedule_delayed_work(&codec->jackpoll_work,
646 codec->jackpoll_interval);
David Henningsson26a6cb62012-10-09 15:04:21 +0200647}
648
Takashi Iwai3fdf1462012-11-22 08:16:31 +0100649/* release all pincfg lists */
650static void free_init_pincfgs(struct hda_codec *codec)
Takashi Iwai3be14142009-02-20 14:11:16 +0100651{
Takashi Iwai346ff702009-02-23 09:42:57 +0100652 snd_array_free(&codec->driver_pins);
Takashi Iwai648a8d22014-02-25 10:38:13 +0100653#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai346ff702009-02-23 09:42:57 +0100654 snd_array_free(&codec->user_pins);
Takashi Iwai3be14142009-02-20 14:11:16 +0100655#endif
Takashi Iwai3be14142009-02-20 14:11:16 +0100656 snd_array_free(&codec->init_pins);
657}
658
Takashi Iwai54d17402005-11-21 16:33:22 +0100659/*
Takashi Iwaieb541332010-08-06 13:48:11 +0200660 * audio-converter setup caches
661 */
662struct hda_cvt_setup {
663 hda_nid_t nid;
664 u8 stream_tag;
665 u8 channel_id;
666 u16 format_id;
667 unsigned char active; /* cvt is currently used */
668 unsigned char dirty; /* setups should be cleared */
669};
670
671/* get or create a cache entry for the given audio converter NID */
672static struct hda_cvt_setup *
673get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
674{
675 struct hda_cvt_setup *p;
676 int i;
677
678 for (i = 0; i < codec->cvt_setups.used; i++) {
679 p = snd_array_elem(&codec->cvt_setups, i);
680 if (p->nid == nid)
681 return p;
682 }
683 p = snd_array_new(&codec->cvt_setups);
684 if (p)
685 p->nid = nid;
686 return p;
687}
688
689/*
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100690 * PCM device
691 */
692static void release_pcm(struct kref *kref)
693{
694 struct hda_pcm *pcm = container_of(kref, struct hda_pcm, kref);
695
696 if (pcm->pcm)
697 snd_device_free(pcm->codec->card, pcm->pcm);
698 clear_bit(pcm->device, pcm->codec->bus->pcm_dev_bits);
699 kfree(pcm->name);
700 kfree(pcm);
701}
702
703void snd_hda_codec_pcm_put(struct hda_pcm *pcm)
704{
705 kref_put(&pcm->kref, release_pcm);
706}
707EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_put);
708
709struct hda_pcm *snd_hda_codec_pcm_new(struct hda_codec *codec,
710 const char *fmt, ...)
711{
712 struct hda_pcm *pcm;
713 va_list args;
714
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100715 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
716 if (!pcm)
717 return NULL;
718
719 pcm->codec = codec;
720 kref_init(&pcm->kref);
Takashi Iwai30e5f002015-04-27 16:39:19 +0200721 va_start(args, fmt);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100722 pcm->name = kvasprintf(GFP_KERNEL, fmt, args);
Takashi Iwai30e5f002015-04-27 16:39:19 +0200723 va_end(args);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100724 if (!pcm->name) {
725 kfree(pcm);
726 return NULL;
727 }
728
729 list_add_tail(&pcm->list, &codec->pcm_list_head);
730 return pcm;
731}
732EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_new);
733
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100734/*
735 * codec destructor
736 */
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100737static void codec_release_pcms(struct hda_codec *codec)
738{
739 struct hda_pcm *pcm, *n;
740
741 list_for_each_entry_safe(pcm, n, &codec->pcm_list_head, list) {
742 list_del_init(&pcm->list);
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100743 if (pcm->pcm)
744 snd_device_disconnect(codec->card, pcm->pcm);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100745 snd_hda_codec_pcm_put(pcm);
746 }
747}
748
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100749void snd_hda_codec_cleanup_for_unbind(struct hda_codec *codec)
750{
Takashi Iwaic4c25332015-03-03 17:22:12 +0100751 if (codec->registered) {
752 /* pm_runtime_put() is called in snd_hdac_device_exit() */
753 pm_runtime_get_noresume(hda_codec_dev(codec));
754 pm_runtime_disable(hda_codec_dev(codec));
755 codec->registered = 0;
756 }
757
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100758 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100759 if (!codec->in_freeing)
760 snd_hda_ctls_clear(codec);
761 codec_release_pcms(codec);
762 snd_hda_detach_beep_device(codec);
763 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
764 snd_hda_jack_tbl_clear(codec);
765 codec->proc_widget_hook = NULL;
766 codec->spec = NULL;
767
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100768 /* free only driver_pins so that init_pins + user_pins are restored */
769 snd_array_free(&codec->driver_pins);
770 snd_array_free(&codec->cvt_setups);
771 snd_array_free(&codec->spdif_out);
772 snd_array_free(&codec->verbs);
773 codec->preset = NULL;
774 codec->slave_dig_outs = NULL;
775 codec->spdif_status_reset = 0;
776 snd_array_free(&codec->mixers);
777 snd_array_free(&codec->nids);
778 remove_conn_list(codec);
Takashi Iwai4d75faa02015-02-25 14:42:38 +0100779 snd_hdac_regmap_exit(&codec->core);
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100780}
781
Takashi Iwaid8193872012-08-31 07:54:38 -0700782static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwaibb6ac722009-03-13 09:02:42 +0100783 unsigned int power_state);
784
Takashi Iwaic4c25332015-03-03 17:22:12 +0100785/* also called from hda_bind.c */
786void snd_hda_codec_register(struct hda_codec *codec)
787{
788 if (codec->registered)
789 return;
790 if (device_is_registered(hda_codec_dev(codec))) {
791 snd_hda_register_beep_device(codec);
Mengdong Lina5e7e072015-04-29 17:43:20 +0800792 snd_hdac_link_power(&codec->core, true);
Takashi Iwaic4c25332015-03-03 17:22:12 +0100793 pm_runtime_enable(hda_codec_dev(codec));
794 /* it was powered up in snd_hda_codec_new(), now all done */
795 snd_hda_power_down(codec);
796 codec->registered = 1;
797 }
798}
799
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100800static int snd_hda_codec_dev_register(struct snd_device *device)
801{
Takashi Iwaic4c25332015-03-03 17:22:12 +0100802 snd_hda_codec_register(device->device_data);
Takashi Iwaid604b392014-02-28 13:42:09 +0100803 return 0;
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100804}
805
806static int snd_hda_codec_dev_disconnect(struct snd_device *device)
807{
808 struct hda_codec *codec = device->device_data;
809
Takashi Iwaid604b392014-02-28 13:42:09 +0100810 snd_hda_detach_beep_device(codec);
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100811 return 0;
812}
813
Takashi Iwai2565c892014-02-19 11:41:09 +0100814static int snd_hda_codec_dev_free(struct snd_device *device)
815{
Takashi Iwaid56db742015-02-27 23:25:35 +0100816 struct hda_codec *codec = device->device_data;
817
818 codec->in_freeing = 1;
Takashi Iwai3256be62015-02-24 14:59:42 +0100819 snd_hdac_device_unregister(&codec->core);
Takashi Iwaia4577822015-06-11 14:02:49 +0200820 snd_hdac_link_power(&codec->core, false);
Takashi Iwaid56db742015-02-27 23:25:35 +0100821 put_device(hda_codec_dev(codec));
Takashi Iwai2565c892014-02-19 11:41:09 +0100822 return 0;
823}
824
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100825static void snd_hda_codec_dev_release(struct device *dev)
826{
Takashi Iwaid56db742015-02-27 23:25:35 +0100827 struct hda_codec *codec = dev_to_hda_codec(dev);
828
829 free_init_pincfgs(codec);
Takashi Iwai7639a062015-03-03 10:07:24 +0100830 snd_hdac_device_exit(&codec->core);
Takashi Iwaid56db742015-02-27 23:25:35 +0100831 snd_hda_sysfs_clear(codec);
Takashi Iwaid56db742015-02-27 23:25:35 +0100832 kfree(codec->modelname);
833 kfree(codec->wcaps);
Takashi Iwaid56db742015-02-27 23:25:35 +0100834 kfree(codec);
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100835}
836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837/**
838 * snd_hda_codec_new - create a HDA codec
839 * @bus: the bus to assign
840 * @codec_addr: the codec address
841 * @codecp: the pointer to store the generated codec
842 *
843 * Returns 0 if successful, or a negative error code.
844 */
Takashi Iwai6efdd852015-02-27 16:09:22 +0100845int snd_hda_codec_new(struct hda_bus *bus, struct snd_card *card,
846 unsigned int codec_addr, struct hda_codec **codecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
848 struct hda_codec *codec;
Jaroslav Kyselaba443682008-08-13 20:55:32 +0200849 char component[31];
Takashi Iwaid8193872012-08-31 07:54:38 -0700850 hda_nid_t fg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 int err;
Takashi Iwai2565c892014-02-19 11:41:09 +0100852 static struct snd_device_ops dev_ops = {
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100853 .dev_register = snd_hda_codec_dev_register,
854 .dev_disconnect = snd_hda_codec_dev_disconnect,
Takashi Iwai2565c892014-02-19 11:41:09 +0100855 .dev_free = snd_hda_codec_dev_free,
856 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Takashi Iwaida3cec32008-08-08 17:12:14 +0200858 if (snd_BUG_ON(!bus))
859 return -EINVAL;
860 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
861 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200863 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Takashi Iwaif4de8fe2015-02-27 16:17:18 +0100864 if (!codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Takashi Iwai7639a062015-03-03 10:07:24 +0100867 sprintf(component, "hdaudioC%dD%d", card->number, codec_addr);
868 err = snd_hdac_device_init(&codec->core, &bus->core, component,
869 codec_addr);
870 if (err < 0) {
871 kfree(codec);
872 return err;
873 }
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100874
Takashi Iwai7639a062015-03-03 10:07:24 +0100875 codec->core.dev.release = snd_hda_codec_dev_release;
876 codec->core.type = HDA_DEV_LEGACY;
Takashi Iwai058524482015-03-03 15:40:08 +0100877 codec->core.exec_verb = codec_exec_verb;
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 codec->bus = bus;
Takashi Iwai6efdd852015-02-27 16:09:22 +0100880 codec->card = card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 codec->addr = codec_addr;
Ingo Molnar62932df2006-01-16 16:34:20 +0100882 mutex_init(&codec->spdif_mutex);
Wu Fengguang5a9e02e2009-01-09 16:45:24 +0800883 mutex_init(&codec->control_mutex);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +0100884 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
885 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
Takashi Iwai3be14142009-02-20 14:11:16 +0100886 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwai346ff702009-02-23 09:42:57 +0100887 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
Takashi Iwaieb541332010-08-06 13:48:11 +0200888 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
Stephen Warren7c935972011-06-01 11:14:17 -0600889 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
Takashi Iwai361dab32012-05-09 14:35:27 +0200890 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100891 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100892 INIT_LIST_HEAD(&codec->conn_list);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100893 INIT_LIST_HEAD(&codec->pcm_list_head);
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100894
David Henningsson26a6cb62012-10-09 15:04:21 +0200895 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
Mengdong Lin7f132922013-11-29 01:48:45 -0500896 codec->depop_delay = -1;
David Henningssonf5662e12014-07-22 14:09:34 +0200897 codec->fixup_id = HDA_FIXUP_ID_NOT_SET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Takashi Iwai83012a72012-08-24 18:38:08 +0200899#ifdef CONFIG_PM
Takashi Iwaicc72da72015-02-19 16:00:22 +0100900 codec->power_jiffies = jiffies;
Takashi Iwaicb53c622007-08-10 17:21:45 +0200901#endif
902
Takashi Iwai648a8d22014-02-25 10:38:13 +0100903 snd_hda_sysfs_init(codec);
904
Takashi Iwaic382a9f2012-05-07 15:01:02 +0200905 if (codec->bus->modelname) {
906 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
907 if (!codec->modelname) {
Heloise NH6986a0e2015-07-17 09:42:06 +0800908 err = -ENOMEM;
Takashi Iwai13aeaf62014-02-25 07:53:47 +0100909 goto error;
Takashi Iwaic382a9f2012-05-07 15:01:02 +0200910 }
911 }
912
Takashi Iwai7639a062015-03-03 10:07:24 +0100913 fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
Takashi Iwaid8193872012-08-31 07:54:38 -0700914 err = read_widget_caps(codec, fg);
Takashi Iwaif4de8fe2015-02-27 16:17:18 +0100915 if (err < 0)
Takashi Iwai3be14142009-02-20 14:11:16 +0100916 goto error;
Takashi Iwai3be14142009-02-20 14:11:16 +0100917 err = read_pin_defaults(codec);
918 if (err < 0)
919 goto error;
Takashi Iwai54d17402005-11-21 16:33:22 +0100920
Takashi Iwaibb6ac722009-03-13 09:02:42 +0100921 /* power-up all before initialization */
Takashi Iwaid8193872012-08-31 07:54:38 -0700922 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaibb6ac722009-03-13 09:02:42 +0100923
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200924 snd_hda_codec_proc_new(codec);
925
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200926 snd_hda_create_hwdep(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200927
Takashi Iwai7639a062015-03-03 10:07:24 +0100928 sprintf(component, "HDA:%08x,%08x,%08x", codec->core.vendor_id,
929 codec->core.subsystem_id, codec->core.revision_id);
Takashi Iwai6efdd852015-02-27 16:09:22 +0100930 snd_component_add(card, component);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200931
Takashi Iwai6efdd852015-02-27 16:09:22 +0100932 err = snd_device_new(card, SNDRV_DEV_CODEC, codec, &dev_ops);
Takashi Iwai2565c892014-02-19 11:41:09 +0100933 if (err < 0)
934 goto error;
935
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200936 if (codecp)
937 *codecp = codec;
938 return 0;
Takashi Iwai3be14142009-02-20 14:11:16 +0100939
940 error:
Takashi Iwaid56db742015-02-27 23:25:35 +0100941 put_device(hda_codec_dev(codec));
Takashi Iwai3be14142009-02-20 14:11:16 +0100942 return err;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200943}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100944EXPORT_SYMBOL_GPL(snd_hda_codec_new);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +0200945
Takashi Iwai95a962c2014-10-29 16:03:58 +0100946/**
947 * snd_hda_codec_update_widgets - Refresh widget caps and pin defaults
948 * @codec: the HDA codec
949 *
950 * Forcibly refresh the all widget caps and the init pin configurations of
951 * the given codec.
952 */
Mengdong Lina15d05d2013-02-08 17:09:31 -0500953int snd_hda_codec_update_widgets(struct hda_codec *codec)
954{
955 hda_nid_t fg;
956 int err;
957
Takashi Iwai9dcc1442015-08-25 08:52:55 +0200958 err = snd_hdac_refresh_widget_sysfs(&codec->core);
Takashi Iwai7639a062015-03-03 10:07:24 +0100959 if (err < 0)
960 return err;
961
Mengdong Lina15d05d2013-02-08 17:09:31 -0500962 /* Assume the function group node does not change,
963 * only the widget nodes may change.
964 */
965 kfree(codec->wcaps);
Takashi Iwai7639a062015-03-03 10:07:24 +0100966 fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
Mengdong Lina15d05d2013-02-08 17:09:31 -0500967 err = read_widget_caps(codec, fg);
Takashi Iwaif4de8fe2015-02-27 16:17:18 +0100968 if (err < 0)
Mengdong Lina15d05d2013-02-08 17:09:31 -0500969 return err;
Mengdong Lina15d05d2013-02-08 17:09:31 -0500970
971 snd_array_free(&codec->init_pins);
972 err = read_pin_defaults(codec);
973
974 return err;
975}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100976EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets);
Mengdong Lina15d05d2013-02-08 17:09:31 -0500977
Takashi Iwaied360812012-08-08 17:12:52 +0200978/* update the stream-id if changed */
979static void update_pcm_stream_id(struct hda_codec *codec,
980 struct hda_cvt_setup *p, hda_nid_t nid,
981 u32 stream_tag, int channel_id)
982{
983 unsigned int oldval, newval;
984
985 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
986 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
987 newval = (stream_tag << 4) | channel_id;
988 if (oldval != newval)
989 snd_hda_codec_write(codec, nid, 0,
990 AC_VERB_SET_CHANNEL_STREAMID,
991 newval);
992 p->stream_tag = stream_tag;
993 p->channel_id = channel_id;
994 }
995}
996
997/* update the format-id if changed */
998static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
999 hda_nid_t nid, int format)
1000{
1001 unsigned int oldval;
1002
1003 if (p->format_id != format) {
1004 oldval = snd_hda_codec_read(codec, nid, 0,
1005 AC_VERB_GET_STREAM_FORMAT, 0);
1006 if (oldval != format) {
1007 msleep(1);
1008 snd_hda_codec_write(codec, nid, 0,
1009 AC_VERB_SET_STREAM_FORMAT,
1010 format);
1011 }
1012 p->format_id = format;
1013 }
1014}
1015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016/**
1017 * snd_hda_codec_setup_stream - set up the codec for streaming
1018 * @codec: the CODEC to set up
1019 * @nid: the NID to set up
1020 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1021 * @channel_id: channel id to pass, zero based.
1022 * @format: stream format.
1023 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001024void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1025 u32 stream_tag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 int channel_id, int format)
1027{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001028 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001029 struct hda_cvt_setup *p;
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001030 int type;
Takashi Iwaieb541332010-08-06 13:48:11 +02001031 int i;
1032
Takashi Iwai0ba21762007-04-16 11:29:14 +02001033 if (!nid)
Takashi Iwaid21b37e2005-04-20 13:45:55 +02001034 return;
1035
Takashi Iwai4e76a882014-02-25 12:21:03 +01001036 codec_dbg(codec,
1037 "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1038 nid, stream_tag, channel_id, format);
Takashi Iwaieb541332010-08-06 13:48:11 +02001039 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001040 if (!p)
Takashi Iwaieb541332010-08-06 13:48:11 +02001041 return;
Takashi Iwaied360812012-08-08 17:12:52 +02001042
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01001043 if (codec->patch_ops.stream_pm)
1044 codec->patch_ops.stream_pm(codec, nid, true);
Takashi Iwaied360812012-08-08 17:12:52 +02001045 if (codec->pcm_format_first)
1046 update_pcm_format(codec, p, nid, format);
1047 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1048 if (!codec->pcm_format_first)
1049 update_pcm_format(codec, p, nid, format);
1050
Takashi Iwaieb541332010-08-06 13:48:11 +02001051 p->active = 1;
1052 p->dirty = 0;
1053
1054 /* make other inactive cvts with the same stream-tag dirty */
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001055 type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwaid068ebc2015-03-02 23:22:59 +01001056 list_for_each_codec(c, codec->bus) {
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001057 for (i = 0; i < c->cvt_setups.used; i++) {
1058 p = snd_array_elem(&c->cvt_setups, i);
Takashi Iwai62b7e5e2010-10-22 17:15:47 +02001059 if (!p->active && p->stream_tag == stream_tag &&
David Henningsson54c2a892012-02-01 12:05:41 +01001060 get_wcaps_type(get_wcaps(c, p->nid)) == type)
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001061 p->dirty = 1;
1062 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001065EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Takashi Iwaif0cea792010-08-13 11:56:53 +02001067static void really_cleanup_stream(struct hda_codec *codec,
1068 struct hda_cvt_setup *q);
1069
Takashi Iwaid5191e52009-11-16 14:58:17 +01001070/**
Takashi Iwaif0cea792010-08-13 11:56:53 +02001071 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
Takashi Iwaid5191e52009-11-16 14:58:17 +01001072 * @codec: the CODEC to clean up
1073 * @nid: the NID to clean up
Takashi Iwaif0cea792010-08-13 11:56:53 +02001074 * @do_now: really clean up the stream instead of clearing the active flag
Takashi Iwaid5191e52009-11-16 14:58:17 +01001075 */
Takashi Iwaif0cea792010-08-13 11:56:53 +02001076void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1077 int do_now)
Takashi Iwai888afa12008-03-18 09:57:50 +01001078{
Takashi Iwaieb541332010-08-06 13:48:11 +02001079 struct hda_cvt_setup *p;
1080
Takashi Iwai888afa12008-03-18 09:57:50 +01001081 if (!nid)
1082 return;
1083
Takashi Iwai0e7adbe2010-10-25 10:37:11 +02001084 if (codec->no_sticky_stream)
1085 do_now = 1;
1086
Takashi Iwai4e76a882014-02-25 12:21:03 +01001087 codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid);
Takashi Iwaieb541332010-08-06 13:48:11 +02001088 p = get_hda_cvt_setup(codec, nid);
Takashi Iwai6c35ae32013-05-10 13:39:50 +02001089 if (p) {
Takashi Iwaif0cea792010-08-13 11:56:53 +02001090 /* here we just clear the active flag when do_now isn't set;
1091 * actual clean-ups will be done later in
1092 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1093 */
1094 if (do_now)
1095 really_cleanup_stream(codec, p);
1096 else
1097 p->active = 0;
1098 }
Takashi Iwai888afa12008-03-18 09:57:50 +01001099}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001100EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream);
Takashi Iwai888afa12008-03-18 09:57:50 +01001101
Takashi Iwaieb541332010-08-06 13:48:11 +02001102static void really_cleanup_stream(struct hda_codec *codec,
1103 struct hda_cvt_setup *q)
1104{
1105 hda_nid_t nid = q->nid;
Takashi Iwai218264a2011-09-27 17:33:45 +02001106 if (q->stream_tag || q->channel_id)
1107 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1108 if (q->format_id)
1109 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1110);
Takashi Iwaieb541332010-08-06 13:48:11 +02001111 memset(q, 0, sizeof(*q));
1112 q->nid = nid;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01001113 if (codec->patch_ops.stream_pm)
1114 codec->patch_ops.stream_pm(codec, nid, false);
Takashi Iwaieb541332010-08-06 13:48:11 +02001115}
1116
1117/* clean up the all conflicting obsolete streams */
1118static void purify_inactive_streams(struct hda_codec *codec)
1119{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001120 struct hda_codec *c;
Takashi Iwaieb541332010-08-06 13:48:11 +02001121 int i;
1122
Takashi Iwaid068ebc2015-03-02 23:22:59 +01001123 list_for_each_codec(c, codec->bus) {
Takashi Iwai3f50ac62010-08-20 09:44:36 +02001124 for (i = 0; i < c->cvt_setups.used; i++) {
1125 struct hda_cvt_setup *p;
1126 p = snd_array_elem(&c->cvt_setups, i);
1127 if (p->dirty)
1128 really_cleanup_stream(c, p);
1129 }
Takashi Iwaieb541332010-08-06 13:48:11 +02001130 }
1131}
1132
Takashi Iwai2a439522011-07-26 09:52:50 +02001133#ifdef CONFIG_PM
Takashi Iwaieb541332010-08-06 13:48:11 +02001134/* clean up all streams; called from suspend */
1135static void hda_cleanup_all_streams(struct hda_codec *codec)
1136{
1137 int i;
1138
1139 for (i = 0; i < codec->cvt_setups.used; i++) {
1140 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1141 if (p->stream_tag)
1142 really_cleanup_stream(codec, p);
1143 }
1144}
Mike Waychison1c7276c2011-04-20 12:04:36 -07001145#endif
Takashi Iwaieb541332010-08-06 13:48:11 +02001146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147/*
1148 * amp access functions
1149 */
1150
Takashi Iwaid5191e52009-11-16 14:58:17 +01001151/**
1152 * query_amp_caps - query AMP capabilities
1153 * @codec: the HD-auio codec
1154 * @nid: the NID to query
1155 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1156 *
1157 * Query AMP capabilities for the given widget and direction.
1158 * Returns the obtained capability bits.
1159 *
1160 * When cap bits have been already read, this doesn't read again but
1161 * returns the cached value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 */
Matthew Ranostay09a99952008-01-24 11:49:21 +01001163u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164{
Takashi Iwaifaa75f82015-02-26 08:54:56 +01001165 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1166 nid = codec->core.afg;
1167 return snd_hda_param_read(codec, nid,
1168 direction == HDA_OUTPUT ?
1169 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001171EXPORT_SYMBOL_GPL(query_amp_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Takashi Iwaid5191e52009-11-16 14:58:17 +01001173/**
David Henningsson861a04e2014-09-23 10:38:17 +02001174 * snd_hda_check_amp_caps - query AMP capabilities
1175 * @codec: the HD-audio codec
1176 * @nid: the NID to query
1177 * @dir: either #HDA_INPUT or #HDA_OUTPUT
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001178 * @bits: bit mask to check the result
David Henningsson861a04e2014-09-23 10:38:17 +02001179 *
1180 * Check whether the widget has the given amp capability for the direction.
1181 */
1182bool snd_hda_check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
1183 int dir, unsigned int bits)
1184{
1185 if (!nid)
1186 return false;
1187 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
1188 if (query_amp_caps(codec, nid, dir) & bits)
1189 return true;
1190 return false;
1191}
1192EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps);
1193
1194/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01001195 * snd_hda_override_amp_caps - Override the AMP capabilities
1196 * @codec: the CODEC to clean up
1197 * @nid: the NID to clean up
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001198 * @dir: either #HDA_INPUT or #HDA_OUTPUT
Takashi Iwaid5191e52009-11-16 14:58:17 +01001199 * @caps: the capability bits to set
1200 *
1201 * Override the cached AMP caps bits value by the given one.
1202 * This function is useful if the driver needs to adjust the AMP ranges,
1203 * e.g. limit to 0dB, etc.
1204 *
1205 * Returns zero if successful or a negative error code.
1206 */
Takashi Iwai897cc182007-05-29 19:01:37 +02001207int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1208 unsigned int caps)
1209{
Takashi Iwaifaa75f82015-02-26 08:54:56 +01001210 unsigned int parm;
1211
1212 snd_hda_override_wcaps(codec, nid,
1213 get_wcaps(codec, nid) | AC_WCAP_AMP_OVRD);
1214 parm = dir == HDA_OUTPUT ? AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP;
1215 return snd_hdac_override_parm(&codec->core, nid, parm, caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001216}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001217EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps);
Takashi Iwai897cc182007-05-29 19:01:37 +02001218
Takashi Iwaid5191e52009-11-16 14:58:17 +01001219/**
Takashi Iwaia686ec42015-06-11 10:51:28 +02001220 * snd_hda_codec_amp_update - update the AMP mono value
1221 * @codec: HD-audio codec
1222 * @nid: NID to read the AMP value
1223 * @ch: channel to update (0 or 1)
1224 * @dir: #HDA_INPUT or #HDA_OUTPUT
1225 * @idx: the index value (only for input direction)
1226 * @mask: bit mask to set
1227 * @val: the bits value to set
1228 *
1229 * Update the AMP values for the given channel, direction and index.
1230 */
1231int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid,
1232 int ch, int dir, int idx, int mask, int val)
1233{
1234 unsigned int cmd = snd_hdac_regmap_encode_amp(nid, ch, dir, idx);
1235
1236 /* enable fake mute if no h/w mute but min=mute */
1237 if ((query_amp_caps(codec, nid, dir) &
1238 (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) == AC_AMPCAP_MIN_MUTE)
1239 cmd |= AC_AMP_FAKE_MUTE;
1240 return snd_hdac_regmap_update_raw(&codec->core, cmd, mask, val);
1241}
1242EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update);
1243
1244/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01001245 * snd_hda_codec_amp_stereo - update the AMP stereo values
1246 * @codec: HD-audio codec
1247 * @nid: NID to read the AMP value
1248 * @direction: #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 like snd_hda_codec_amp_update(), but for a
1254 * stereo widget with the same mask and value.
Takashi Iwai47fd8302007-08-10 17:11:07 +02001255 */
1256int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1257 int direction, int idx, int mask, int val)
1258{
1259 int ch, ret = 0;
Takashi Iwai467126462010-03-29 09:19:38 +02001260
1261 if (snd_BUG_ON(mask & ~0xff))
1262 mask &= 0xff;
Takashi Iwai47fd8302007-08-10 17:11:07 +02001263 for (ch = 0; ch < 2; ch++)
1264 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1265 idx, mask, val);
1266 return ret;
1267}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001268EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo);
Takashi Iwai47fd8302007-08-10 17:11:07 +02001269
Takashi Iwai95a962c2014-10-29 16:03:58 +01001270/**
1271 * snd_hda_codec_amp_init - initialize the AMP value
1272 * @codec: the HDA codec
1273 * @nid: NID to read the AMP value
1274 * @ch: channel (left=0 or right=1)
1275 * @dir: #HDA_INPUT or #HDA_OUTPUT
1276 * @idx: the index value (only for input direction)
1277 * @mask: bit mask to set
1278 * @val: the bits value to set
1279 *
1280 * Works like snd_hda_codec_amp_update() but it writes the value only at
Takashi Iwai280e57d2012-12-14 10:32:21 +01001281 * the first access. If the amp was already initialized / updated beforehand,
1282 * this does nothing.
1283 */
1284int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
1285 int dir, int idx, int mask, int val)
1286{
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01001287 int orig;
1288
1289 if (!codec->core.regmap)
1290 return -EINVAL;
1291 regcache_cache_only(codec->core.regmap, true);
1292 orig = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1293 regcache_cache_only(codec->core.regmap, false);
1294 if (orig >= 0)
1295 return 0;
1296 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx, mask, val);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001297}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001298EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001299
Takashi Iwai95a962c2014-10-29 16:03:58 +01001300/**
1301 * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value
1302 * @codec: the HDA codec
1303 * @nid: NID to read the AMP value
1304 * @dir: #HDA_INPUT or #HDA_OUTPUT
1305 * @idx: the index value (only for input direction)
1306 * @mask: bit mask to set
1307 * @val: the bits value to set
1308 *
1309 * Call snd_hda_codec_amp_init() for both stereo channels.
1310 */
Takashi Iwai280e57d2012-12-14 10:32:21 +01001311int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
1312 int dir, int idx, int mask, int val)
1313{
1314 int ch, ret = 0;
1315
1316 if (snd_BUG_ON(mask & ~0xff))
1317 mask &= 0xff;
1318 for (ch = 0; ch < 2; ch++)
1319 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
1320 idx, mask, val);
1321 return ret;
1322}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001323EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo);
Takashi Iwai280e57d2012-12-14 10:32:21 +01001324
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001325static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1326 unsigned int ofs)
1327{
1328 u32 caps = query_amp_caps(codec, nid, dir);
1329 /* get num steps */
1330 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1331 if (ofs < caps)
1332 caps -= ofs;
1333 return caps;
1334}
1335
Takashi Iwaid5191e52009-11-16 14:58:17 +01001336/**
1337 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001338 * @kcontrol: referred ctl element
1339 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01001340 *
1341 * The control element is supposed to have the private_value field
1342 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1343 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001344int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1345 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346{
1347 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1348 u16 nid = get_amp_nid(kcontrol);
1349 u8 chs = get_amp_channels(kcontrol);
1350 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001351 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001353 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1354 uinfo->count = chs == 3 ? 2 : 1;
1355 uinfo->value.integer.min = 0;
1356 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1357 if (!uinfo->value.integer.max) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001358 codec_warn(codec,
1359 "num_steps = 0 for NID=0x%x (ctl = %s)\n",
1360 nid, kcontrol->id.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 return -EINVAL;
1362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 return 0;
1364}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001365EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001367
1368static inline unsigned int
1369read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1370 int ch, int dir, int idx, unsigned int ofs)
1371{
1372 unsigned int val;
1373 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1374 val &= HDA_AMP_VOLMASK;
1375 if (val >= ofs)
1376 val -= ofs;
1377 else
1378 val = 0;
1379 return val;
1380}
1381
1382static inline int
1383update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1384 int ch, int dir, int idx, unsigned int ofs,
1385 unsigned int val)
1386{
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001387 unsigned int maxval;
1388
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001389 if (val > 0)
1390 val += ofs;
Takashi Iwai7ccc3ef2010-07-26 17:00:15 +02001391 /* ofs = 0: raw max value */
1392 maxval = get_amp_max_value(codec, nid, dir, 0);
Takashi Iwaiafbd9b82010-07-08 18:40:37 +02001393 if (val > maxval)
1394 val = maxval;
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01001395 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1396 HDA_AMP_VOLMASK, val);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001397}
1398
Takashi Iwaid5191e52009-11-16 14:58:17 +01001399/**
1400 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001401 * @kcontrol: ctl element
1402 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01001403 *
1404 * The control element is supposed to have the private_value field
1405 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1406 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001407int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1408 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409{
1410 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1411 hda_nid_t nid = get_amp_nid(kcontrol);
1412 int chs = get_amp_channels(kcontrol);
1413 int dir = get_amp_direction(kcontrol);
1414 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001415 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 long *valp = ucontrol->value.integer.value;
1417
1418 if (chs & 1)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001419 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001421 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 return 0;
1423}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001424EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
Takashi Iwaid5191e52009-11-16 14:58:17 +01001426/**
1427 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001428 * @kcontrol: ctl element
1429 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01001430 *
1431 * The control element is supposed to have the private_value field
1432 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1433 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02001434int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1435 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436{
1437 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1438 hda_nid_t nid = get_amp_nid(kcontrol);
1439 int chs = get_amp_channels(kcontrol);
1440 int dir = get_amp_direction(kcontrol);
1441 int idx = get_amp_index(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001442 unsigned int ofs = get_amp_offset(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 long *valp = ucontrol->value.integer.value;
1444 int change = 0;
1445
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001446 if (chs & 1) {
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001447 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02001448 valp++;
1449 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02001450 if (chs & 2)
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001451 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 return change;
1453}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001454EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Takashi Iwaid5191e52009-11-16 14:58:17 +01001456/**
1457 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
Takashi Iwaia11e9b12014-10-29 15:06:01 +01001458 * @kcontrol: ctl element
1459 * @op_flag: operation flag
1460 * @size: byte size of input TLV
1461 * @_tlv: TLV data
Takashi Iwaid5191e52009-11-16 14:58:17 +01001462 *
1463 * The control element is supposed to have the private_value field
1464 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1465 */
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001466int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1467 unsigned int size, unsigned int __user *_tlv)
1468{
1469 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1470 hda_nid_t nid = get_amp_nid(kcontrol);
1471 int dir = get_amp_direction(kcontrol);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001472 unsigned int ofs = get_amp_offset(kcontrol);
Clemens Ladischde8c85f2010-10-15 10:32:50 +02001473 bool min_mute = get_amp_min_mute(kcontrol);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001474 u32 caps, val1, val2;
1475
1476 if (size < 4 * sizeof(unsigned int))
1477 return -ENOMEM;
1478 caps = query_amp_caps(codec, nid, dir);
Takashi Iwai0ba21762007-04-16 11:29:14 +02001479 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1480 val2 = (val2 + 1) * 25;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001481 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
Takashi Iwai29fdbec2009-01-20 13:07:55 +01001482 val1 += ofs;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001483 val1 = ((int)val1) * ((int)val2);
Takashi Iwai38681372012-02-27 15:00:58 +01001484 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
Takashi Iwaic08d9162010-10-17 10:40:53 +02001485 val2 |= TLV_DB_SCALE_MUTE;
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001486 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1487 return -EFAULT;
1488 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1489 return -EFAULT;
1490 if (put_user(val1, _tlv + 2))
1491 return -EFAULT;
1492 if (put_user(val2, _tlv + 3))
1493 return -EFAULT;
1494 return 0;
1495}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001496EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv);
Jaroslav Kysela302e9c52006-07-05 17:39:49 +02001497
Takashi Iwaid5191e52009-11-16 14:58:17 +01001498/**
1499 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
1500 * @codec: HD-audio codec
1501 * @nid: NID of a reference widget
1502 * @dir: #HDA_INPUT or #HDA_OUTPUT
1503 * @tlv: TLV data to be stored, at least 4 elements
1504 *
1505 * Set (static) TLV data for a virtual master volume using the AMP caps
1506 * obtained from the reference NID.
1507 * The volume range is recalculated as if the max volume is 0dB.
Takashi Iwai2134ea42008-01-10 16:53:55 +01001508 */
1509void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1510 unsigned int *tlv)
1511{
1512 u32 caps;
1513 int nums, step;
1514
1515 caps = query_amp_caps(codec, nid, dir);
1516 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1517 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1518 step = (step + 1) * 25;
1519 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1520 tlv[1] = 2 * sizeof(unsigned int);
1521 tlv[2] = -nums * step;
1522 tlv[3] = step;
1523}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001524EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001525
1526/* find a mixer control element with the given name */
Takashi Iwai09f99702008-02-04 12:31:13 +01001527static struct snd_kcontrol *
Takashi Iwaidcda5802012-10-12 17:24:51 +02001528find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
Takashi Iwai2134ea42008-01-10 16:53:55 +01001529{
1530 struct snd_ctl_elem_id id;
1531 memset(&id, 0, sizeof(id));
1532 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Takashi Iwaidcda5802012-10-12 17:24:51 +02001533 id.device = dev;
Takashi Iwai09f99702008-02-04 12:31:13 +01001534 id.index = idx;
Takashi Iwai18cb7102009-04-16 10:22:24 +02001535 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
1536 return NULL;
Takashi Iwai2134ea42008-01-10 16:53:55 +01001537 strcpy(id.name, name);
Takashi Iwai6efdd852015-02-27 16:09:22 +01001538 return snd_ctl_find_id(codec->card, &id);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001539}
1540
Takashi Iwaid5191e52009-11-16 14:58:17 +01001541/**
1542 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
1543 * @codec: HD-audio codec
1544 * @name: ctl id name string
1545 *
1546 * Get the control element with the given id string and IFACE_MIXER.
1547 */
Takashi Iwai09f99702008-02-04 12:31:13 +01001548struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1549 const char *name)
1550{
Takashi Iwaidcda5802012-10-12 17:24:51 +02001551 return find_mixer_ctl(codec, name, 0, 0);
Takashi Iwai09f99702008-02-04 12:31:13 +01001552}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001553EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl);
Takashi Iwai09f99702008-02-04 12:31:13 +01001554
Takashi Iwaidcda5802012-10-12 17:24:51 +02001555static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01001556 int start_idx)
Takashi Iwai1afe2062010-12-23 10:17:52 +01001557{
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01001558 int i, idx;
1559 /* 16 ctlrs should be large enough */
1560 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
1561 if (!find_mixer_ctl(codec, name, 0, idx))
Takashi Iwai1afe2062010-12-23 10:17:52 +01001562 return idx;
1563 }
1564 return -EBUSY;
1565}
1566
Takashi Iwaid5191e52009-11-16 14:58:17 +01001567/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001568 * snd_hda_ctl_add - Add a control element and assign to the codec
Takashi Iwaid5191e52009-11-16 14:58:17 +01001569 * @codec: HD-audio codec
1570 * @nid: corresponding NID (optional)
1571 * @kctl: the control element to assign
1572 *
1573 * Add the given control element to an array inside the codec instance.
1574 * All control elements belonging to a codec are supposed to be added
1575 * by this function so that a proper clean-up works at the free or
1576 * reconfiguration time.
1577 *
1578 * If non-zero @nid is passed, the NID is assigned to the control element.
1579 * The assignment is shown in the codec proc file.
1580 *
1581 * snd_hda_ctl_add() checks the control subdev id field whether
1582 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001583 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
1584 * specifies if kctl->private_value is a HDA amplifier value.
Takashi Iwaid5191e52009-11-16 14:58:17 +01001585 */
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001586int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
1587 struct snd_kcontrol *kctl)
Takashi Iwaid13bd412008-07-30 15:01:45 +02001588{
1589 int err;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001590 unsigned short flags = 0;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001591 struct hda_nid_item *item;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001592
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001593 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001594 flags |= HDA_NID_ITEM_AMP;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001595 if (nid == 0)
1596 nid = get_amp_nid_(kctl->private_value);
1597 }
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001598 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
1599 nid = kctl->id.subdevice & 0xffff;
Jaroslav Kysela5e26dfd2009-12-10 13:57:01 +01001600 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01001601 kctl->id.subdevice = 0;
Takashi Iwai6efdd852015-02-27 16:09:22 +01001602 err = snd_ctl_add(codec->card, kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001603 if (err < 0)
1604 return err;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001605 item = snd_array_new(&codec->mixers);
1606 if (!item)
Takashi Iwaid13bd412008-07-30 15:01:45 +02001607 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001608 item->kctl = kctl;
1609 item->nid = nid;
Jaroslav Kysela9e3fd872009-12-08 17:45:25 +01001610 item->flags = flags;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001611 return 0;
1612}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001613EXPORT_SYMBOL_GPL(snd_hda_ctl_add);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001614
Takashi Iwaid5191e52009-11-16 14:58:17 +01001615/**
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001616 * snd_hda_add_nid - Assign a NID to a control element
1617 * @codec: HD-audio codec
1618 * @nid: corresponding NID (optional)
1619 * @kctl: the control element to assign
1620 * @index: index to kctl
1621 *
1622 * Add the given control element to an array inside the codec instance.
1623 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
1624 * NID:KCTL mapping - for example "Capture Source" selector.
1625 */
1626int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
1627 unsigned int index, hda_nid_t nid)
1628{
1629 struct hda_nid_item *item;
1630
1631 if (nid > 0) {
1632 item = snd_array_new(&codec->nids);
1633 if (!item)
1634 return -ENOMEM;
1635 item->kctl = kctl;
1636 item->index = index;
1637 item->nid = nid;
1638 return 0;
1639 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01001640 codec_err(codec, "no NID for mapping control %s:%d:%d\n",
1641 kctl->id.name, kctl->id.index, index);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001642 return -EINVAL;
1643}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001644EXPORT_SYMBOL_GPL(snd_hda_add_nid);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001645
1646/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01001647 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
1648 * @codec: HD-audio codec
1649 */
Takashi Iwaid13bd412008-07-30 15:01:45 +02001650void snd_hda_ctls_clear(struct hda_codec *codec)
1651{
1652 int i;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001653 struct hda_nid_item *items = codec->mixers.list;
Takashi Iwaid13bd412008-07-30 15:01:45 +02001654 for (i = 0; i < codec->mixers.used; i++)
Takashi Iwai6efdd852015-02-27 16:09:22 +01001655 snd_ctl_remove(codec->card, items[i].kctl);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001656 snd_array_free(&codec->mixers);
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01001657 snd_array_free(&codec->nids);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001658}
1659
Takashi Iwai95a962c2014-10-29 16:03:58 +01001660/**
1661 * snd_hda_lock_devices - pseudo device locking
1662 * @bus: the BUS
1663 *
Takashi Iwaia65d6292009-02-23 16:57:04 +01001664 * toggle card->shutdown to allow/disallow the device access (as a hack)
1665 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001666int snd_hda_lock_devices(struct hda_bus *bus)
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001667{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001668 struct snd_card *card = bus->card;
1669 struct hda_codec *codec;
1670
Takashi Iwaia65d6292009-02-23 16:57:04 +01001671 spin_lock(&card->files_lock);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001672 if (card->shutdown)
1673 goto err_unlock;
Takashi Iwaia65d6292009-02-23 16:57:04 +01001674 card->shutdown = 1;
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001675 if (!list_empty(&card->ctl_files))
1676 goto err_clear;
1677
Takashi Iwaid068ebc2015-03-02 23:22:59 +01001678 list_for_each_codec(codec, bus) {
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01001679 struct hda_pcm *cpcm;
1680 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001681 if (!cpcm->pcm)
1682 continue;
1683 if (cpcm->pcm->streams[0].substream_opened ||
1684 cpcm->pcm->streams[1].substream_opened)
1685 goto err_clear;
1686 }
1687 }
Takashi Iwaia65d6292009-02-23 16:57:04 +01001688 spin_unlock(&card->files_lock);
1689 return 0;
Takashi Iwaia65d6292009-02-23 16:57:04 +01001690
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001691 err_clear:
1692 card->shutdown = 0;
1693 err_unlock:
1694 spin_unlock(&card->files_lock);
1695 return -EINVAL;
1696}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001697EXPORT_SYMBOL_GPL(snd_hda_lock_devices);
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001698
Takashi Iwai95a962c2014-10-29 16:03:58 +01001699/**
1700 * snd_hda_unlock_devices - pseudo device unlocking
1701 * @bus: the BUS
1702 */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001703void snd_hda_unlock_devices(struct hda_bus *bus)
Takashi Iwaia65d6292009-02-23 16:57:04 +01001704{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001705 struct snd_card *card = bus->card;
1706
Takashi Iwaia65d6292009-02-23 16:57:04 +01001707 spin_lock(&card->files_lock);
1708 card->shutdown = 0;
1709 spin_unlock(&card->files_lock);
1710}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001711EXPORT_SYMBOL_GPL(snd_hda_unlock_devices);
Takashi Iwaia65d6292009-02-23 16:57:04 +01001712
Takashi Iwaid5191e52009-11-16 14:58:17 +01001713/**
1714 * snd_hda_codec_reset - Clear all objects assigned to the codec
1715 * @codec: HD-audio codec
1716 *
1717 * This frees the all PCM and control elements assigned to the codec, and
1718 * clears the caches and restores the pin default configurations.
1719 *
1720 * When a device is being used, it returns -EBSY. If successfully freed,
1721 * returns zero.
1722 */
Takashi Iwaia65d6292009-02-23 16:57:04 +01001723int snd_hda_codec_reset(struct hda_codec *codec)
1724{
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001725 struct hda_bus *bus = codec->bus;
Takashi Iwaia65d6292009-02-23 16:57:04 +01001726
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001727 if (snd_hda_lock_devices(bus) < 0)
Takashi Iwaia65d6292009-02-23 16:57:04 +01001728 return -EBUSY;
Takashi Iwaia65d6292009-02-23 16:57:04 +01001729
1730 /* OK, let it free */
Takashi Iwai3256be62015-02-24 14:59:42 +01001731 snd_hdac_device_unregister(&codec->core);
Takashi Iwaid8a766a2015-02-17 15:25:37 +01001732
Takashi Iwaia65d6292009-02-23 16:57:04 +01001733 /* allow device access again */
Takashi Iwaid3d020b2012-04-26 12:11:44 +02001734 snd_hda_unlock_devices(bus);
Takashi Iwaia65d6292009-02-23 16:57:04 +01001735 return 0;
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02001736}
1737
Takashi Iwai6194b992014-06-06 18:12:16 +02001738typedef int (*map_slave_func_t)(struct hda_codec *, void *, struct snd_kcontrol *);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01001739
1740/* apply the function to all matching slave ctls in the mixer list */
1741static int map_slaves(struct hda_codec *codec, const char * const *slaves,
Takashi Iwai9322ca52012-02-03 14:28:01 +01001742 const char *suffix, map_slave_func_t func, void *data)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01001743{
1744 struct hda_nid_item *items;
1745 const char * const *s;
1746 int i, err;
1747
1748 items = codec->mixers.list;
1749 for (i = 0; i < codec->mixers.used; i++) {
1750 struct snd_kcontrol *sctl = items[i].kctl;
Takashi Iwaica16ec02013-10-28 12:00:35 +01001751 if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01001752 continue;
1753 for (s = slaves; *s; s++) {
Takashi Iwai9322ca52012-02-03 14:28:01 +01001754 char tmpname[sizeof(sctl->id.name)];
1755 const char *name = *s;
1756 if (suffix) {
1757 snprintf(tmpname, sizeof(tmpname), "%s %s",
1758 name, suffix);
1759 name = tmpname;
1760 }
Takashi Iwai9322ca52012-02-03 14:28:01 +01001761 if (!strcmp(sctl->id.name, name)) {
Takashi Iwai6194b992014-06-06 18:12:16 +02001762 err = func(codec, data, sctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01001763 if (err)
1764 return err;
1765 break;
1766 }
1767 }
1768 }
1769 return 0;
1770}
1771
Takashi Iwai6194b992014-06-06 18:12:16 +02001772static int check_slave_present(struct hda_codec *codec,
1773 void *data, struct snd_kcontrol *sctl)
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01001774{
1775 return 1;
1776}
1777
Takashi Iwai18478e82012-03-09 17:51:10 +01001778/* guess the value corresponding to 0dB */
Takashi Iwai6194b992014-06-06 18:12:16 +02001779static int get_kctl_0dB_offset(struct hda_codec *codec,
1780 struct snd_kcontrol *kctl, int *step_to_check)
Takashi Iwai18478e82012-03-09 17:51:10 +01001781{
1782 int _tlv[4];
1783 const int *tlv = NULL;
1784 int val = -1;
1785
1786 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
1787 /* FIXME: set_fs() hack for obtaining user-space TLV data */
1788 mm_segment_t fs = get_fs();
1789 set_fs(get_ds());
1790 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
1791 tlv = _tlv;
1792 set_fs(fs);
1793 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
1794 tlv = kctl->tlv.p;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01001795 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) {
1796 int step = tlv[3];
1797 step &= ~TLV_DB_SCALE_MUTE;
1798 if (!step)
1799 return -1;
Takashi Iwai485e3e0c2013-11-04 15:51:00 +01001800 if (*step_to_check && *step_to_check != step) {
Takashi Iwai6194b992014-06-06 18:12:16 +02001801 codec_err(codec, "Mismatching dB step for vmaster slave (%d!=%d)\n",
Takashi Iwai4e76a882014-02-25 12:21:03 +01001802- *step_to_check, step);
Takashi Iwai485e3e0c2013-11-04 15:51:00 +01001803 return -1;
1804 }
1805 *step_to_check = step;
Takashi Iwaia4e7a122013-11-04 15:44:09 +01001806 val = -tlv[2] / step;
1807 }
Takashi Iwai18478e82012-03-09 17:51:10 +01001808 return val;
1809}
1810
1811/* call kctl->put with the given value(s) */
1812static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
1813{
1814 struct snd_ctl_elem_value *ucontrol;
1815 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
1816 if (!ucontrol)
1817 return -ENOMEM;
1818 ucontrol->value.integer.value[0] = val;
1819 ucontrol->value.integer.value[1] = val;
1820 kctl->put(kctl, ucontrol);
1821 kfree(ucontrol);
1822 return 0;
1823}
1824
1825/* initialize the slave volume with 0dB */
Takashi Iwai6194b992014-06-06 18:12:16 +02001826static int init_slave_0dB(struct hda_codec *codec,
1827 void *data, struct snd_kcontrol *slave)
Takashi Iwai18478e82012-03-09 17:51:10 +01001828{
Takashi Iwai6194b992014-06-06 18:12:16 +02001829 int offset = get_kctl_0dB_offset(codec, slave, data);
Takashi Iwai18478e82012-03-09 17:51:10 +01001830 if (offset > 0)
1831 put_kctl_with_value(slave, offset);
1832 return 0;
1833}
1834
1835/* unmute the slave */
Takashi Iwai6194b992014-06-06 18:12:16 +02001836static int init_slave_unmute(struct hda_codec *codec,
1837 void *data, struct snd_kcontrol *slave)
Takashi Iwai18478e82012-03-09 17:51:10 +01001838{
1839 return put_kctl_with_value(slave, 1);
1840}
1841
Takashi Iwaie8750942014-06-30 14:02:39 +02001842static int add_slave(struct hda_codec *codec,
1843 void *data, struct snd_kcontrol *slave)
1844{
1845 return snd_ctl_add_slave(data, slave);
1846}
1847
Takashi Iwaid5191e52009-11-16 14:58:17 +01001848/**
Takashi Iwai95a962c2014-10-29 16:03:58 +01001849 * __snd_hda_add_vmaster - create a virtual master control and add slaves
Takashi Iwaid5191e52009-11-16 14:58:17 +01001850 * @codec: HD-audio codec
1851 * @name: vmaster control name
1852 * @tlv: TLV data (optional)
1853 * @slaves: slave control names (optional)
Takashi Iwai9322ca52012-02-03 14:28:01 +01001854 * @suffix: suffix string to each slave name (optional)
Takashi Iwai18478e82012-03-09 17:51:10 +01001855 * @init_slave_vol: initialize slaves to unmute/0dB
Takashi Iwai29e58532012-03-12 12:25:03 +01001856 * @ctl_ret: store the vmaster kcontrol in return
Takashi Iwaid5191e52009-11-16 14:58:17 +01001857 *
1858 * Create a virtual master control with the given name. The TLV data
1859 * must be either NULL or a valid data.
1860 *
1861 * @slaves is a NULL-terminated array of strings, each of which is a
1862 * slave control name. All controls with these names are assigned to
1863 * the new virtual master control.
1864 *
1865 * This function returns zero if successful or a negative error code.
1866 */
Takashi Iwai18478e82012-03-09 17:51:10 +01001867int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
Takashi Iwai9322ca52012-02-03 14:28:01 +01001868 unsigned int *tlv, const char * const *slaves,
Takashi Iwai29e58532012-03-12 12:25:03 +01001869 const char *suffix, bool init_slave_vol,
1870 struct snd_kcontrol **ctl_ret)
Takashi Iwai2134ea42008-01-10 16:53:55 +01001871{
1872 struct snd_kcontrol *kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01001873 int err;
1874
Takashi Iwai29e58532012-03-12 12:25:03 +01001875 if (ctl_ret)
1876 *ctl_ret = NULL;
1877
Takashi Iwai9322ca52012-02-03 14:28:01 +01001878 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01001879 if (err != 1) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001880 codec_dbg(codec, "No slave found for %s\n", name);
Takashi Iwai2f085542008-02-22 18:43:50 +01001881 return 0;
1882 }
Takashi Iwai2134ea42008-01-10 16:53:55 +01001883 kctl = snd_ctl_make_virtual_master(name, tlv);
1884 if (!kctl)
1885 return -ENOMEM;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01001886 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001887 if (err < 0)
1888 return err;
Norberto Lopes28aedaf2010-02-28 20:16:53 +01001889
Takashi Iwaie8750942014-06-30 14:02:39 +02001890 err = map_slaves(codec, slaves, suffix, add_slave, kctl);
Takashi Iwaiaeb4b882011-11-10 12:28:38 +01001891 if (err < 0)
1892 return err;
Takashi Iwai18478e82012-03-09 17:51:10 +01001893
1894 /* init with master mute & zero volume */
1895 put_kctl_with_value(kctl, 0);
Takashi Iwai485e3e0c2013-11-04 15:51:00 +01001896 if (init_slave_vol) {
1897 int step = 0;
Takashi Iwai18478e82012-03-09 17:51:10 +01001898 map_slaves(codec, slaves, suffix,
Takashi Iwai485e3e0c2013-11-04 15:51:00 +01001899 tlv ? init_slave_0dB : init_slave_unmute, &step);
1900 }
Takashi Iwai18478e82012-03-09 17:51:10 +01001901
Takashi Iwai29e58532012-03-12 12:25:03 +01001902 if (ctl_ret)
1903 *ctl_ret = kctl;
Takashi Iwai2134ea42008-01-10 16:53:55 +01001904 return 0;
1905}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001906EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster);
Takashi Iwai2134ea42008-01-10 16:53:55 +01001907
Takashi Iwaid2f344b2012-03-12 16:59:58 +01001908/*
1909 * mute-LED control using vmaster
1910 */
1911static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
1912 struct snd_ctl_elem_info *uinfo)
1913{
1914 static const char * const texts[] = {
David Henningssonc86c2d42013-01-03 14:12:29 +01001915 "On", "Off", "Follow Master"
Takashi Iwaid2f344b2012-03-12 16:59:58 +01001916 };
Takashi Iwaid2f344b2012-03-12 16:59:58 +01001917
Takashi Iwai3ff72212014-10-20 18:17:28 +02001918 return snd_ctl_enum_info(uinfo, 1, 3, texts);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01001919}
1920
1921static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
1922 struct snd_ctl_elem_value *ucontrol)
1923{
1924 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
1925 ucontrol->value.enumerated.item[0] = hook->mute_mode;
1926 return 0;
1927}
1928
1929static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
1930 struct snd_ctl_elem_value *ucontrol)
1931{
1932 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
1933 unsigned int old_mode = hook->mute_mode;
1934
1935 hook->mute_mode = ucontrol->value.enumerated.item[0];
1936 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
1937 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
1938 if (old_mode == hook->mute_mode)
1939 return 0;
1940 snd_hda_sync_vmaster_hook(hook);
1941 return 1;
1942}
1943
1944static struct snd_kcontrol_new vmaster_mute_mode = {
1945 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1946 .name = "Mute-LED Mode",
1947 .info = vmaster_mute_mode_info,
1948 .get = vmaster_mute_mode_get,
1949 .put = vmaster_mute_mode_put,
1950};
1951
Takashi Iwaiee52e562015-04-27 10:36:11 +02001952/* meta hook to call each driver's vmaster hook */
1953static void vmaster_hook(void *private_data, int enabled)
1954{
1955 struct hda_vmaster_mute_hook *hook = private_data;
1956
1957 if (hook->mute_mode != HDA_VMUTE_FOLLOW_MASTER)
1958 enabled = hook->mute_mode;
1959 hook->hook(hook->codec, enabled);
1960}
1961
Takashi Iwai95a962c2014-10-29 16:03:58 +01001962/**
1963 * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED
1964 * @codec: the HDA codec
1965 * @hook: the vmaster hook object
1966 * @expose_enum_ctl: flag to create an enum ctl
1967 *
1968 * Add a mute-LED hook with the given vmaster switch kctl.
1969 * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically
1970 * created and associated with the given hook.
Takashi Iwaid2f344b2012-03-12 16:59:58 +01001971 */
1972int snd_hda_add_vmaster_hook(struct hda_codec *codec,
Takashi Iwaif29735cb2012-03-13 07:55:10 +01001973 struct hda_vmaster_mute_hook *hook,
1974 bool expose_enum_ctl)
Takashi Iwaid2f344b2012-03-12 16:59:58 +01001975{
1976 struct snd_kcontrol *kctl;
1977
1978 if (!hook->hook || !hook->sw_kctl)
1979 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01001980 hook->codec = codec;
1981 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
Takashi Iwaiee52e562015-04-27 10:36:11 +02001982 snd_ctl_add_vmaster_hook(hook->sw_kctl, vmaster_hook, hook);
Takashi Iwaif29735cb2012-03-13 07:55:10 +01001983 if (!expose_enum_ctl)
1984 return 0;
Takashi Iwaid2f344b2012-03-12 16:59:58 +01001985 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
1986 if (!kctl)
1987 return -ENOMEM;
1988 return snd_hda_ctl_add(codec, 0, kctl);
1989}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001990EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01001991
Takashi Iwai95a962c2014-10-29 16:03:58 +01001992/**
1993 * snd_hda_sync_vmaster_hook - Sync vmaster hook
1994 * @hook: the vmaster hook
1995 *
1996 * Call the hook with the current value for synchronization.
1997 * Should be called in init callback.
Takashi Iwaid2f344b2012-03-12 16:59:58 +01001998 */
1999void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2000{
2001 if (!hook->hook || !hook->codec)
2002 return;
Takashi Iwai594813f2013-04-17 18:16:05 +02002003 /* don't call vmaster hook in the destructor since it might have
2004 * been already destroyed
2005 */
2006 if (hook->codec->bus->shutdown)
2007 return;
Takashi Iwaiee52e562015-04-27 10:36:11 +02002008 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002009}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002010EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook);
Takashi Iwaid2f344b2012-03-12 16:59:58 +01002011
2012
Takashi Iwaid5191e52009-11-16 14:58:17 +01002013/**
2014 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002015 * @kcontrol: referred ctl element
2016 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002017 *
2018 * The control element is supposed to have the private_value field
2019 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2020 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002021int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2022 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023{
2024 int chs = get_amp_channels(kcontrol);
2025
2026 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2027 uinfo->count = chs == 3 ? 2 : 1;
2028 uinfo->value.integer.min = 0;
2029 uinfo->value.integer.max = 1;
2030 return 0;
2031}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002032EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
Takashi Iwaid5191e52009-11-16 14:58:17 +01002034/**
2035 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002036 * @kcontrol: ctl element
2037 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002038 *
2039 * The control element is supposed to have the private_value field
2040 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2041 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002042int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2043 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044{
2045 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2046 hda_nid_t nid = get_amp_nid(kcontrol);
2047 int chs = get_amp_channels(kcontrol);
2048 int dir = get_amp_direction(kcontrol);
2049 int idx = get_amp_index(kcontrol);
2050 long *valp = ucontrol->value.integer.value;
2051
2052 if (chs & 1)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002053 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002054 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 if (chs & 2)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002056 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
Takashi Iwai47fd8302007-08-10 17:11:07 +02002057 HDA_AMP_MUTE) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 return 0;
2059}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002060EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
Takashi Iwaid5191e52009-11-16 14:58:17 +01002062/**
2063 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002064 * @kcontrol: ctl element
2065 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002066 *
2067 * The control element is supposed to have the private_value field
2068 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2069 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002070int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2071 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072{
2073 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2074 hda_nid_t nid = get_amp_nid(kcontrol);
2075 int chs = get_amp_channels(kcontrol);
2076 int dir = get_amp_direction(kcontrol);
2077 int idx = get_amp_index(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 long *valp = ucontrol->value.integer.value;
2079 int change = 0;
2080
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002081 if (chs & 1) {
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01002082 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
2083 HDA_AMP_MUTE,
2084 *valp ? 0 : HDA_AMP_MUTE);
Nicolas Grazianob9f5a892005-07-29 12:17:20 +02002085 valp++;
2086 }
Takashi Iwai4a19fae2005-06-08 14:43:58 +02002087 if (chs & 2)
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01002088 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
2089 HDA_AMP_MUTE,
2090 *valp ? 0 : HDA_AMP_MUTE);
Takashi Iwai9e5341b2010-09-21 09:57:06 +02002091 hda_call_check_power_status(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 return change;
2093}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002094EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095
2096/*
Takashi Iwai985be542005-11-02 18:26:49 +01002097 * bound volume controls
2098 *
2099 * bind multiple volumes (# indices, from 0)
2100 */
2101
2102#define AMP_VAL_IDX_SHIFT 19
2103#define AMP_VAL_IDX_MASK (0x0f<<19)
2104
Takashi Iwaid5191e52009-11-16 14:58:17 +01002105/**
2106 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002107 * @kcontrol: ctl element
2108 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002109 *
2110 * The control element is supposed to have the private_value field
2111 * set up via HDA_BIND_MUTE*() macros.
2112 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002113int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2114 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002115{
2116 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2117 unsigned long pval;
2118 int err;
2119
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002120 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002121 pval = kcontrol->private_value;
2122 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2123 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2124 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002125 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002126 return err;
2127}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002128EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get);
Takashi Iwai985be542005-11-02 18:26:49 +01002129
Takashi Iwaid5191e52009-11-16 14:58:17 +01002130/**
2131 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002132 * @kcontrol: ctl element
2133 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002134 *
2135 * The control element is supposed to have the private_value field
2136 * set up via HDA_BIND_MUTE*() macros.
2137 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02002138int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2139 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai985be542005-11-02 18:26:49 +01002140{
2141 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2142 unsigned long pval;
2143 int i, indices, err = 0, change = 0;
2144
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002145 mutex_lock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002146 pval = kcontrol->private_value;
2147 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2148 for (i = 0; i < indices; i++) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002149 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2150 (i << AMP_VAL_IDX_SHIFT);
Takashi Iwai985be542005-11-02 18:26:49 +01002151 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2152 if (err < 0)
2153 break;
2154 change |= err;
2155 }
2156 kcontrol->private_value = pval;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002157 mutex_unlock(&codec->control_mutex);
Takashi Iwai985be542005-11-02 18:26:49 +01002158 return err < 0 ? err : change;
2159}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002160EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put);
Takashi Iwai985be542005-11-02 18:26:49 +01002161
Takashi Iwaid5191e52009-11-16 14:58:17 +01002162/**
2163 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002164 * @kcontrol: referred ctl element
2165 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002166 *
2167 * The control element is supposed to have the private_value field
2168 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
Takashi Iwai532d5382007-07-27 19:02:40 +02002169 */
2170int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2171 struct snd_ctl_elem_info *uinfo)
2172{
2173 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2174 struct hda_bind_ctls *c;
2175 int err;
2176
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002177 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002178 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002179 kcontrol->private_value = *c->values;
2180 err = c->ops->info(kcontrol, uinfo);
2181 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002182 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002183 return err;
2184}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002185EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info);
Takashi Iwai532d5382007-07-27 19:02:40 +02002186
Takashi Iwaid5191e52009-11-16 14:58:17 +01002187/**
2188 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002189 * @kcontrol: ctl element
2190 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002191 *
2192 * The control element is supposed to have the private_value field
2193 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2194 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002195int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2196 struct snd_ctl_elem_value *ucontrol)
2197{
2198 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2199 struct hda_bind_ctls *c;
2200 int err;
2201
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002202 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002203 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002204 kcontrol->private_value = *c->values;
2205 err = c->ops->get(kcontrol, ucontrol);
2206 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002207 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002208 return err;
2209}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002210EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get);
Takashi Iwai532d5382007-07-27 19:02:40 +02002211
Takashi Iwaid5191e52009-11-16 14:58:17 +01002212/**
2213 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002214 * @kcontrol: ctl element
2215 * @ucontrol: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002216 *
2217 * The control element is supposed to have the private_value field
2218 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2219 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002220int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2221 struct snd_ctl_elem_value *ucontrol)
2222{
2223 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2224 struct hda_bind_ctls *c;
2225 unsigned long *vals;
2226 int err = 0, change = 0;
2227
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002228 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002229 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002230 for (vals = c->values; *vals; vals++) {
2231 kcontrol->private_value = *vals;
2232 err = c->ops->put(kcontrol, ucontrol);
2233 if (err < 0)
2234 break;
2235 change |= err;
2236 }
2237 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002238 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002239 return err < 0 ? err : change;
2240}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002241EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put);
Takashi Iwai532d5382007-07-27 19:02:40 +02002242
Takashi Iwaid5191e52009-11-16 14:58:17 +01002243/**
2244 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
Takashi Iwaia11e9b12014-10-29 15:06:01 +01002245 * @kcontrol: ctl element
2246 * @op_flag: operation flag
2247 * @size: byte size of input TLV
2248 * @tlv: TLV data
Takashi Iwaid5191e52009-11-16 14:58:17 +01002249 *
2250 * The control element is supposed to have the private_value field
2251 * set up via HDA_BIND_VOL() macro.
2252 */
Takashi Iwai532d5382007-07-27 19:02:40 +02002253int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2254 unsigned int size, unsigned int __user *tlv)
2255{
2256 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2257 struct hda_bind_ctls *c;
2258 int err;
2259
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002260 mutex_lock(&codec->control_mutex);
Serge A. Suchkov14c65f92008-02-22 18:43:16 +01002261 c = (struct hda_bind_ctls *)kcontrol->private_value;
Takashi Iwai532d5382007-07-27 19:02:40 +02002262 kcontrol->private_value = *c->values;
2263 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2264 kcontrol->private_value = (long)c;
Wu Fengguang5a9e02e2009-01-09 16:45:24 +08002265 mutex_unlock(&codec->control_mutex);
Takashi Iwai532d5382007-07-27 19:02:40 +02002266 return err;
2267}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002268EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv);
Takashi Iwai532d5382007-07-27 19:02:40 +02002269
2270struct hda_ctl_ops snd_hda_bind_vol = {
2271 .info = snd_hda_mixer_amp_volume_info,
2272 .get = snd_hda_mixer_amp_volume_get,
2273 .put = snd_hda_mixer_amp_volume_put,
2274 .tlv = snd_hda_mixer_amp_tlv
2275};
Takashi Iwai2698ea92013-12-18 07:45:52 +01002276EXPORT_SYMBOL_GPL(snd_hda_bind_vol);
Takashi Iwai532d5382007-07-27 19:02:40 +02002277
2278struct hda_ctl_ops snd_hda_bind_sw = {
2279 .info = snd_hda_mixer_amp_switch_info,
2280 .get = snd_hda_mixer_amp_switch_get,
2281 .put = snd_hda_mixer_amp_switch_put,
2282 .tlv = snd_hda_mixer_amp_tlv
2283};
Takashi Iwai2698ea92013-12-18 07:45:52 +01002284EXPORT_SYMBOL_GPL(snd_hda_bind_sw);
Takashi Iwai532d5382007-07-27 19:02:40 +02002285
2286/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 * SPDIF out controls
2288 */
2289
Takashi Iwai0ba21762007-04-16 11:29:14 +02002290static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2291 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292{
2293 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2294 uinfo->count = 1;
2295 return 0;
2296}
2297
Takashi Iwai0ba21762007-04-16 11:29:14 +02002298static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2299 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300{
2301 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2302 IEC958_AES0_NONAUDIO |
2303 IEC958_AES0_CON_EMPHASIS_5015 |
2304 IEC958_AES0_CON_NOT_COPYRIGHT;
2305 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2306 IEC958_AES1_CON_ORIGINAL;
2307 return 0;
2308}
2309
Takashi Iwai0ba21762007-04-16 11:29:14 +02002310static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2311 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312{
2313 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2314 IEC958_AES0_NONAUDIO |
2315 IEC958_AES0_PRO_EMPHASIS_5015;
2316 return 0;
2317}
2318
Takashi Iwai0ba21762007-04-16 11:29:14 +02002319static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2320 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321{
2322 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06002323 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002324 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002326 mutex_lock(&codec->spdif_mutex);
2327 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06002328 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2329 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2330 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2331 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002332 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333
2334 return 0;
2335}
2336
2337/* convert from SPDIF status bits to HDA SPDIF bits
2338 * bit 0 (DigEn) is always set zero (to be filled later)
2339 */
2340static unsigned short convert_from_spdif_status(unsigned int sbits)
2341{
2342 unsigned short val = 0;
2343
2344 if (sbits & IEC958_AES0_PROFESSIONAL)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002345 val |= AC_DIG1_PROFESSIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 if (sbits & IEC958_AES0_NONAUDIO)
Takashi Iwai0ba21762007-04-16 11:29:14 +02002347 val |= AC_DIG1_NONAUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002349 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2350 IEC958_AES0_PRO_EMPHASIS_5015)
2351 val |= AC_DIG1_EMPHASIS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002353 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2354 IEC958_AES0_CON_EMPHASIS_5015)
2355 val |= AC_DIG1_EMPHASIS;
2356 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2357 val |= AC_DIG1_COPYRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
Takashi Iwai0ba21762007-04-16 11:29:14 +02002359 val |= AC_DIG1_LEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2361 }
2362 return val;
2363}
2364
2365/* convert to SPDIF status bits from HDA SPDIF bits
2366 */
2367static unsigned int convert_to_spdif_status(unsigned short val)
2368{
2369 unsigned int sbits = 0;
2370
Takashi Iwai0ba21762007-04-16 11:29:14 +02002371 if (val & AC_DIG1_NONAUDIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 sbits |= IEC958_AES0_NONAUDIO;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002373 if (val & AC_DIG1_PROFESSIONAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 sbits |= IEC958_AES0_PROFESSIONAL;
2375 if (sbits & IEC958_AES0_PROFESSIONAL) {
Takashi Iwaia686fd12013-03-20 15:42:00 +01002376 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2378 } else {
Takashi Iwai0ba21762007-04-16 11:29:14 +02002379 if (val & AC_DIG1_EMPHASIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002381 if (!(val & AC_DIG1_COPYRIGHT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
Takashi Iwai0ba21762007-04-16 11:29:14 +02002383 if (val & AC_DIG1_LEVEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2385 sbits |= val & (0x7f << 8);
2386 }
2387 return sbits;
2388}
2389
Takashi Iwai2f728532008-09-25 16:32:41 +02002390/* set digital convert verbs both for the given NID and its slaves */
2391static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwaia551d912015-02-26 12:34:49 +01002392 int mask, int val)
Takashi Iwai2f728532008-09-25 16:32:41 +02002393{
Takashi Iwaidda14412011-05-02 11:29:30 +02002394 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02002395
Takashi Iwaia551d912015-02-26 12:34:49 +01002396 snd_hdac_regmap_update(&codec->core, nid, AC_VERB_SET_DIGI_CONVERT_1,
2397 mask, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002398 d = codec->slave_dig_outs;
2399 if (!d)
2400 return;
2401 for (; *d; d++)
Takashi Iwai7d4b5e92015-04-16 15:14:53 +02002402 snd_hdac_regmap_update(&codec->core, *d,
Takashi Iwaia551d912015-02-26 12:34:49 +01002403 AC_VERB_SET_DIGI_CONVERT_1, mask, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002404}
2405
2406static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2407 int dig1, int dig2)
2408{
Takashi Iwaia551d912015-02-26 12:34:49 +01002409 unsigned int mask = 0;
2410 unsigned int val = 0;
2411
2412 if (dig1 != -1) {
2413 mask |= 0xff;
2414 val = dig1;
2415 }
2416 if (dig2 != -1) {
2417 mask |= 0xff00;
2418 val |= dig2 << 8;
2419 }
2420 set_dig_out(codec, nid, mask, val);
Takashi Iwai2f728532008-09-25 16:32:41 +02002421}
2422
Takashi Iwai0ba21762007-04-16 11:29:14 +02002423static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2424 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425{
2426 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06002427 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002428 struct hda_spdif_out *spdif;
2429 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 unsigned short val;
2431 int change;
2432
Ingo Molnar62932df2006-01-16 16:34:20 +01002433 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002434 spdif = snd_array_elem(&codec->spdif_out, idx);
2435 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06002436 spdif->status = ucontrol->value.iec958.status[0] |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2438 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2439 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
Stephen Warren7c935972011-06-01 11:14:17 -06002440 val = convert_from_spdif_status(spdif->status);
2441 val |= spdif->ctls & 1;
2442 change = spdif->ctls != val;
2443 spdif->ctls = val;
Stephen Warren74b654c2011-06-01 11:14:18 -06002444 if (change && nid != (u16)-1)
Takashi Iwai2f728532008-09-25 16:32:41 +02002445 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
Ingo Molnar62932df2006-01-16 16:34:20 +01002446 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 return change;
2448}
2449
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002450#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451
Takashi Iwai0ba21762007-04-16 11:29:14 +02002452static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2453 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454{
2455 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06002456 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002457 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002459 mutex_lock(&codec->spdif_mutex);
2460 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren7c935972011-06-01 11:14:17 -06002461 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002462 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 return 0;
2464}
2465
Stephen Warren74b654c2011-06-01 11:14:18 -06002466static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
2467 int dig1, int dig2)
2468{
2469 set_dig_out_convert(codec, nid, dig1, dig2);
2470 /* unmute amp switch (if any) */
2471 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
2472 (dig1 & AC_DIG1_ENABLE))
2473 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2474 HDA_AMP_MUTE, 0);
2475}
2476
Takashi Iwai0ba21762007-04-16 11:29:14 +02002477static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2478 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479{
2480 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Stephen Warren7c935972011-06-01 11:14:17 -06002481 int idx = kcontrol->private_value;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002482 struct hda_spdif_out *spdif;
2483 hda_nid_t nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 unsigned short val;
2485 int change;
2486
Ingo Molnar62932df2006-01-16 16:34:20 +01002487 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002488 spdif = snd_array_elem(&codec->spdif_out, idx);
2489 nid = spdif->nid;
Stephen Warren7c935972011-06-01 11:14:17 -06002490 val = spdif->ctls & ~AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 if (ucontrol->value.integer.value[0])
Takashi Iwai0ba21762007-04-16 11:29:14 +02002492 val |= AC_DIG1_ENABLE;
Stephen Warren7c935972011-06-01 11:14:17 -06002493 change = spdif->ctls != val;
Stephen Warren74b654c2011-06-01 11:14:18 -06002494 spdif->ctls = val;
2495 if (change && nid != (u16)-1)
2496 set_spdif_ctls(codec, nid, val & 0xff, -1);
Ingo Molnar62932df2006-01-16 16:34:20 +01002497 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 return change;
2499}
2500
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002501static struct snd_kcontrol_new dig_mixes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 {
2503 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2504 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002505 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 .info = snd_hda_spdif_mask_info,
2507 .get = snd_hda_spdif_cmask_get,
2508 },
2509 {
2510 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2511 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002512 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 .info = snd_hda_spdif_mask_info,
2514 .get = snd_hda_spdif_pmask_get,
2515 },
2516 {
2517 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002518 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 .info = snd_hda_spdif_mask_info,
2520 .get = snd_hda_spdif_default_get,
2521 .put = snd_hda_spdif_default_put,
2522 },
2523 {
2524 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002525 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 .info = snd_hda_spdif_out_switch_info,
2527 .get = snd_hda_spdif_out_switch_get,
2528 .put = snd_hda_spdif_out_switch_put,
2529 },
2530 { } /* end */
2531};
2532
2533/**
Takashi Iwaidcda5802012-10-12 17:24:51 +02002534 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 * @codec: the HDA codec
Takashi Iwaidcda5802012-10-12 17:24:51 +02002536 * @associated_nid: NID that new ctls associated with
2537 * @cvt_nid: converter NID
2538 * @type: HDA_PCM_TYPE_*
2539 * Creates controls related with the digital output.
2540 * Called from each patch supporting the digital out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 *
2542 * Returns 0 if successful, or a negative error code.
2543 */
Takashi Iwaidcda5802012-10-12 17:24:51 +02002544int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
2545 hda_nid_t associated_nid,
2546 hda_nid_t cvt_nid,
2547 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548{
2549 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002550 struct snd_kcontrol *kctl;
2551 struct snd_kcontrol_new *dig_mix;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002552 int idx = 0;
Takashi Iwaia551d912015-02-26 12:34:49 +01002553 int val = 0;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002554 const int spdif_index = 16;
Stephen Warren7c935972011-06-01 11:14:17 -06002555 struct hda_spdif_out *spdif;
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002556 struct hda_bus *bus = codec->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002558 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02002559 type == HDA_PCM_TYPE_SPDIF) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002560 idx = spdif_index;
2561 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
Takashi Iwaidcda5802012-10-12 17:24:51 +02002562 type == HDA_PCM_TYPE_HDMI) {
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002563 /* suppose a single SPDIF device */
2564 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2565 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
2566 if (!kctl)
2567 break;
2568 kctl->id.index = spdif_index;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002569 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002570 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002571 }
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002572 if (!bus->primary_dig_out_type)
2573 bus->primary_dig_out_type = type;
Takashi Iwaidcda5802012-10-12 17:24:51 +02002574
Takashi Iwaiea9b43a2013-02-12 17:02:41 +01002575 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
Takashi Iwai1afe2062010-12-23 10:17:52 +01002576 if (idx < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002577 codec_err(codec, "too many IEC958 outputs\n");
Takashi Iwai09f99702008-02-04 12:31:13 +01002578 return -EBUSY;
2579 }
Stephen Warren7c935972011-06-01 11:14:17 -06002580 spdif = snd_array_new(&codec->spdif_out);
Mengdong Lin25336e82013-03-07 14:10:25 -05002581 if (!spdif)
2582 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2584 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaib91f0802008-11-04 08:43:08 +01002585 if (!kctl)
2586 return -ENOMEM;
Takashi Iwai09f99702008-02-04 12:31:13 +01002587 kctl->id.index = idx;
Stephen Warren7c935972011-06-01 11:14:17 -06002588 kctl->private_value = codec->spdif_out.used - 1;
Stephen Warren74b654c2011-06-01 11:14:18 -06002589 err = snd_hda_ctl_add(codec, associated_nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002590 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 return err;
2592 }
Stephen Warren74b654c2011-06-01 11:14:18 -06002593 spdif->nid = cvt_nid;
Takashi Iwaia551d912015-02-26 12:34:49 +01002594 snd_hdac_regmap_read(&codec->core, cvt_nid,
2595 AC_VERB_GET_DIGI_CONVERT_1, &val);
2596 spdif->ctls = val;
Stephen Warren7c935972011-06-01 11:14:17 -06002597 spdif->status = convert_to_spdif_status(spdif->ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 return 0;
2599}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002600EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601
Takashi Iwai95a962c2014-10-29 16:03:58 +01002602/**
2603 * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID
2604 * @codec: the HDA codec
2605 * @nid: widget NID
2606 *
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002607 * call within spdif_mutex lock
2608 */
Stephen Warren7c935972011-06-01 11:14:17 -06002609struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
2610 hda_nid_t nid)
2611{
2612 int i;
2613 for (i = 0; i < codec->spdif_out.used; i++) {
2614 struct hda_spdif_out *spdif =
2615 snd_array_elem(&codec->spdif_out, i);
2616 if (spdif->nid == nid)
2617 return spdif;
2618 }
2619 return NULL;
2620}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002621EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid);
Stephen Warren7c935972011-06-01 11:14:17 -06002622
Takashi Iwai95a962c2014-10-29 16:03:58 +01002623/**
2624 * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl
2625 * @codec: the HDA codec
2626 * @idx: the SPDIF ctl index
2627 *
2628 * Unassign the widget from the given SPDIF control.
2629 */
Stephen Warren74b654c2011-06-01 11:14:18 -06002630void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
2631{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002632 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06002633
2634 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002635 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06002636 spdif->nid = (u16)-1;
2637 mutex_unlock(&codec->spdif_mutex);
2638}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002639EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign);
Stephen Warren74b654c2011-06-01 11:14:18 -06002640
Takashi Iwai95a962c2014-10-29 16:03:58 +01002641/**
2642 * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID
2643 * @codec: the HDA codec
2644 * @idx: the SPDIF ctl idx
2645 * @nid: widget NID
2646 *
2647 * Assign the widget to the SPDIF control with the given index.
2648 */
Stephen Warren74b654c2011-06-01 11:14:18 -06002649void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
2650{
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002651 struct hda_spdif_out *spdif;
Stephen Warren74b654c2011-06-01 11:14:18 -06002652 unsigned short val;
2653
2654 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002655 spdif = snd_array_elem(&codec->spdif_out, idx);
Stephen Warren74b654c2011-06-01 11:14:18 -06002656 if (spdif->nid != nid) {
2657 spdif->nid = nid;
2658 val = spdif->ctls;
2659 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
2660 }
2661 mutex_unlock(&codec->spdif_mutex);
2662}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002663EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign);
Stephen Warren74b654c2011-06-01 11:14:18 -06002664
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665/*
Takashi Iwai9a081602008-02-12 18:37:26 +01002666 * SPDIF sharing with analog output
2667 */
2668static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2669 struct snd_ctl_elem_value *ucontrol)
2670{
2671 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2672 ucontrol->value.integer.value[0] = mout->share_spdif;
2673 return 0;
2674}
2675
2676static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2677 struct snd_ctl_elem_value *ucontrol)
2678{
2679 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2680 mout->share_spdif = !!ucontrol->value.integer.value[0];
2681 return 0;
2682}
2683
2684static struct snd_kcontrol_new spdif_share_sw = {
2685 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2686 .name = "IEC958 Default PCM Playback Switch",
2687 .info = snd_ctl_boolean_mono_info,
2688 .get = spdif_share_sw_get,
2689 .put = spdif_share_sw_put,
2690};
2691
Takashi Iwaid5191e52009-11-16 14:58:17 +01002692/**
2693 * snd_hda_create_spdif_share_sw - create Default PCM switch
2694 * @codec: the HDA codec
2695 * @mout: multi-out instance
2696 */
Takashi Iwai9a081602008-02-12 18:37:26 +01002697int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2698 struct hda_multi_out *mout)
2699{
Mengdong Lin4c7a5482013-03-07 14:11:05 -05002700 struct snd_kcontrol *kctl;
2701
Takashi Iwai9a081602008-02-12 18:37:26 +01002702 if (!mout->dig_out_nid)
2703 return 0;
Mengdong Lin4c7a5482013-03-07 14:11:05 -05002704
2705 kctl = snd_ctl_new1(&spdif_share_sw, mout);
2706 if (!kctl)
2707 return -ENOMEM;
Takashi Iwai9a081602008-02-12 18:37:26 +01002708 /* ATTENTION: here mout is passed as private_data, instead of codec */
Mengdong Lin4c7a5482013-03-07 14:11:05 -05002709 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
Takashi Iwai9a081602008-02-12 18:37:26 +01002710}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002711EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw);
Takashi Iwai9a081602008-02-12 18:37:26 +01002712
2713/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 * SPDIF input
2715 */
2716
2717#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2718
Takashi Iwai0ba21762007-04-16 11:29:14 +02002719static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
2720 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721{
2722 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2723
2724 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
2725 return 0;
2726}
2727
Takashi Iwai0ba21762007-04-16 11:29:14 +02002728static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
2729 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730{
2731 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2732 hda_nid_t nid = kcontrol->private_value;
2733 unsigned int val = !!ucontrol->value.integer.value[0];
2734 int change;
2735
Ingo Molnar62932df2006-01-16 16:34:20 +01002736 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 change = codec->spdif_in_enable != val;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02002738 if (change) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 codec->spdif_in_enable = val;
Takashi Iwaia551d912015-02-26 12:34:49 +01002740 snd_hdac_regmap_write(&codec->core, nid,
2741 AC_VERB_SET_DIGI_CONVERT_1, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 }
Ingo Molnar62932df2006-01-16 16:34:20 +01002743 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 return change;
2745}
2746
Takashi Iwai0ba21762007-04-16 11:29:14 +02002747static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
2748 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749{
2750 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2751 hda_nid_t nid = kcontrol->private_value;
Takashi Iwaia551d912015-02-26 12:34:49 +01002752 unsigned int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 unsigned int sbits;
2754
Takashi Iwaia551d912015-02-26 12:34:49 +01002755 snd_hdac_regmap_read(&codec->core, nid,
2756 AC_VERB_GET_DIGI_CONVERT_1, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 sbits = convert_to_spdif_status(val);
2758 ucontrol->value.iec958.status[0] = sbits;
2759 ucontrol->value.iec958.status[1] = sbits >> 8;
2760 ucontrol->value.iec958.status[2] = sbits >> 16;
2761 ucontrol->value.iec958.status[3] = sbits >> 24;
2762 return 0;
2763}
2764
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002765static struct snd_kcontrol_new dig_in_ctls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 {
2767 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002768 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 .info = snd_hda_spdif_in_switch_info,
2770 .get = snd_hda_spdif_in_switch_get,
2771 .put = snd_hda_spdif_in_switch_put,
2772 },
2773 {
2774 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2775 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Norberto Lopes28aedaf2010-02-28 20:16:53 +01002776 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 .info = snd_hda_spdif_mask_info,
2778 .get = snd_hda_spdif_in_status_get,
2779 },
2780 { } /* end */
2781};
2782
2783/**
2784 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
2785 * @codec: the HDA codec
2786 * @nid: audio in widget NID
2787 *
2788 * Creates controls related with the SPDIF input.
2789 * Called from each patch supporting the SPDIF in.
2790 *
2791 * Returns 0 if successful, or a negative error code.
2792 */
Takashi Iwai12f288b2007-08-02 15:51:59 +02002793int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794{
2795 int err;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01002796 struct snd_kcontrol *kctl;
2797 struct snd_kcontrol_new *dig_mix;
Takashi Iwai09f99702008-02-04 12:31:13 +01002798 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799
Takashi Iwaidcda5802012-10-12 17:24:51 +02002800 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01002801 if (idx < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002802 codec_err(codec, "too many IEC958 inputs\n");
Takashi Iwai09f99702008-02-04 12:31:13 +01002803 return -EBUSY;
2804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
2806 kctl = snd_ctl_new1(dig_mix, codec);
Takashi Iwaic8dcdf82009-02-06 16:21:20 +01002807 if (!kctl)
2808 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 kctl->private_value = nid;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01002810 err = snd_hda_ctl_add(codec, nid, kctl);
Takashi Iwai0ba21762007-04-16 11:29:14 +02002811 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 return err;
2813 }
Takashi Iwai0ba21762007-04-16 11:29:14 +02002814 codec->spdif_in_enable =
Andrew Paprocki3982d172007-12-19 12:13:44 +01002815 snd_hda_codec_read(codec, nid, 0,
2816 AC_VERB_GET_DIGI_CONVERT_1, 0) &
Takashi Iwai0ba21762007-04-16 11:29:14 +02002817 AC_DIG1_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 return 0;
2819}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002820EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821
Takashi Iwai95a962c2014-10-29 16:03:58 +01002822/**
2823 * snd_hda_codec_set_power_to_all - Set the power state to all widgets
2824 * @codec: the HDA codec
2825 * @fg: function group (not used now)
2826 * @power_state: the power state to set (AC_PWRST_*)
2827 *
2828 * Set the given power state to all widgets that have the power control.
2829 * If the codec has power_filter set, it evaluates the power state and
2830 * filter out if it's unchanged as D3.
2831 */
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002832void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
Takashi Iwai9419ab62013-01-24 17:23:35 +01002833 unsigned int power_state)
Takashi Iwai54d17402005-11-21 16:33:22 +01002834{
Takashi Iwai7639a062015-03-03 10:07:24 +01002835 hda_nid_t nid;
Takashi Iwai54d17402005-11-21 16:33:22 +01002836
Takashi Iwai7639a062015-03-03 10:07:24 +01002837 for_each_hda_codec_node(nid, codec) {
Takashi Iwai7eba5c92007-11-14 14:53:42 +01002838 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9419ab62013-01-24 17:23:35 +01002839 unsigned int state = power_state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002840 if (!(wcaps & AC_WCAP_POWER))
2841 continue;
Takashi Iwai9419ab62013-01-24 17:23:35 +01002842 if (codec->power_filter) {
2843 state = codec->power_filter(codec, nid, power_state);
2844 if (state != power_state && power_state == AC_PWRST_D3)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002845 continue;
Takashi Iwai1194b5b2007-10-10 10:04:26 +02002846 }
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002847 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
Takashi Iwai9419ab62013-01-24 17:23:35 +01002848 state);
Takashi Iwai54d17402005-11-21 16:33:22 +01002849 }
Takashi Iwai54d17402005-11-21 16:33:22 +01002850}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002851EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all);
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002852
2853/*
Takashi Iwai432c6412012-08-28 09:59:20 -07002854 * wait until the state is reached, returns the current state
2855 */
2856static unsigned int hda_sync_power_state(struct hda_codec *codec,
2857 hda_nid_t fg,
2858 unsigned int power_state)
2859{
2860 unsigned long end_time = jiffies + msecs_to_jiffies(500);
2861 unsigned int state, actual_state;
2862
2863 for (;;) {
2864 state = snd_hda_codec_read(codec, fg, 0,
2865 AC_VERB_GET_POWER_STATE, 0);
2866 if (state & AC_PWRST_ERROR)
2867 break;
2868 actual_state = (state >> 4) & 0x0f;
2869 if (actual_state == power_state)
2870 break;
2871 if (time_after_eq(jiffies, end_time))
2872 break;
2873 /* wait until the codec reachs to the target state */
2874 msleep(1);
2875 }
2876 return state;
2877}
2878
Takashi Iwai95a962c2014-10-29 16:03:58 +01002879/**
2880 * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD
2881 * @codec: the HDA codec
2882 * @nid: widget NID
2883 * @power_state: power state to evalue
2884 *
2885 * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set.
2886 * This can be used a codec power_filter callback.
2887 */
Takashi Iwaiba615b82013-03-13 14:47:21 +01002888unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
2889 hda_nid_t nid,
2890 unsigned int power_state)
Takashi Iwai9419ab62013-01-24 17:23:35 +01002891{
Takashi Iwai7639a062015-03-03 10:07:24 +01002892 if (nid == codec->core.afg || nid == codec->core.mfg)
Takashi Iwaidfc6e462014-01-13 16:09:57 +01002893 return power_state;
Takashi Iwai9419ab62013-01-24 17:23:35 +01002894 if (power_state == AC_PWRST_D3 &&
2895 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
2896 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
2897 int eapd = snd_hda_codec_read(codec, nid, 0,
2898 AC_VERB_GET_EAPD_BTLENABLE, 0);
2899 if (eapd & 0x02)
2900 return AC_PWRST_D0;
2901 }
2902 return power_state;
2903}
Takashi Iwai2698ea92013-12-18 07:45:52 +01002904EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter);
Takashi Iwai9419ab62013-01-24 17:23:35 +01002905
Takashi Iwai432c6412012-08-28 09:59:20 -07002906/*
Takashi Iwai08fa20a2012-08-31 07:46:56 -07002907 * set power state of the codec, and return the power state
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002908 */
Takashi Iwaid8193872012-08-31 07:54:38 -07002909static unsigned int hda_set_power_state(struct hda_codec *codec,
Takashi Iwai08fa20a2012-08-31 07:46:56 -07002910 unsigned int power_state)
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002911{
Takashi Iwai7639a062015-03-03 10:07:24 +01002912 hda_nid_t fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
Wang Xingchao09617ce2012-06-08 10:26:08 +08002913 int count;
2914 unsigned int state;
Takashi Iwai63e51fd72013-06-06 14:20:19 +02002915 int flags = 0;
Wang Xingchao09617ce2012-06-08 10:26:08 +08002916
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002917 /* this delay seems necessary to avoid click noise at power-down */
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08002918 if (power_state == AC_PWRST_D3) {
Mengdong Lin7f132922013-11-29 01:48:45 -05002919 if (codec->depop_delay < 0)
Takashi Iwai7639a062015-03-03 10:07:24 +01002920 msleep(codec_has_epss(codec) ? 10 : 100);
Mengdong Lin7f132922013-11-29 01:48:45 -05002921 else if (codec->depop_delay > 0)
2922 msleep(codec->depop_delay);
Takashi Iwai63e51fd72013-06-06 14:20:19 +02002923 flags = HDA_RW_NO_RESPONSE_FALLBACK;
Wang Xingchao0f4ccbb2012-06-07 16:51:33 +08002924 }
Wang Xingchao09617ce2012-06-08 10:26:08 +08002925
2926 /* repeat power states setting at most 10 times*/
2927 for (count = 0; count < 10; count++) {
Takashi Iwai432c6412012-08-28 09:59:20 -07002928 if (codec->patch_ops.set_power_state)
2929 codec->patch_ops.set_power_state(codec, fg,
2930 power_state);
2931 else {
Takashi Iwaidfc6e462014-01-13 16:09:57 +01002932 state = power_state;
2933 if (codec->power_filter)
2934 state = codec->power_filter(codec, fg, state);
2935 if (state == power_state || power_state != AC_PWRST_D3)
2936 snd_hda_codec_read(codec, fg, flags,
2937 AC_VERB_SET_POWER_STATE,
2938 state);
Takashi Iwai9419ab62013-01-24 17:23:35 +01002939 snd_hda_codec_set_power_to_all(codec, fg, power_state);
Takashi Iwai432c6412012-08-28 09:59:20 -07002940 }
2941 state = hda_sync_power_state(codec, fg, power_state);
Wang Xingchao09617ce2012-06-08 10:26:08 +08002942 if (!(state & AC_PWRST_ERROR))
2943 break;
2944 }
Mengdong Linb8dfc4622012-08-23 17:32:30 +08002945
Takashi Iwai08fa20a2012-08-31 07:46:56 -07002946 return state;
Takashi Iwai4d7fbdb2011-07-26 10:33:10 +02002947}
Takashi Iwai54d17402005-11-21 16:33:22 +01002948
Takashi Iwaib9c590b2013-01-24 17:27:32 +01002949/* sync power states of all widgets;
2950 * this is called at the end of codec parsing
2951 */
2952static void sync_power_up_states(struct hda_codec *codec)
2953{
Takashi Iwai7639a062015-03-03 10:07:24 +01002954 hda_nid_t nid;
Takashi Iwaib9c590b2013-01-24 17:27:32 +01002955
Takashi Iwaiba615b82013-03-13 14:47:21 +01002956 /* don't care if no filter is used */
2957 if (!codec->power_filter)
Takashi Iwaib9c590b2013-01-24 17:27:32 +01002958 return;
2959
Takashi Iwai7639a062015-03-03 10:07:24 +01002960 for_each_hda_codec_node(nid, codec) {
Takashi Iwaib9c590b2013-01-24 17:27:32 +01002961 unsigned int wcaps = get_wcaps(codec, nid);
Takashi Iwai9040d102013-01-24 17:47:17 +01002962 unsigned int target;
Takashi Iwaib9c590b2013-01-24 17:27:32 +01002963 if (!(wcaps & AC_WCAP_POWER))
2964 continue;
2965 target = codec->power_filter(codec, nid, AC_PWRST_D0);
2966 if (target == AC_PWRST_D0)
2967 continue;
Takashi Iwai9040d102013-01-24 17:47:17 +01002968 if (!snd_hda_check_power_state(codec, nid, target))
Takashi Iwaib9c590b2013-01-24 17:27:32 +01002969 snd_hda_codec_write(codec, nid, 0,
2970 AC_VERB_SET_POWER_STATE, target);
2971 }
2972}
2973
Takashi Iwai648a8d22014-02-25 10:38:13 +01002974#ifdef CONFIG_SND_HDA_RECONFIG
Takashi Iwai11aeff02008-07-30 15:01:46 +02002975/* execute additional init verbs */
2976static void hda_exec_init_verbs(struct hda_codec *codec)
2977{
2978 if (codec->init_verbs.list)
2979 snd_hda_sequence_write(codec, codec->init_verbs.list);
2980}
2981#else
2982static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
2983#endif
2984
Takashi Iwai2a439522011-07-26 09:52:50 +02002985#ifdef CONFIG_PM
Takashi Iwaicc72da72015-02-19 16:00:22 +01002986/* update the power on/off account with the current jiffies */
2987static void update_power_acct(struct hda_codec *codec, bool on)
2988{
2989 unsigned long delta = jiffies - codec->power_jiffies;
2990
2991 if (on)
2992 codec->power_on_acct += delta;
2993 else
2994 codec->power_off_acct += delta;
2995 codec->power_jiffies += delta;
2996}
2997
2998void snd_hda_update_power_acct(struct hda_codec *codec)
2999{
3000 update_power_acct(codec, hda_codec_is_power_on(codec));
3001}
3002
Takashi Iwaicb53c622007-08-10 17:21:45 +02003003/*
3004 * call suspend and power-down; used both from PM and power-save
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003005 * this function returns the power state in the end
Takashi Iwaicb53c622007-08-10 17:21:45 +02003006 */
Takashi Iwaicc72da72015-02-19 16:00:22 +01003007static unsigned int hda_call_codec_suspend(struct hda_codec *codec)
Takashi Iwaicb53c622007-08-10 17:21:45 +02003008{
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003009 unsigned int state;
3010
Takashi Iwai7639a062015-03-03 10:07:24 +01003011 atomic_inc(&codec->core.in_pm);
Takashi Iwai989c3182012-11-19 14:14:58 +01003012
Takashi Iwaicb53c622007-08-10 17:21:45 +02003013 if (codec->patch_ops.suspend)
Takashi Iwai68cb2b52012-07-02 15:20:37 +02003014 codec->patch_ops.suspend(codec);
Takashi Iwaieb541332010-08-06 13:48:11 +02003015 hda_cleanup_all_streams(codec);
Takashi Iwaid8193872012-08-31 07:54:38 -07003016 state = hda_set_power_state(codec, AC_PWRST_D3);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003017 update_power_acct(codec, true);
Takashi Iwai7639a062015-03-03 10:07:24 +01003018 atomic_dec(&codec->core.in_pm);
Takashi Iwai08fa20a2012-08-31 07:46:56 -07003019 return state;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003020}
3021
3022/*
3023 * kick up codec; used both from PM and power-save
3024 */
3025static void hda_call_codec_resume(struct hda_codec *codec)
3026{
Takashi Iwai7639a062015-03-03 10:07:24 +01003027 atomic_inc(&codec->core.in_pm);
Takashi Iwai989c3182012-11-19 14:14:58 +01003028
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01003029 if (codec->core.regmap)
3030 regcache_mark_dirty(codec->core.regmap);
3031
Takashi Iwaicc72da72015-02-19 16:00:22 +01003032 codec->power_jiffies = jiffies;
Takashi Iwaicc72da72015-02-19 16:00:22 +01003033
Takashi Iwaid8193872012-08-31 07:54:38 -07003034 hda_set_power_state(codec, AC_PWRST_D0);
Takashi Iwaiac0547d2010-07-05 16:50:13 +02003035 restore_shutup_pins(codec);
Takashi Iwai11aeff02008-07-30 15:01:46 +02003036 hda_exec_init_verbs(codec);
Takashi Iwai31614bb2013-01-23 15:58:40 +01003037 snd_hda_jack_set_dirty_all(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003038 if (codec->patch_ops.resume)
3039 codec->patch_ops.resume(codec);
3040 else {
Takashi Iwai9d99f312007-08-14 15:15:52 +02003041 if (codec->patch_ops.init)
3042 codec->patch_ops.init(codec);
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01003043 if (codec->core.regmap)
3044 regcache_sync(codec->core.regmap);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003045 }
David Henningsson26a6cb62012-10-09 15:04:21 +02003046
3047 if (codec->jackpoll_interval)
3048 hda_jackpoll_work(&codec->jackpoll_work.work);
Takashi Iwai31614bb2013-01-23 15:58:40 +01003049 else
David Henningsson26a6cb62012-10-09 15:04:21 +02003050 snd_hda_jack_report_sync(codec);
Takashi Iwai7639a062015-03-03 10:07:24 +01003051 atomic_dec(&codec->core.in_pm);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003052}
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003053
Takashi Iwaicc72da72015-02-19 16:00:22 +01003054static int hda_codec_runtime_suspend(struct device *dev)
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003055{
3056 struct hda_codec *codec = dev_to_hda_codec(dev);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003057 struct hda_pcm *pcm;
Takashi Iwaicc72da72015-02-19 16:00:22 +01003058 unsigned int state;
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003059
3060 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003061 list_for_each_entry(pcm, &codec->pcm_list_head, list)
3062 snd_pcm_suspend_all(pcm->pcm);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003063 state = hda_call_codec_suspend(codec);
Takashi Iwai7639a062015-03-03 10:07:24 +01003064 if (codec_has_clkstop(codec) && codec_has_epss(codec) &&
3065 (state & AC_PWRST_CLK_STOP_OK))
3066 snd_hdac_codec_link_down(&codec->core);
Mengdong Lina5e7e072015-04-29 17:43:20 +08003067 snd_hdac_link_power(&codec->core, false);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003068 return 0;
3069}
3070
Takashi Iwaicc72da72015-02-19 16:00:22 +01003071static int hda_codec_runtime_resume(struct device *dev)
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003072{
Takashi Iwai55ed9cd2015-02-19 17:35:32 +01003073 struct hda_codec *codec = dev_to_hda_codec(dev);
3074
Mengdong Lina5e7e072015-04-29 17:43:20 +08003075 snd_hdac_link_power(&codec->core, true);
Takashi Iwai7639a062015-03-03 10:07:24 +01003076 snd_hdac_codec_link_up(&codec->core);
Takashi Iwai55ed9cd2015-02-19 17:35:32 +01003077 hda_call_codec_resume(codec);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003078 pm_runtime_mark_last_busy(dev);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003079 return 0;
3080}
Takashi Iwai2a439522011-07-26 09:52:50 +02003081#endif /* CONFIG_PM */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003082
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003083/* referred in hda_bind.c */
3084const struct dev_pm_ops hda_codec_driver_pm = {
Takashi Iwaicc72da72015-02-19 16:00:22 +01003085 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
3086 pm_runtime_force_resume)
3087 SET_RUNTIME_PM_OPS(hda_codec_runtime_suspend, hda_codec_runtime_resume,
3088 NULL)
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01003089};
Takashi Iwai54d17402005-11-21 16:33:22 +01003090
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003091/*
3092 * add standard channel maps if not specified
3093 */
3094static int add_std_chmaps(struct hda_codec *codec)
3095{
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003096 struct hda_pcm *pcm;
3097 int str, err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003098
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003099 list_for_each_entry(pcm, &codec->pcm_list_head, list) {
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003100 for (str = 0; str < 2; str++) {
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003101 struct hda_pcm_stream *hinfo = &pcm->stream[str];
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003102 struct snd_pcm_chmap *chmap;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003103 const struct snd_pcm_chmap_elem *elem;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003104
Takashi Iwaib25cf302015-08-26 14:21:14 +02003105 if (!pcm->pcm || pcm->own_chmap || !hinfo->substreams)
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003106 continue;
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003107 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003108 err = snd_pcm_add_chmap_ctls(pcm->pcm, str, elem,
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003109 hinfo->channels_max,
3110 0, &chmap);
3111 if (err < 0)
3112 return err;
3113 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
3114 }
3115 }
3116 return 0;
3117}
3118
Takashi Iwaiee81abb2012-11-08 17:12:10 +01003119/* default channel maps for 2.1 speakers;
3120 * since HD-audio supports only stereo, odd number channels are omitted
3121 */
3122const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
3123 { .channels = 2,
3124 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
3125 { .channels = 4,
3126 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
3127 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
3128 { }
3129};
3130EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
3131
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003132int snd_hda_codec_build_controls(struct hda_codec *codec)
3133{
3134 int err = 0;
Takashi Iwai11aeff02008-07-30 15:01:46 +02003135 hda_exec_init_verbs(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003136 /* continue to initialize... */
3137 if (codec->patch_ops.init)
3138 err = codec->patch_ops.init(codec);
3139 if (!err && codec->patch_ops.build_controls)
3140 err = codec->patch_ops.build_controls(codec);
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003141 if (err < 0)
3142 return err;
Takashi Iwai9c9a5172012-07-31 11:35:35 +02003143
3144 /* we create chmaps here instead of build_pcms */
3145 err = add_std_chmaps(codec);
3146 if (err < 0)
3147 return err;
3148
David Henningsson26a6cb62012-10-09 15:04:21 +02003149 if (codec->jackpoll_interval)
3150 hda_jackpoll_work(&codec->jackpoll_work.work);
3151 else
3152 snd_hda_jack_report_sync(codec); /* call at the last init point */
Takashi Iwaib9c590b2013-01-24 17:27:32 +01003153 sync_power_up_states(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 return 0;
3155}
3156
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 * PCM stuff
3159 */
3160static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3161 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003162 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163{
3164 return 0;
3165}
3166
3167static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3168 struct hda_codec *codec,
3169 unsigned int stream_tag,
3170 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003171 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172{
3173 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3174 return 0;
3175}
3176
3177static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3178 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003179 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180{
Takashi Iwai888afa12008-03-18 09:57:50 +01003181 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 return 0;
3183}
3184
Takashi Iwai6c1f45e2008-07-30 15:01:45 +02003185static int set_pcm_default_values(struct hda_codec *codec,
3186 struct hda_pcm_stream *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187{
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003188 int err;
3189
Takashi Iwai0ba21762007-04-16 11:29:14 +02003190 /* query support PCM information from the given NID */
3191 if (info->nid && (!info->rates || !info->formats)) {
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003192 err = snd_hda_query_supported_pcm(codec, info->nid,
Takashi Iwai0ba21762007-04-16 11:29:14 +02003193 info->rates ? NULL : &info->rates,
3194 info->formats ? NULL : &info->formats,
3195 info->maxbps ? NULL : &info->maxbps);
Jaroslav Kyselaee504712009-03-17 14:30:31 +01003196 if (err < 0)
3197 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 }
3199 if (info->ops.open == NULL)
3200 info->ops.open = hda_pcm_default_open_close;
3201 if (info->ops.close == NULL)
3202 info->ops.close = hda_pcm_default_open_close;
3203 if (info->ops.prepare == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003204 if (snd_BUG_ON(!info->nid))
3205 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 info->ops.prepare = hda_pcm_default_prepare;
3207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 if (info->ops.cleanup == NULL) {
Takashi Iwaida3cec32008-08-08 17:12:14 +02003209 if (snd_BUG_ON(!info->nid))
3210 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 info->ops.cleanup = hda_pcm_default_cleanup;
3212 }
3213 return 0;
3214}
3215
Takashi Iwaieb541332010-08-06 13:48:11 +02003216/*
3217 * codec prepare/cleanup entries
3218 */
Takashi Iwai95a962c2014-10-29 16:03:58 +01003219/**
3220 * snd_hda_codec_prepare - Prepare a stream
3221 * @codec: the HDA codec
3222 * @hinfo: PCM information
3223 * @stream: stream tag to assign
3224 * @format: format id to assign
3225 * @substream: PCM substream to assign
3226 *
3227 * Calls the prepare callback set by the codec with the given arguments.
3228 * Clean up the inactive streams when successful.
3229 */
Takashi Iwaieb541332010-08-06 13:48:11 +02003230int snd_hda_codec_prepare(struct hda_codec *codec,
3231 struct hda_pcm_stream *hinfo,
3232 unsigned int stream,
3233 unsigned int format,
3234 struct snd_pcm_substream *substream)
3235{
3236 int ret;
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003237 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwai61ca4102015-02-27 17:57:55 +01003238 if (hinfo->ops.prepare)
3239 ret = hinfo->ops.prepare(hinfo, codec, stream, format,
3240 substream);
3241 else
3242 ret = -ENODEV;
Takashi Iwaieb541332010-08-06 13:48:11 +02003243 if (ret >= 0)
3244 purify_inactive_streams(codec);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003245 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003246 return ret;
3247}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003248EXPORT_SYMBOL_GPL(snd_hda_codec_prepare);
Takashi Iwaieb541332010-08-06 13:48:11 +02003249
Takashi Iwai95a962c2014-10-29 16:03:58 +01003250/**
3251 * snd_hda_codec_cleanup - Prepare a stream
3252 * @codec: the HDA codec
3253 * @hinfo: PCM information
3254 * @substream: PCM substream
3255 *
3256 * Calls the cleanup callback set by the codec with the given arguments.
3257 */
Takashi Iwaieb541332010-08-06 13:48:11 +02003258void snd_hda_codec_cleanup(struct hda_codec *codec,
3259 struct hda_pcm_stream *hinfo,
3260 struct snd_pcm_substream *substream)
3261{
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003262 mutex_lock(&codec->bus->prepare_mutex);
Takashi Iwai61ca4102015-02-27 17:57:55 +01003263 if (hinfo->ops.cleanup)
3264 hinfo->ops.cleanup(hinfo, codec, substream);
Takashi Iwai3f50ac62010-08-20 09:44:36 +02003265 mutex_unlock(&codec->bus->prepare_mutex);
Takashi Iwaieb541332010-08-06 13:48:11 +02003266}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003267EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup);
Takashi Iwaieb541332010-08-06 13:48:11 +02003268
Takashi Iwaid5191e52009-11-16 14:58:17 +01003269/* global */
Jaroslav Kyselae3303232009-11-10 14:53:02 +01003270const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3271 "Audio", "SPDIF", "HDMI", "Modem"
3272};
3273
Takashi Iwai176d5332008-07-30 15:01:44 +02003274/*
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003275 * get the empty PCM device number to assign
3276 */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003277static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003278{
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003279 /* audio device indices; not linear to keep compatibility */
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003280 /* assigned to static slots up to dev#10; if more needed, assign
3281 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
3282 */
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003283 static int audio_idx[HDA_PCM_NTYPES][5] = {
3284 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3285 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
Wu Fengguang92608ba2009-10-30 11:40:03 +01003286 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003287 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003288 };
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003289 int i;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003290
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003291 if (type >= HDA_PCM_NTYPES) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003292 dev_err(bus->card->dev, "Invalid PCM type %d\n", type);
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003293 return -EINVAL;
3294 }
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003295
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003296 for (i = 0; audio_idx[type][i] >= 0; i++) {
3297#ifndef CONFIG_SND_DYNAMIC_MINORS
3298 if (audio_idx[type][i] >= 8)
3299 break;
3300#endif
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003301 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3302 return audio_idx[type][i];
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003303 }
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003304
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003305#ifdef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai01b65bf2011-11-24 14:31:46 +01003306 /* non-fixed slots starting from 10 */
3307 for (i = 10; i < 32; i++) {
3308 if (!test_and_set_bit(i, bus->pcm_dev_bits))
3309 return i;
3310 }
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003311#endif
Takashi Iwai01b65bf2011-11-24 14:31:46 +01003312
Takashi Iwai4e76a882014-02-25 12:21:03 +01003313 dev_warn(bus->card->dev, "Too many %s devices\n",
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003314 snd_hda_pcm_type_name[type]);
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003315#ifndef CONFIG_SND_DYNAMIC_MINORS
Takashi Iwai4e76a882014-02-25 12:21:03 +01003316 dev_warn(bus->card->dev,
3317 "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
Takashi Iwai36bb00d2013-06-06 12:10:24 +02003318#endif
Wu Fengguangf5d6def52009-10-30 11:38:26 +01003319 return -EAGAIN;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003320}
3321
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003322/* call build_pcms ops of the given codec and set up the default parameters */
3323int snd_hda_codec_parse_pcms(struct hda_codec *codec)
Takashi Iwai176d5332008-07-30 15:01:44 +02003324{
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003325 struct hda_pcm *cpcm;
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003326 int err;
Takashi Iwai176d5332008-07-30 15:01:44 +02003327
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003328 if (!list_empty(&codec->pcm_list_head))
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003329 return 0; /* already parsed */
3330
3331 if (!codec->patch_ops.build_pcms)
3332 return 0;
3333
3334 err = codec->patch_ops.build_pcms(codec);
3335 if (err < 0) {
3336 codec_err(codec, "cannot build PCMs for #%d (error %d)\n",
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003337 codec->core.addr, err);
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003338 return err;
3339 }
3340
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003341 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003342 int stream;
3343
3344 for (stream = 0; stream < 2; stream++) {
3345 struct hda_pcm_stream *info = &cpcm->stream[stream];
3346
3347 if (!info->substreams)
3348 continue;
Takashi Iwai176d5332008-07-30 15:01:44 +02003349 err = set_pcm_default_values(codec, info);
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003350 if (err < 0) {
3351 codec_warn(codec,
3352 "fail to setup default for PCM %s\n",
3353 cpcm->name);
Takashi Iwai176d5332008-07-30 15:01:44 +02003354 return err;
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003355 }
Takashi Iwai176d5332008-07-30 15:01:44 +02003356 }
3357 }
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003358
3359 return 0;
Takashi Iwai176d5332008-07-30 15:01:44 +02003360}
3361
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003362/* assign all PCMs of the given codec */
3363int snd_hda_codec_build_pcms(struct hda_codec *codec)
3364{
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003365 struct hda_bus *bus = codec->bus;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003366 struct hda_pcm *cpcm;
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003367 int dev, err;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003368
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003369 err = snd_hda_codec_parse_pcms(codec);
3370 if (err < 0) {
3371 snd_hda_codec_reset(codec);
3372 return err;
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003373 }
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003374
3375 /* attach a new PCM streams */
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01003376 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003377 if (cpcm->pcm)
3378 continue; /* already attached */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003379 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
Takashi Iwai41b5b012009-01-20 18:21:23 +01003380 continue; /* no substreams assigned */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003381
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003382 dev = get_empty_pcm_device(bus, cpcm->pcm_type);
3383 if (dev < 0)
3384 continue; /* no fatal error */
3385 cpcm->device = dev;
Takashi Iwai0a505752015-04-16 23:25:02 +02003386 err = snd_hda_attach_pcm_stream(bus, codec, cpcm);
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003387 if (err < 0) {
3388 codec_err(codec,
3389 "cannot attach PCM stream %d for codec #%d\n",
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003390 dev, codec->core.addr);
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003391 continue; /* no fatal error */
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003392 }
3393 }
Takashi Iwai1a4ba302015-02-24 11:50:11 +01003394
Takashi Iwai529bd6c2008-11-27 14:17:01 +01003395 return 0;
3396}
3397
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 * snd_hda_add_new_ctls - create controls from the array
3400 * @codec: the HDA codec
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003401 * @knew: the array of struct snd_kcontrol_new
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 *
3403 * This helper function creates and add new controls in the given array.
3404 * The array must be terminated with an empty entry as terminator.
3405 *
3406 * Returns 0 if successful, or a negative error code.
3407 */
Takashi Iwai031024e2011-05-02 11:29:30 +02003408int snd_hda_add_new_ctls(struct hda_codec *codec,
3409 const struct snd_kcontrol_new *knew)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410{
Jaroslav Kysela4d02d1b2009-11-12 10:15:48 +01003411 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412
3413 for (; knew->name; knew++) {
Takashi Iwai54d17402005-11-21 16:33:22 +01003414 struct snd_kcontrol *kctl;
Takashi Iwai1afe2062010-12-23 10:17:52 +01003415 int addr = 0, idx = 0;
Jaroslav Kysela5b0cb1d2009-12-08 16:13:32 +01003416 if (knew->iface == -1) /* skip this codec private value */
3417 continue;
Takashi Iwai1afe2062010-12-23 10:17:52 +01003418 for (;;) {
Takashi Iwai54d17402005-11-21 16:33:22 +01003419 kctl = snd_ctl_new1(knew, codec);
Takashi Iwai0ba21762007-04-16 11:29:14 +02003420 if (!kctl)
Takashi Iwai54d17402005-11-21 16:33:22 +01003421 return -ENOMEM;
Takashi Iwai1afe2062010-12-23 10:17:52 +01003422 if (addr > 0)
3423 kctl->id.device = addr;
3424 if (idx > 0)
3425 kctl->id.index = idx;
Jaroslav Kysela3911a4c2009-11-11 13:43:01 +01003426 err = snd_hda_ctl_add(codec, 0, kctl);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003427 if (!err)
3428 break;
3429 /* try first with another device index corresponding to
3430 * the codec addr; if it still fails (or it's the
3431 * primary codec), then try another control index
3432 */
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003433 if (!addr && codec->core.addr)
3434 addr = codec->core.addr;
Takashi Iwai1afe2062010-12-23 10:17:52 +01003435 else if (!idx && !knew->index) {
3436 idx = find_empty_mixer_ctl_idx(codec,
Takashi Iwaidcda5802012-10-12 17:24:51 +02003437 knew->name, 0);
Takashi Iwai1afe2062010-12-23 10:17:52 +01003438 if (idx <= 0)
3439 return err;
3440 } else
Takashi Iwai54d17402005-11-21 16:33:22 +01003441 return err;
3442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 }
3444 return 0;
3445}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003446EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447
Takashi Iwai83012a72012-08-24 18:38:08 +02003448#ifdef CONFIG_PM
Takashi Iwaibb573922015-02-20 09:26:04 +01003449static void codec_set_power_save(struct hda_codec *codec, int delay)
Takashi Iwaicc72da72015-02-19 16:00:22 +01003450{
3451 struct device *dev = hda_codec_dev(codec);
Takashi Iwaicc72da72015-02-19 16:00:22 +01003452
Lu, Han2377c3c2015-06-09 16:50:38 +08003453 if (delay == 0 && codec->auto_runtime_pm)
3454 delay = 3000;
3455
Takashi Iwaicc72da72015-02-19 16:00:22 +01003456 if (delay > 0) {
3457 pm_runtime_set_autosuspend_delay(dev, delay);
3458 pm_runtime_use_autosuspend(dev);
3459 pm_runtime_allow(dev);
3460 if (!pm_runtime_suspended(dev))
3461 pm_runtime_mark_last_busy(dev);
3462 } else {
3463 pm_runtime_dont_use_autosuspend(dev);
3464 pm_runtime_forbid(dev);
3465 }
3466}
Takashi Iwaibb573922015-02-20 09:26:04 +01003467
3468/**
3469 * snd_hda_set_power_save - reprogram autosuspend for the given delay
3470 * @bus: HD-audio bus
3471 * @delay: autosuspend delay in msec, 0 = off
3472 *
3473 * Synchronize the runtime PM autosuspend state from the power_save option.
3474 */
3475void snd_hda_set_power_save(struct hda_bus *bus, int delay)
3476{
3477 struct hda_codec *c;
3478
Takashi Iwaid068ebc2015-03-02 23:22:59 +01003479 list_for_each_codec(c, bus)
Takashi Iwaibb573922015-02-20 09:26:04 +01003480 codec_set_power_save(c, delay);
3481}
3482EXPORT_SYMBOL_GPL(snd_hda_set_power_save);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003483
Takashi Iwaid5191e52009-11-16 14:58:17 +01003484/**
3485 * snd_hda_check_amp_list_power - Check the amp list and update the power
3486 * @codec: HD-audio codec
3487 * @check: the object containing an AMP list and the status
3488 * @nid: NID to check / update
3489 *
3490 * Check whether the given NID is in the amp list. If it's in the list,
3491 * check the current AMP status, and update the the power-status according
3492 * to the mute status.
3493 *
3494 * This function is supposed to be set or called from the check_power_status
3495 * patch ops.
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003496 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02003497int snd_hda_check_amp_list_power(struct hda_codec *codec,
3498 struct hda_loopback_check *check,
3499 hda_nid_t nid)
3500{
Takashi Iwai031024e2011-05-02 11:29:30 +02003501 const struct hda_amp_list *p;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003502 int ch, v;
3503
3504 if (!check->amplist)
3505 return 0;
3506 for (p = check->amplist; p->nid; p++) {
3507 if (p->nid == nid)
3508 break;
3509 }
3510 if (!p->nid)
3511 return 0; /* nothing changed */
3512
3513 for (p = check->amplist; p->nid; p++) {
3514 for (ch = 0; ch < 2; ch++) {
3515 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
3516 p->idx);
3517 if (!(v & HDA_AMP_MUTE) && v > 0) {
3518 if (!check->power_on) {
3519 check->power_on = 1;
Takashi Iwai664c7152015-04-08 11:43:14 +02003520 snd_hda_power_up_pm(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003521 }
3522 return 1;
3523 }
3524 }
3525 }
3526 if (check->power_on) {
3527 check->power_on = 0;
Takashi Iwai664c7152015-04-08 11:43:14 +02003528 snd_hda_power_down_pm(codec);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003529 }
3530 return 0;
3531}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003532EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power);
Takashi Iwaicb53c622007-08-10 17:21:45 +02003533#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003535/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 * input MUX helper
3537 */
Takashi Iwaid5191e52009-11-16 14:58:17 +01003538
3539/**
3540 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003541 * @imux: imux helper object
3542 * @uinfo: pointer to get/store the data
Takashi Iwaid5191e52009-11-16 14:58:17 +01003543 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003544int snd_hda_input_mux_info(const struct hda_input_mux *imux,
3545 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546{
3547 unsigned int index;
3548
3549 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3550 uinfo->count = 1;
3551 uinfo->value.enumerated.items = imux->num_items;
Takashi Iwai5513b0c2007-10-09 11:58:41 +02003552 if (!imux->num_items)
3553 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 index = uinfo->value.enumerated.item;
3555 if (index >= imux->num_items)
3556 index = imux->num_items - 1;
3557 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
3558 return 0;
3559}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003560EXPORT_SYMBOL_GPL(snd_hda_input_mux_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561
Takashi Iwaid5191e52009-11-16 14:58:17 +01003562/**
3563 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003564 * @codec: the HDA codec
3565 * @imux: imux helper object
3566 * @ucontrol: pointer to get/store the data
3567 * @nid: input mux NID
3568 * @cur_val: pointer to get/store the current imux value
Takashi Iwaid5191e52009-11-16 14:58:17 +01003569 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003570int snd_hda_input_mux_put(struct hda_codec *codec,
3571 const struct hda_input_mux *imux,
3572 struct snd_ctl_elem_value *ucontrol,
3573 hda_nid_t nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 unsigned int *cur_val)
3575{
3576 unsigned int idx;
3577
Takashi Iwai5513b0c2007-10-09 11:58:41 +02003578 if (!imux->num_items)
3579 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 idx = ucontrol->value.enumerated.item[0];
3581 if (idx >= imux->num_items)
3582 idx = imux->num_items - 1;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003583 if (*cur_val == idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 return 0;
Takashi Iwai82beb8f2007-08-10 17:09:26 +02003585 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
3586 imux->items[idx].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 *cur_val = idx;
3588 return 1;
3589}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003590EXPORT_SYMBOL_GPL(snd_hda_input_mux_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591
3592
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003593/**
3594 * snd_hda_enum_helper_info - Helper for simple enum ctls
3595 * @kcontrol: ctl element
3596 * @uinfo: pointer to get/store the data
3597 * @num_items: number of enum items
3598 * @texts: enum item string array
3599 *
Takashi Iwaidda415d2012-11-30 18:34:38 +01003600 * process kcontrol info callback of a simple string enum array
3601 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
3602 */
3603int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
3604 struct snd_ctl_elem_info *uinfo,
3605 int num_items, const char * const *texts)
3606{
3607 static const char * const texts_default[] = {
3608 "Disabled", "Enabled"
3609 };
3610
3611 if (!texts || !num_items) {
3612 num_items = 2;
3613 texts = texts_default;
3614 }
3615
Takashi Iwai3ff72212014-10-20 18:17:28 +02003616 return snd_ctl_enum_info(uinfo, 1, num_items, texts);
Takashi Iwaidda415d2012-11-30 18:34:38 +01003617}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003618EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info);
Takashi Iwaidda415d2012-11-30 18:34:38 +01003619
3620/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 * Multi-channel / digital-out PCM helper functions
3622 */
3623
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003624/* setup SPDIF output stream */
3625static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
3626 unsigned int stream_tag, unsigned int format)
3627{
Laurence Darby3bef1c32012-11-03 17:00:06 +00003628 struct hda_spdif_out *spdif;
3629 unsigned int curr_fmt;
3630 bool reset;
Stephen Warren7c935972011-06-01 11:14:17 -06003631
Laurence Darby3bef1c32012-11-03 17:00:06 +00003632 spdif = snd_hda_spdif_out_of_nid(codec, nid);
3633 curr_fmt = snd_hda_codec_read(codec, nid, 0,
3634 AC_VERB_GET_STREAM_FORMAT, 0);
3635 reset = codec->spdif_status_reset &&
3636 (spdif->ctls & AC_DIG1_ENABLE) &&
3637 curr_fmt != format;
3638
3639 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
3640 updated */
3641 if (reset)
Norberto Lopes28aedaf2010-02-28 20:16:53 +01003642 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06003643 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
Takashi Iwai2f728532008-09-25 16:32:41 +02003644 -1);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003645 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
Takashi Iwai2f728532008-09-25 16:32:41 +02003646 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02003647 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02003648 for (d = codec->slave_dig_outs; *d; d++)
3649 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
3650 format);
Matthew Ranostayde51ca12008-09-07 14:31:40 -04003651 }
Takashi Iwai2f728532008-09-25 16:32:41 +02003652 /* turn on again (if needed) */
Laurence Darby3bef1c32012-11-03 17:00:06 +00003653 if (reset)
Takashi Iwai2f728532008-09-25 16:32:41 +02003654 set_dig_out_convert(codec, nid,
Stephen Warren7c935972011-06-01 11:14:17 -06003655 spdif->ctls & 0xff, -1);
Takashi Iwai2f728532008-09-25 16:32:41 +02003656}
Matthew Ranostayde51ca12008-09-07 14:31:40 -04003657
Takashi Iwai2f728532008-09-25 16:32:41 +02003658static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
3659{
3660 snd_hda_codec_cleanup_stream(codec, nid);
3661 if (codec->slave_dig_outs) {
Takashi Iwaidda14412011-05-02 11:29:30 +02003662 const hda_nid_t *d;
Takashi Iwai2f728532008-09-25 16:32:41 +02003663 for (d = codec->slave_dig_outs; *d; d++)
3664 snd_hda_codec_cleanup_stream(codec, *d);
3665 }
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003666}
3667
Takashi Iwaid5191e52009-11-16 14:58:17 +01003668/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01003669 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003670 * @codec: the HDA codec
3671 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003673int snd_hda_multi_out_dig_open(struct hda_codec *codec,
3674 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675{
Ingo Molnar62932df2006-01-16 16:34:20 +01003676 mutex_lock(&codec->spdif_mutex);
Takashi Iwai5930ca42007-04-16 11:23:56 +02003677 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
3678 /* already opened as analog dup; reset it once */
Takashi Iwai2f728532008-09-25 16:32:41 +02003679 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
Ingo Molnar62932df2006-01-16 16:34:20 +01003681 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 return 0;
3683}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003684EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685
Takashi Iwaid5191e52009-11-16 14:58:17 +01003686/**
3687 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003688 * @codec: the HDA codec
3689 * @mout: hda_multi_out object
3690 * @stream_tag: stream tag to assign
3691 * @format: format id to assign
3692 * @substream: PCM substream to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01003693 */
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003694int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
3695 struct hda_multi_out *mout,
3696 unsigned int stream_tag,
3697 unsigned int format,
3698 struct snd_pcm_substream *substream)
3699{
3700 mutex_lock(&codec->spdif_mutex);
3701 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
3702 mutex_unlock(&codec->spdif_mutex);
3703 return 0;
3704}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003705EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare);
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003706
Takashi Iwaid5191e52009-11-16 14:58:17 +01003707/**
3708 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003709 * @codec: the HDA codec
3710 * @mout: hda_multi_out object
Takashi Iwaid5191e52009-11-16 14:58:17 +01003711 */
Takashi Iwai9411e212009-02-13 11:32:28 +01003712int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
3713 struct hda_multi_out *mout)
3714{
3715 mutex_lock(&codec->spdif_mutex);
3716 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3717 mutex_unlock(&codec->spdif_mutex);
3718 return 0;
3719}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003720EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup);
Takashi Iwai9411e212009-02-13 11:32:28 +01003721
Takashi Iwaid5191e52009-11-16 14:58:17 +01003722/**
3723 * snd_hda_multi_out_dig_close - release the digital out stream
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003724 * @codec: the HDA codec
3725 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003727int snd_hda_multi_out_dig_close(struct hda_codec *codec,
3728 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729{
Ingo Molnar62932df2006-01-16 16:34:20 +01003730 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 mout->dig_out_used = 0;
Ingo Molnar62932df2006-01-16 16:34:20 +01003732 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733 return 0;
3734}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003735EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736
Takashi Iwaid5191e52009-11-16 14:58:17 +01003737/**
3738 * snd_hda_multi_out_analog_open - open analog outputs
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003739 * @codec: the HDA codec
3740 * @mout: hda_multi_out object
3741 * @substream: PCM substream to assign
3742 * @hinfo: PCM information to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01003743 *
3744 * Open analog outputs and set up the hw-constraints.
3745 * If the digital outputs can be opened as slave, open the digital
3746 * outputs, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003748int snd_hda_multi_out_analog_open(struct hda_codec *codec,
3749 struct hda_multi_out *mout,
Takashi Iwai9a081602008-02-12 18:37:26 +01003750 struct snd_pcm_substream *substream,
3751 struct hda_pcm_stream *hinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752{
Takashi Iwai9a081602008-02-12 18:37:26 +01003753 struct snd_pcm_runtime *runtime = substream->runtime;
3754 runtime->hw.channels_max = mout->max_channels;
3755 if (mout->dig_out_nid) {
3756 if (!mout->analog_rates) {
3757 mout->analog_rates = hinfo->rates;
3758 mout->analog_formats = hinfo->formats;
3759 mout->analog_maxbps = hinfo->maxbps;
3760 } else {
3761 runtime->hw.rates = mout->analog_rates;
3762 runtime->hw.formats = mout->analog_formats;
3763 hinfo->maxbps = mout->analog_maxbps;
3764 }
3765 if (!mout->spdif_rates) {
3766 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
3767 &mout->spdif_rates,
3768 &mout->spdif_formats,
3769 &mout->spdif_maxbps);
3770 }
3771 mutex_lock(&codec->spdif_mutex);
3772 if (mout->share_spdif) {
Takashi Iwai022b4662009-07-03 23:03:30 +02003773 if ((runtime->hw.rates & mout->spdif_rates) &&
3774 (runtime->hw.formats & mout->spdif_formats)) {
3775 runtime->hw.rates &= mout->spdif_rates;
3776 runtime->hw.formats &= mout->spdif_formats;
3777 if (mout->spdif_maxbps < hinfo->maxbps)
3778 hinfo->maxbps = mout->spdif_maxbps;
3779 } else {
3780 mout->share_spdif = 0;
3781 /* FIXME: need notify? */
3782 }
Takashi Iwai9a081602008-02-12 18:37:26 +01003783 }
Frederik Deweerdteaa99852008-04-14 13:11:44 +02003784 mutex_unlock(&codec->spdif_mutex);
Takashi Iwai9a081602008-02-12 18:37:26 +01003785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 return snd_pcm_hw_constraint_step(substream->runtime, 0,
3787 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
3788}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003789EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790
Takashi Iwaid5191e52009-11-16 14:58:17 +01003791/**
3792 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003793 * @codec: the HDA codec
3794 * @mout: hda_multi_out object
3795 * @stream_tag: stream tag to assign
3796 * @format: format id to assign
3797 * @substream: PCM substream to assign
Takashi Iwaid5191e52009-11-16 14:58:17 +01003798 *
3799 * Set up the i/o for analog out.
3800 * When the digital out is available, copy the front out to digital out, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003802int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
3803 struct hda_multi_out *mout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804 unsigned int stream_tag,
3805 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +01003806 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807{
Takashi Iwaidda14412011-05-02 11:29:30 +02003808 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809 int chs = substream->runtime->channels;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003810 struct hda_spdif_out *spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 int i;
3812
Ingo Molnar62932df2006-01-16 16:34:20 +01003813 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003814 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
Takashi Iwai9a081602008-02-12 18:37:26 +01003815 if (mout->dig_out_nid && mout->share_spdif &&
3816 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817 if (chs == 2 &&
Takashi Iwai0ba21762007-04-16 11:29:14 +02003818 snd_hda_is_supported_format(codec, mout->dig_out_nid,
3819 format) &&
Stephen Warren7c935972011-06-01 11:14:17 -06003820 !(spdif->status & IEC958_AES0_NONAUDIO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
Takashi Iwai6b97eb42007-04-05 14:51:48 +02003822 setup_dig_out_stream(codec, mout->dig_out_nid,
3823 stream_tag, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 } else {
3825 mout->dig_out_used = 0;
Takashi Iwai2f728532008-09-25 16:32:41 +02003826 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 }
3828 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003829 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830
3831 /* front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003832 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
3833 0, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02003834 if (!mout->no_share_stream &&
3835 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 /* headphone out will just decode front left/right (stereo) */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003837 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
3838 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01003839 /* extra outputs copied from front */
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02003840 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
3841 if (!mout->no_share_stream && mout->hp_out_nid[i])
3842 snd_hda_codec_setup_stream(codec,
3843 mout->hp_out_nid[i],
3844 stream_tag, 0, format);
Takashi Iwai82bc9552006-03-21 11:24:42 +01003845
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846 /* surrounds */
3847 for (i = 1; i < mout->num_dacs; i++) {
Takashi Iwai4b3acaf2005-06-10 19:48:10 +02003848 if (chs >= (i + 1) * 2) /* independent out */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003849 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3850 i * 2, format);
Takashi Iwaid29240c2007-10-26 12:35:56 +02003851 else if (!mout->no_share_stream) /* copy front */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003852 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3853 0, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854 }
David Henningssoncd4035e2013-10-10 09:01:25 +02003855
3856 /* extra surrounds */
3857 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
3858 int ch = 0;
3859 if (!mout->extra_out_nid[i])
3860 break;
3861 if (chs >= (i + 1) * 2)
3862 ch = i * 2;
3863 else if (!mout->no_share_stream)
3864 break;
3865 snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
3866 stream_tag, ch, format);
3867 }
3868
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869 return 0;
3870}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003871EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872
Takashi Iwaid5191e52009-11-16 14:58:17 +01003873/**
3874 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003875 * @codec: the HDA codec
3876 * @mout: hda_multi_out object
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877 */
Takashi Iwai0ba21762007-04-16 11:29:14 +02003878int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
3879 struct hda_multi_out *mout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880{
Takashi Iwaidda14412011-05-02 11:29:30 +02003881 const hda_nid_t *nids = mout->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882 int i;
3883
3884 for (i = 0; i < mout->num_dacs; i++)
Takashi Iwai888afa12008-03-18 09:57:50 +01003885 snd_hda_codec_cleanup_stream(codec, nids[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 if (mout->hp_nid)
Takashi Iwai888afa12008-03-18 09:57:50 +01003887 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
Takashi Iwaia06dbfc2011-08-23 18:16:13 +02003888 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
3889 if (mout->hp_out_nid[i])
3890 snd_hda_codec_cleanup_stream(codec,
3891 mout->hp_out_nid[i]);
Takashi Iwai82bc9552006-03-21 11:24:42 +01003892 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
3893 if (mout->extra_out_nid[i])
Takashi Iwai888afa12008-03-18 09:57:50 +01003894 snd_hda_codec_cleanup_stream(codec,
3895 mout->extra_out_nid[i]);
Ingo Molnar62932df2006-01-16 16:34:20 +01003896 mutex_lock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
Takashi Iwai2f728532008-09-25 16:32:41 +02003898 cleanup_dig_out_stream(codec, mout->dig_out_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 mout->dig_out_used = 0;
3900 }
Ingo Molnar62932df2006-01-16 16:34:20 +01003901 mutex_unlock(&codec->spdif_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 return 0;
3903}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003904EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905
Takashi Iwai47408602012-04-20 13:06:53 +02003906/**
3907 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003908 * @codec: the HDA codec
3909 * @pin: referred pin NID
Takashi Iwai47408602012-04-20 13:06:53 +02003910 *
3911 * Guess the suitable VREF pin bits to be set as the pin-control value.
3912 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
3913 */
3914unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
3915{
3916 unsigned int pincap;
3917 unsigned int oldval;
3918 oldval = snd_hda_codec_read(codec, pin, 0,
3919 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3920 pincap = snd_hda_query_pin_caps(codec, pin);
3921 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
3922 /* Exception: if the default pin setup is vref50, we give it priority */
3923 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
3924 return AC_PINCTL_VREF_80;
3925 else if (pincap & AC_PINCAP_VREF_50)
3926 return AC_PINCTL_VREF_50;
3927 else if (pincap & AC_PINCAP_VREF_100)
3928 return AC_PINCTL_VREF_100;
3929 else if (pincap & AC_PINCAP_VREF_GRD)
3930 return AC_PINCTL_VREF_GRD;
3931 return AC_PINCTL_VREF_HIZ;
3932}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003933EXPORT_SYMBOL_GPL(snd_hda_get_default_vref);
Takashi Iwai47408602012-04-20 13:06:53 +02003934
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003935/**
3936 * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap
3937 * @codec: the HDA codec
3938 * @pin: referred pin NID
3939 * @val: pin ctl value to audit
3940 */
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01003941unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
3942 hda_nid_t pin, unsigned int val)
3943{
3944 static unsigned int cap_lists[][2] = {
3945 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
3946 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
3947 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
3948 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
3949 };
3950 unsigned int cap;
3951
3952 if (!val)
3953 return 0;
3954 cap = snd_hda_query_pin_caps(codec, pin);
3955 if (!cap)
3956 return val; /* don't know what to do... */
3957
3958 if (val & AC_PINCTL_OUT_EN) {
3959 if (!(cap & AC_PINCAP_OUT))
3960 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
3961 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
3962 val &= ~AC_PINCTL_HP_EN;
3963 }
3964
3965 if (val & AC_PINCTL_IN_EN) {
3966 if (!(cap & AC_PINCAP_IN))
3967 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
3968 else {
3969 unsigned int vcap, vref;
3970 int i;
3971 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
3972 vref = val & AC_PINCTL_VREFEN;
3973 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
3974 if (vref == cap_lists[i][0] &&
3975 !(vcap & cap_lists[i][1])) {
3976 if (i == ARRAY_SIZE(cap_lists) - 1)
3977 vref = AC_PINCTL_VREF_HIZ;
3978 else
3979 vref = cap_lists[i + 1][0];
3980 }
3981 }
3982 val &= ~AC_PINCTL_VREFEN;
3983 val |= vref;
3984 }
3985 }
3986
3987 return val;
3988}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003989EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl);
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01003990
Takashi Iwaia11e9b12014-10-29 15:06:01 +01003991/**
3992 * _snd_hda_pin_ctl - Helper to set pin ctl value
3993 * @codec: the HDA codec
3994 * @pin: referred pin NID
3995 * @val: pin control value to set
3996 * @cached: access over codec pinctl cache or direct write
3997 *
3998 * This function is a helper to set a pin ctl value more safely.
3999 * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the
4000 * value in pin target array via snd_hda_codec_set_pin_target(), then
4001 * actually writes the value via either snd_hda_codec_update_cache() or
4002 * snd_hda_codec_write() depending on @cached flag.
4003 */
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02004004int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
4005 unsigned int val, bool cached)
4006{
Takashi Iwai62f3a2f2013-01-10 08:56:46 +01004007 val = snd_hda_correct_pin_ctl(codec, pin, val);
Takashi Iwaid7fdc002013-01-10 08:38:04 +01004008 snd_hda_codec_set_pin_target(codec, pin, val);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02004009 if (cached)
4010 return snd_hda_codec_update_cache(codec, pin, 0,
4011 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
4012 else
4013 return snd_hda_codec_write(codec, pin, 0,
4014 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
4015}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004016EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl);
Takashi Iwaicdd03ce2012-04-20 12:34:50 +02004017
Takashi Iwai990061c2010-09-09 22:08:44 +02004018/**
4019 * snd_hda_add_imux_item - Add an item to input_mux
Takashi Iwaia11e9b12014-10-29 15:06:01 +01004020 * @codec: the HDA codec
4021 * @imux: imux helper object
4022 * @label: the name of imux item to assign
4023 * @index: index number of imux item to assign
4024 * @type_idx: pointer to store the resultant label index
Takashi Iwai990061c2010-09-09 22:08:44 +02004025 *
4026 * When the same label is used already in the existing items, the number
4027 * suffix is appended to the label. This label index number is stored
4028 * to type_idx when non-NULL pointer is given.
4029 */
Takashi Iwai6194b992014-06-06 18:12:16 +02004030int snd_hda_add_imux_item(struct hda_codec *codec,
4031 struct hda_input_mux *imux, const char *label,
Takashi Iwai10a20af2010-09-09 16:28:02 +02004032 int index, int *type_idx)
4033{
4034 int i, label_idx = 0;
4035 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
Takashi Iwai6194b992014-06-06 18:12:16 +02004036 codec_err(codec, "hda_codec: Too many imux items!\n");
Takashi Iwai10a20af2010-09-09 16:28:02 +02004037 return -EINVAL;
4038 }
4039 for (i = 0; i < imux->num_items; i++) {
4040 if (!strncmp(label, imux->items[i].label, strlen(label)))
4041 label_idx++;
4042 }
4043 if (type_idx)
4044 *type_idx = label_idx;
4045 if (label_idx > 0)
4046 snprintf(imux->items[imux->num_items].label,
4047 sizeof(imux->items[imux->num_items].label),
4048 "%s %d", label, label_idx);
4049 else
4050 strlcpy(imux->items[imux->num_items].label, label,
4051 sizeof(imux->items[imux->num_items].label));
4052 imux->items[imux->num_items].index = index;
4053 imux->num_items++;
4054 return 0;
4055}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004056EXPORT_SYMBOL_GPL(snd_hda_add_imux_item);
Takashi Iwaid7b1ae92010-08-30 13:00:16 +02004057
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058/**
Takashi Iwai0a505752015-04-16 23:25:02 +02004059 * snd_hda_bus_reset_codecs - Reset the bus
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004060 * @bus: HD-audio bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 */
Takashi Iwai0a505752015-04-16 23:25:02 +02004062void snd_hda_bus_reset_codecs(struct hda_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063{
Takashi Iwai0ba21762007-04-16 11:29:14 +02004064 struct hda_codec *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065
Takashi Iwaid068ebc2015-03-02 23:22:59 +01004066 list_for_each_codec(codec, bus) {
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004067 /* FIXME: maybe a better way needed for forced reset */
David Henningsson26a6cb62012-10-09 15:04:21 +02004068 cancel_delayed_work_sync(&codec->jackpoll_work);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004069#ifdef CONFIG_PM
Mengdong Lin0e24dbb2013-11-26 23:00:51 -05004070 if (hda_codec_is_power_on(codec)) {
Takashi Iwaicc72da72015-02-19 16:00:22 +01004071 hda_call_codec_suspend(codec);
Mengdong Lin12edb892013-11-26 23:32:23 -05004072 hda_call_codec_resume(codec);
Takashi Iwai59ed1ea2015-02-18 15:39:59 +01004073 }
4074#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076}
Takashi Iwaib2e18592008-07-30 15:01:44 +02004077
Takashi Iwaid5191e52009-11-16 14:58:17 +01004078/**
Takashi Iwaid5191e52009-11-16 14:58:17 +01004079 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
4080 * @pcm: PCM caps bits
4081 * @buf: the string buffer to write
4082 * @buflen: the max buffer length
4083 *
4084 * used by hda_proc.c and hda_eld.c
4085 */
Takashi Iwaib2022262008-11-21 21:24:03 +01004086void snd_print_pcm_bits(int pcm, char *buf, int buflen)
4087{
4088 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
4089 int i, j;
4090
4091 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
4092 if (pcm & (AC_SUPPCM_BITS_8 << i))
4093 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
4094
4095 buf[j] = '\0'; /* necessary when j == 0 */
4096}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004097EXPORT_SYMBOL_GPL(snd_print_pcm_bits);
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004098
4099MODULE_DESCRIPTION("HDA codec core");
4100MODULE_LICENSE("GPL");