Linux Audio

Check our new training course

Linux debugging, profiling, tracing and performance analysis training

Mar 24-27, 2025, special US time zones
Register
Loading...
v6.9.4
   1// SPDX-License-Identifier: GPL-2.0+
   2//
   3// soc-dapm.c  --  ALSA SoC Dynamic Audio Power Management
   4//
   5// Copyright 2005 Wolfson Microelectronics PLC.
   6// Author: Liam Girdwood <lrg@slimlogic.co.uk>
   7//
   8//  Features:
   9//    o Changes power status of internal codec blocks depending on the
  10//      dynamic configuration of codec internal audio paths and active
  11//      DACs/ADCs.
  12//    o Platform power domain - can support external components i.e. amps and
  13//      mic/headphone insertion events.
  14//    o Automatic Mic Bias support
  15//    o Jack insertion power event initiation - e.g. hp insertion will enable
  16//      sinks, dacs, etc
  17//    o Delayed power down of audio subsystem to reduce pops between a quick
  18//      device reopen.
 
 
 
 
 
 
  19
  20#include <linux/module.h>
 
  21#include <linux/init.h>
  22#include <linux/async.h>
  23#include <linux/delay.h>
  24#include <linux/pm.h>
  25#include <linux/bitops.h>
  26#include <linux/platform_device.h>
  27#include <linux/jiffies.h>
  28#include <linux/debugfs.h>
  29#include <linux/pm_runtime.h>
  30#include <linux/regulator/consumer.h>
  31#include <linux/pinctrl/consumer.h>
  32#include <linux/clk.h>
  33#include <linux/slab.h>
  34#include <sound/core.h>
  35#include <sound/pcm.h>
  36#include <sound/pcm_params.h>
  37#include <sound/soc.h>
  38#include <sound/initval.h>
  39
  40#include <trace/events/asoc.h>
  41
  42#define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
  43
  44#define SND_SOC_DAPM_DIR_REVERSE(x) ((x == SND_SOC_DAPM_DIR_IN) ? \
  45	SND_SOC_DAPM_DIR_OUT : SND_SOC_DAPM_DIR_IN)
  46
  47#define snd_soc_dapm_for_each_direction(dir) \
  48	for ((dir) = SND_SOC_DAPM_DIR_IN; (dir) <= SND_SOC_DAPM_DIR_OUT; \
  49		(dir)++)
  50
  51static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
  52	struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
  53	const char *control,
  54	int (*connected)(struct snd_soc_dapm_widget *source,
  55			 struct snd_soc_dapm_widget *sink));
  56
  57struct snd_soc_dapm_widget *
  58snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
  59			 const struct snd_soc_dapm_widget *widget);
  60
  61struct snd_soc_dapm_widget *
  62snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
  63			 const struct snd_soc_dapm_widget *widget);
  64
  65static unsigned int soc_dapm_read(struct snd_soc_dapm_context *dapm, int reg);
  66
  67/* dapm power sequences - make this per codec in the future */
  68static int dapm_up_seq[] = {
  69	[snd_soc_dapm_pre] = 1,
  70	[snd_soc_dapm_regulator_supply] = 2,
  71	[snd_soc_dapm_pinctrl] = 2,
  72	[snd_soc_dapm_clock_supply] = 2,
  73	[snd_soc_dapm_supply] = 3,
  74	[snd_soc_dapm_dai_link] = 3,
  75	[snd_soc_dapm_micbias] = 4,
  76	[snd_soc_dapm_vmid] = 4,
  77	[snd_soc_dapm_dai_in] = 5,
  78	[snd_soc_dapm_dai_out] = 5,
  79	[snd_soc_dapm_aif_in] = 5,
  80	[snd_soc_dapm_aif_out] = 5,
  81	[snd_soc_dapm_mic] = 6,
  82	[snd_soc_dapm_siggen] = 6,
  83	[snd_soc_dapm_input] = 6,
  84	[snd_soc_dapm_output] = 6,
  85	[snd_soc_dapm_mux] = 7,
  86	[snd_soc_dapm_demux] = 7,
  87	[snd_soc_dapm_dac] = 8,
  88	[snd_soc_dapm_switch] = 9,
  89	[snd_soc_dapm_mixer] = 9,
  90	[snd_soc_dapm_mixer_named_ctl] = 9,
  91	[snd_soc_dapm_pga] = 10,
  92	[snd_soc_dapm_buffer] = 10,
  93	[snd_soc_dapm_scheduler] = 10,
  94	[snd_soc_dapm_effect] = 10,
  95	[snd_soc_dapm_src] = 10,
  96	[snd_soc_dapm_asrc] = 10,
  97	[snd_soc_dapm_encoder] = 10,
  98	[snd_soc_dapm_decoder] = 10,
  99	[snd_soc_dapm_adc] = 11,
 100	[snd_soc_dapm_out_drv] = 12,
 101	[snd_soc_dapm_hp] = 12,
 102	[snd_soc_dapm_line] = 12,
 103	[snd_soc_dapm_sink] = 12,
 104	[snd_soc_dapm_spk] = 13,
 105	[snd_soc_dapm_kcontrol] = 14,
 106	[snd_soc_dapm_post] = 15,
 107};
 108
 109static int dapm_down_seq[] = {
 110	[snd_soc_dapm_pre] = 1,
 111	[snd_soc_dapm_kcontrol] = 2,
 112	[snd_soc_dapm_adc] = 3,
 113	[snd_soc_dapm_spk] = 4,
 114	[snd_soc_dapm_hp] = 5,
 115	[snd_soc_dapm_line] = 5,
 116	[snd_soc_dapm_out_drv] = 5,
 117	[snd_soc_dapm_sink] = 6,
 118	[snd_soc_dapm_pga] = 6,
 119	[snd_soc_dapm_buffer] = 6,
 120	[snd_soc_dapm_scheduler] = 6,
 121	[snd_soc_dapm_effect] = 6,
 122	[snd_soc_dapm_src] = 6,
 123	[snd_soc_dapm_asrc] = 6,
 124	[snd_soc_dapm_encoder] = 6,
 125	[snd_soc_dapm_decoder] = 6,
 126	[snd_soc_dapm_switch] = 7,
 127	[snd_soc_dapm_mixer_named_ctl] = 7,
 128	[snd_soc_dapm_mixer] = 7,
 129	[snd_soc_dapm_dac] = 8,
 130	[snd_soc_dapm_mic] = 9,
 131	[snd_soc_dapm_siggen] = 9,
 132	[snd_soc_dapm_input] = 9,
 133	[snd_soc_dapm_output] = 9,
 134	[snd_soc_dapm_micbias] = 10,
 135	[snd_soc_dapm_vmid] = 10,
 136	[snd_soc_dapm_mux] = 11,
 137	[snd_soc_dapm_demux] = 11,
 138	[snd_soc_dapm_aif_in] = 12,
 139	[snd_soc_dapm_aif_out] = 12,
 140	[snd_soc_dapm_dai_in] = 12,
 141	[snd_soc_dapm_dai_out] = 12,
 142	[snd_soc_dapm_dai_link] = 13,
 143	[snd_soc_dapm_supply] = 14,
 144	[snd_soc_dapm_clock_supply] = 15,
 145	[snd_soc_dapm_pinctrl] = 15,
 146	[snd_soc_dapm_regulator_supply] = 15,
 147	[snd_soc_dapm_post] = 16,
 148};
 149
 150static void dapm_assert_locked(struct snd_soc_dapm_context *dapm)
 151{
 152	if (snd_soc_card_is_instantiated(dapm->card))
 153		snd_soc_dapm_mutex_assert_held(dapm);
 154}
 155
 156static void pop_wait(u32 pop_time)
 157{
 158	if (pop_time)
 159		schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
 160}
 161
 162__printf(3, 4)
 163static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
 164{
 165	va_list args;
 166	char *buf;
 167
 168	if (!pop_time)
 169		return;
 170
 171	buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
 172	if (buf == NULL)
 173		return;
 174
 175	va_start(args, fmt);
 176	vsnprintf(buf, PAGE_SIZE, fmt, args);
 177	dev_info(dev, "%s", buf);
 178	va_end(args);
 179
 180	kfree(buf);
 181}
 182
 183static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
 184{
 185	return !list_empty(&w->dirty);
 186}
 187
 188static void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
 189{
 190	dapm_assert_locked(w->dapm);
 191
 192	if (!dapm_dirty_widget(w)) {
 193		dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
 194			 w->name, reason);
 195		list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
 196	}
 197}
 198
 199/*
 200 * Common implementation for dapm_widget_invalidate_input_paths() and
 201 * dapm_widget_invalidate_output_paths(). The function is inlined since the
 202 * combined size of the two specialized functions is only marginally larger then
 203 * the size of the generic function and at the same time the fast path of the
 204 * specialized functions is significantly smaller than the generic function.
 205 */
 206static __always_inline void dapm_widget_invalidate_paths(
 207	struct snd_soc_dapm_widget *w, enum snd_soc_dapm_direction dir)
 208{
 209	enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
 210	struct snd_soc_dapm_widget *node;
 211	struct snd_soc_dapm_path *p;
 212	LIST_HEAD(list);
 213
 214	dapm_assert_locked(w->dapm);
 215
 216	if (w->endpoints[dir] == -1)
 217		return;
 218
 219	list_add_tail(&w->work_list, &list);
 220	w->endpoints[dir] = -1;
 221
 222	list_for_each_entry(w, &list, work_list) {
 223		snd_soc_dapm_widget_for_each_path(w, dir, p) {
 224			if (p->is_supply || p->weak || !p->connect)
 225				continue;
 226			node = p->node[rdir];
 227			if (node->endpoints[dir] != -1) {
 228				node->endpoints[dir] = -1;
 229				list_add_tail(&node->work_list, &list);
 230			}
 231		}
 232	}
 233}
 234
 235/*
 236 * dapm_widget_invalidate_input_paths() - Invalidate the cached number of
 237 *  input paths
 238 * @w: The widget for which to invalidate the cached number of input paths
 239 *
 240 * Resets the cached number of inputs for the specified widget and all widgets
 241 * that can be reached via outcoming paths from the widget.
 242 *
 243 * This function must be called if the number of output paths for a widget might
 244 * have changed. E.g. if the source state of a widget changes or a path is added
 245 * or activated with the widget as the sink.
 246 */
 247static void dapm_widget_invalidate_input_paths(struct snd_soc_dapm_widget *w)
 248{
 249	dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_IN);
 250}
 251
 252/*
 253 * dapm_widget_invalidate_output_paths() - Invalidate the cached number of
 254 *  output paths
 255 * @w: The widget for which to invalidate the cached number of output paths
 256 *
 257 * Resets the cached number of outputs for the specified widget and all widgets
 258 * that can be reached via incoming paths from the widget.
 259 *
 260 * This function must be called if the number of output paths for a widget might
 261 * have changed. E.g. if the sink state of a widget changes or a path is added
 262 * or activated with the widget as the source.
 263 */
 264static void dapm_widget_invalidate_output_paths(struct snd_soc_dapm_widget *w)
 265{
 266	dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_OUT);
 267}
 268
 269/*
 270 * dapm_path_invalidate() - Invalidates the cached number of inputs and outputs
 271 *  for the widgets connected to a path
 272 * @p: The path to invalidate
 273 *
 274 * Resets the cached number of inputs for the sink of the path and the cached
 275 * number of outputs for the source of the path.
 276 *
 277 * This function must be called when a path is added, removed or the connected
 278 * state changes.
 279 */
 280static void dapm_path_invalidate(struct snd_soc_dapm_path *p)
 281{
 282	/*
 283	 * Weak paths or supply paths do not influence the number of input or
 284	 * output paths of their neighbors.
 285	 */
 286	if (p->weak || p->is_supply)
 287		return;
 288
 289	/*
 290	 * The number of connected endpoints is the sum of the number of
 291	 * connected endpoints of all neighbors. If a node with 0 connected
 292	 * endpoints is either connected or disconnected that sum won't change,
 293	 * so there is no need to re-check the path.
 294	 */
 295	if (p->source->endpoints[SND_SOC_DAPM_DIR_IN] != 0)
 296		dapm_widget_invalidate_input_paths(p->sink);
 297	if (p->sink->endpoints[SND_SOC_DAPM_DIR_OUT] != 0)
 298		dapm_widget_invalidate_output_paths(p->source);
 299}
 300
 301void dapm_mark_endpoints_dirty(struct snd_soc_card *card)
 302{
 303	struct snd_soc_dapm_widget *w;
 304
 305	snd_soc_dapm_mutex_lock_root(card);
 306
 307	for_each_card_widgets(card, w) {
 308		if (w->is_ep) {
 309			dapm_mark_dirty(w, "Rechecking endpoints");
 310			if (w->is_ep & SND_SOC_DAPM_EP_SINK)
 311				dapm_widget_invalidate_output_paths(w);
 312			if (w->is_ep & SND_SOC_DAPM_EP_SOURCE)
 313				dapm_widget_invalidate_input_paths(w);
 314		}
 315	}
 316
 317	snd_soc_dapm_mutex_unlock(card);
 318}
 319EXPORT_SYMBOL_GPL(dapm_mark_endpoints_dirty);
 320
 321/* create a new dapm widget */
 322static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
 323	const struct snd_soc_dapm_widget *_widget,
 324	const char *prefix)
 325{
 326	struct snd_soc_dapm_widget *w;
 327
 328	w = kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
 329	if (!w)
 330		return NULL;
 331
 332	if (prefix)
 333		w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, _widget->name);
 334	else
 335		w->name = kstrdup_const(_widget->name, GFP_KERNEL);
 336	if (!w->name) {
 337		kfree(w);
 338		return NULL;
 339	}
 340
 341	if (_widget->sname) {
 342		w->sname = kstrdup_const(_widget->sname, GFP_KERNEL);
 343		if (!w->sname) {
 344			kfree_const(w->name);
 345			kfree(w);
 346			return NULL;
 347		}
 348	}
 349	return w;
 350}
 351
 352struct dapm_kcontrol_data {
 353	unsigned int value;
 354	struct snd_soc_dapm_widget *widget;
 355	struct list_head paths;
 356	struct snd_soc_dapm_widget_list *wlist;
 357};
 358
 359static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget *widget,
 360	struct snd_kcontrol *kcontrol, const char *ctrl_name)
 361{
 362	struct dapm_kcontrol_data *data;
 363	struct soc_mixer_control *mc;
 364	struct soc_enum *e;
 365	const char *name;
 366	int ret;
 367
 368	data = kzalloc(sizeof(*data), GFP_KERNEL);
 369	if (!data)
 370		return -ENOMEM;
 371
 372	INIT_LIST_HEAD(&data->paths);
 373
 374	switch (widget->id) {
 375	case snd_soc_dapm_switch:
 376	case snd_soc_dapm_mixer:
 377	case snd_soc_dapm_mixer_named_ctl:
 378		mc = (struct soc_mixer_control *)kcontrol->private_value;
 379
 380		if (mc->autodisable) {
 381			struct snd_soc_dapm_widget template;
 382
 383			if (snd_soc_volsw_is_stereo(mc))
 384				dev_warn(widget->dapm->dev,
 385					 "ASoC: Unsupported stereo autodisable control '%s'\n",
 386					 ctrl_name);
 387
 388			name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
 389					 "Autodisable");
 390			if (!name) {
 391				ret = -ENOMEM;
 392				goto err_data;
 393			}
 394
 395			memset(&template, 0, sizeof(template));
 396			template.reg = mc->reg;
 397			template.mask = (1 << fls(mc->max)) - 1;
 398			template.shift = mc->shift;
 399			if (mc->invert)
 400				template.off_val = mc->max;
 401			else
 402				template.off_val = 0;
 403			template.on_val = template.off_val;
 404			template.id = snd_soc_dapm_kcontrol;
 405			template.name = name;
 406
 407			data->value = template.on_val;
 408
 409			data->widget =
 410				snd_soc_dapm_new_control_unlocked(widget->dapm,
 411				&template);
 412			kfree(name);
 413			if (IS_ERR(data->widget)) {
 414				ret = PTR_ERR(data->widget);
 415				goto err_data;
 416			}
 417		}
 418		break;
 419	case snd_soc_dapm_demux:
 420	case snd_soc_dapm_mux:
 421		e = (struct soc_enum *)kcontrol->private_value;
 422
 423		if (e->autodisable) {
 424			struct snd_soc_dapm_widget template;
 425
 426			name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
 427					 "Autodisable");
 428			if (!name) {
 429				ret = -ENOMEM;
 430				goto err_data;
 431			}
 432
 433			memset(&template, 0, sizeof(template));
 434			template.reg = e->reg;
 435			template.mask = e->mask;
 436			template.shift = e->shift_l;
 437			template.off_val = snd_soc_enum_item_to_val(e, 0);
 438			template.on_val = template.off_val;
 439			template.id = snd_soc_dapm_kcontrol;
 440			template.name = name;
 441
 442			data->value = template.on_val;
 443
 444			data->widget = snd_soc_dapm_new_control_unlocked(
 445						widget->dapm, &template);
 446			kfree(name);
 447			if (IS_ERR(data->widget)) {
 448				ret = PTR_ERR(data->widget);
 449				goto err_data;
 450			}
 451
 452			snd_soc_dapm_add_path(widget->dapm, data->widget,
 453					      widget, NULL, NULL);
 454		} else if (e->reg != SND_SOC_NOPM) {
 455			data->value = soc_dapm_read(widget->dapm, e->reg) &
 456				      (e->mask << e->shift_l);
 457		}
 458		break;
 459	default:
 460		break;
 461	}
 462
 463	kcontrol->private_data = data;
 464
 465	return 0;
 466
 467err_data:
 468	kfree(data);
 469	return ret;
 470}
 471
 472static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
 473{
 474	struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
 475
 476	list_del(&data->paths);
 477	kfree(data->wlist);
 478	kfree(data);
 479}
 480
 481static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
 482	const struct snd_kcontrol *kcontrol)
 483{
 484	struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
 485
 486	return data->wlist;
 487}
 488
 489static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
 490	struct snd_soc_dapm_widget *widget)
 
 491{
 492	struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
 493	struct snd_soc_dapm_widget_list *new_wlist;
 494	unsigned int n;
 495
 496	if (data->wlist)
 497		n = data->wlist->num_widgets + 1;
 498	else
 499		n = 1;
 500
 501	new_wlist = krealloc(data->wlist,
 502			     struct_size(new_wlist, widgets, n),
 503			     GFP_KERNEL);
 504	if (!new_wlist)
 505		return -ENOMEM;
 506
 507	new_wlist->num_widgets = n;
 508	new_wlist->widgets[n - 1] = widget;
 509
 510	data->wlist = new_wlist;
 511
 512	return 0;
 513}
 514
 515static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
 516	struct snd_soc_dapm_path *path)
 517{
 518	struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
 519
 520	list_add_tail(&path->list_kcontrol, &data->paths);
 521}
 522
 523static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
 524{
 525	struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
 526
 527	if (!data->widget)
 528		return true;
 529
 530	return data->widget->power;
 531}
 532
 533static struct list_head *dapm_kcontrol_get_path_list(
 534	const struct snd_kcontrol *kcontrol)
 535{
 536	struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
 537
 538	return &data->paths;
 539}
 540
 541#define dapm_kcontrol_for_each_path(path, kcontrol) \
 542	list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
 543		list_kcontrol)
 544
 545unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
 546{
 547	struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
 548
 549	return data->value;
 550}
 551EXPORT_SYMBOL_GPL(dapm_kcontrol_get_value);
 552
 553static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
 554	unsigned int value)
 555{
 556	struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
 557
 558	if (data->value == value)
 559		return false;
 560
 561	if (data->widget) {
 562		switch (dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->id) {
 563		case snd_soc_dapm_switch:
 564		case snd_soc_dapm_mixer:
 565		case snd_soc_dapm_mixer_named_ctl:
 566			data->widget->on_val = value & data->widget->mask;
 567			break;
 568		case snd_soc_dapm_demux:
 569		case snd_soc_dapm_mux:
 570			data->widget->on_val = value >> data->widget->shift;
 571			break;
 572		default:
 573			data->widget->on_val = value;
 574			break;
 575		}
 576	}
 577
 578	data->value = value;
 579
 580	return true;
 581}
 582
 583/**
 584 * snd_soc_dapm_kcontrol_widget() - Returns the widget associated to a
 585 *   kcontrol
 586 * @kcontrol: The kcontrol
 587 */
 588struct snd_soc_dapm_widget *snd_soc_dapm_kcontrol_widget(
 589				struct snd_kcontrol *kcontrol)
 590{
 591	return dapm_kcontrol_get_wlist(kcontrol)->widgets[0];
 592}
 593EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_widget);
 594
 595/**
 596 * snd_soc_dapm_kcontrol_dapm() - Returns the dapm context associated to a
 597 *  kcontrol
 598 * @kcontrol: The kcontrol
 599 *
 600 * Note: This function must only be used on kcontrols that are known to have
 601 * been registered for a CODEC. Otherwise the behaviour is undefined.
 602 */
 603struct snd_soc_dapm_context *snd_soc_dapm_kcontrol_dapm(
 604	struct snd_kcontrol *kcontrol)
 605{
 606	return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->dapm;
 607}
 608EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_dapm);
 609
 610static void dapm_reset(struct snd_soc_card *card)
 611{
 612	struct snd_soc_dapm_widget *w;
 613
 614	snd_soc_dapm_mutex_assert_held(card);
 615
 616	memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
 617
 618	for_each_card_widgets(card, w) {
 619		w->new_power = w->power;
 620		w->power_checked = false;
 
 
 621	}
 622}
 623
 624static const char *soc_dapm_prefix(struct snd_soc_dapm_context *dapm)
 625{
 626	if (!dapm->component)
 627		return NULL;
 628	return dapm->component->name_prefix;
 629}
 630
 631static unsigned int soc_dapm_read(struct snd_soc_dapm_context *dapm, int reg)
 632{
 633	if (!dapm->component)
 634		return -EIO;
 635	return  snd_soc_component_read(dapm->component, reg);
 636}
 637
 638static int soc_dapm_update_bits(struct snd_soc_dapm_context *dapm,
 639	int reg, unsigned int mask, unsigned int value)
 640{
 641	if (!dapm->component)
 642		return -EIO;
 643	return snd_soc_component_update_bits(dapm->component, reg,
 644					     mask, value);
 
 
 
 645}
 646
 647static int soc_dapm_test_bits(struct snd_soc_dapm_context *dapm,
 648	int reg, unsigned int mask, unsigned int value)
 649{
 650	if (!dapm->component)
 651		return -EIO;
 652	return snd_soc_component_test_bits(dapm->component, reg, mask, value);
 
 653}
 654
 655static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
 656{
 657	if (dapm->component)
 658		snd_soc_component_async_complete(dapm->component);
 
 
 659}
 660
 661static struct snd_soc_dapm_widget *
 662dapm_wcache_lookup(struct snd_soc_dapm_widget *w, const char *name)
 663{
 664	if (w) {
 665		struct list_head *wlist = &w->dapm->card->widgets;
 666		const int depth = 2;
 667		int i = 0;
 668
 669		list_for_each_entry_from(w, wlist, list) {
 670			if (!strcmp(name, w->name))
 671				return w;
 
 
 
 
 
 
 
 
 
 672
 673			if (++i == depth)
 674				break;
 
 
 
 
 
 
 
 675		}
 
 676	}
 677
 678	return NULL;
 679}
 680
 681/**
 682 * snd_soc_dapm_force_bias_level() - Sets the DAPM bias level
 683 * @dapm: The DAPM context for which to set the level
 684 * @level: The level to set
 685 *
 686 * Forces the DAPM bias level to a specific state. It will call the bias level
 687 * callback of DAPM context with the specified level. This will even happen if
 688 * the context is already at the same level. Furthermore it will not go through
 689 * the normal bias level sequencing, meaning any intermediate states between the
 690 * current and the target state will not be entered.
 691 *
 692 * Note that the change in bias level is only temporary and the next time
 693 * snd_soc_dapm_sync() is called the state will be set to the level as
 694 * determined by the DAPM core. The function is mainly intended to be used to
 695 * used during probe or resume from suspend to power up the device so
 696 * initialization can be done, before the DAPM core takes over.
 697 */
 698int snd_soc_dapm_force_bias_level(struct snd_soc_dapm_context *dapm,
 699	enum snd_soc_bias_level level)
 700{
 701	int ret = 0;
 702
 703	if (dapm->component)
 704		ret = snd_soc_component_set_bias_level(dapm->component, level);
 705
 706	if (ret == 0)
 707		dapm->bias_level = level;
 708
 709	return ret;
 710}
 711EXPORT_SYMBOL_GPL(snd_soc_dapm_force_bias_level);
 712
 713/**
 714 * snd_soc_dapm_set_bias_level - set the bias level for the system
 715 * @dapm: DAPM context
 716 * @level: level to configure
 717 *
 718 * Configure the bias (power) levels for the SoC audio device.
 719 *
 720 * Returns 0 for success else error.
 721 */
 722static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
 723				       enum snd_soc_bias_level level)
 724{
 725	struct snd_soc_card *card = dapm->card;
 726	int ret = 0;
 727
 728	trace_snd_soc_bias_level_start(dapm, level);
 729
 730	ret = snd_soc_card_set_bias_level(card, dapm, level);
 
 731	if (ret != 0)
 732		goto out;
 733
 734	if (!card || dapm != &card->dapm)
 735		ret = snd_soc_dapm_force_bias_level(dapm, level);
 736
 
 
 
 
 737	if (ret != 0)
 738		goto out;
 739
 740	ret = snd_soc_card_set_bias_level_post(card, dapm, level);
 
 741out:
 742	trace_snd_soc_bias_level_done(dapm, level);
 743
 744	return ret;
 745}
 746
 747/* connect mux widget to its interconnecting audio paths */
 748static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
 749	struct snd_soc_dapm_path *path, const char *control_name,
 750	struct snd_soc_dapm_widget *w)
 751{
 752	const struct snd_kcontrol_new *kcontrol = &w->kcontrol_news[0];
 753	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
 754	unsigned int item;
 755	int i;
 
 
 
 
 
 
 
 
 756
 757	if (e->reg != SND_SOC_NOPM) {
 758		unsigned int val;
 759		val = soc_dapm_read(dapm, e->reg);
 760		val = (val >> e->shift_l) & e->mask;
 761		item = snd_soc_enum_val_to_item(e, val);
 762	} else {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 763		/* since a virtual mux has no backing registers to
 764		 * decide which path to connect, it will try to match
 765		 * with the first enumeration.  This is to ensure
 766		 * that the default mux choice (the first) will be
 767		 * correctly powered up during initialization.
 768		 */
 769		item = 0;
 
 770	}
 
 
 
 
 
 771
 772	i = match_string(e->texts, e->items, control_name);
 773	if (i < 0)
 774		return -ENODEV;
 775
 776	path->name = e->texts[i];
 777	path->connect = (i == item);
 778	return 0;
 779
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 780}
 781
 782/* set up initial codec paths */
 783static void dapm_set_mixer_path_status(struct snd_soc_dapm_path *p, int i,
 784				       int nth_path)
 
 
 785{
 786	struct soc_mixer_control *mc = (struct soc_mixer_control *)
 787		p->sink->kcontrol_news[i].private_value;
 788	unsigned int reg = mc->reg;
 789	unsigned int invert = mc->invert;
 790
 791	if (reg != SND_SOC_NOPM) {
 792		unsigned int shift = mc->shift;
 793		unsigned int max = mc->max;
 794		unsigned int mask = (1 << fls(max)) - 1;
 795		unsigned int val = soc_dapm_read(p->sink->dapm, reg);
 796
 797		/*
 798		 * The nth_path argument allows this function to know
 799		 * which path of a kcontrol it is setting the initial
 800		 * status for. Ideally this would support any number
 801		 * of paths and channels. But since kcontrols only come
 802		 * in mono and stereo variants, we are limited to 2
 803		 * channels.
 804		 *
 805		 * The following code assumes for stereo controls the
 806		 * first path is the left channel, and all remaining
 807		 * paths are the right channel.
 808		 */
 809		if (snd_soc_volsw_is_stereo(mc) && nth_path > 0) {
 810			if (reg != mc->rreg)
 811				val = soc_dapm_read(p->sink->dapm, mc->rreg);
 812			val = (val >> mc->rshift) & mask;
 813		} else {
 814			val = (val >> shift) & mask;
 815		}
 816		if (invert)
 817			val = max - val;
 818		p->connect = !!val;
 819	} else {
 820		/* since a virtual mixer has no backing registers to
 821		 * decide which path to connect, it will try to match
 822		 * with initial state.  This is to ensure
 823		 * that the default mixer choice will be
 824		 * correctly powered up during initialization.
 825		 */
 826		p->connect = invert;
 827	}
 
 
 828}
 829
 830/* connect mixer widget to its interconnecting audio paths */
 831static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
 
 832	struct snd_soc_dapm_path *path, const char *control_name)
 833{
 834	int i, nth_path = 0;
 835
 836	/* search for mixer kcontrol */
 837	for (i = 0; i < path->sink->num_kcontrols; i++) {
 838		if (!strcmp(control_name, path->sink->kcontrol_news[i].name)) {
 839			path->name = path->sink->kcontrol_news[i].name;
 840			dapm_set_mixer_path_status(path, i, nth_path++);
 
 
 
 841			return 0;
 842		}
 843	}
 844	return -ENODEV;
 845}
 846
 847static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
 848	struct snd_soc_dapm_widget *kcontrolw,
 849	const struct snd_kcontrol_new *kcontrol_new,
 850	struct snd_kcontrol **kcontrol)
 851{
 852	struct snd_soc_dapm_widget *w;
 853	int i;
 854
 855	*kcontrol = NULL;
 856
 857	for_each_card_widgets(dapm->card, w) {
 858		if (w == kcontrolw || w->dapm != kcontrolw->dapm)
 859			continue;
 860		for (i = 0; i < w->num_kcontrols; i++) {
 861			if (&w->kcontrol_news[i] == kcontrol_new) {
 862				if (w->kcontrols)
 863					*kcontrol = w->kcontrols[i];
 864				return 1;
 865			}
 866		}
 867	}
 868
 869	return 0;
 870}
 871
 872/*
 873 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
 874 * create it. Either way, add the widget into the control's widget list
 875 */
 876static int dapm_create_or_share_kcontrol(struct snd_soc_dapm_widget *w,
 877	int kci)
 878{
 879	struct snd_soc_dapm_context *dapm = w->dapm;
 
 
 
 880	struct snd_card *card = dapm->card->snd_card;
 881	const char *prefix;
 882	size_t prefix_len;
 883	int shared;
 884	struct snd_kcontrol *kcontrol;
 885	bool wname_in_long_name, kcname_in_long_name;
 886	char *long_name = NULL;
 887	const char *name;
 888	int ret = 0;
 889
 890	prefix = soc_dapm_prefix(dapm);
 891	if (prefix)
 892		prefix_len = strlen(prefix) + 1;
 893	else
 894		prefix_len = 0;
 895
 896	shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
 897					 &kcontrol);
 898
 899	if (!kcontrol) {
 900		if (shared) {
 901			wname_in_long_name = false;
 902			kcname_in_long_name = true;
 903		} else {
 904			switch (w->id) {
 905			case snd_soc_dapm_switch:
 906			case snd_soc_dapm_mixer:
 907			case snd_soc_dapm_pga:
 908			case snd_soc_dapm_effect:
 909			case snd_soc_dapm_out_drv:
 910				wname_in_long_name = true;
 911				kcname_in_long_name = true;
 912				break;
 913			case snd_soc_dapm_mixer_named_ctl:
 914				wname_in_long_name = false;
 915				kcname_in_long_name = true;
 916				break;
 917			case snd_soc_dapm_demux:
 918			case snd_soc_dapm_mux:
 919				wname_in_long_name = true;
 920				kcname_in_long_name = false;
 921				break;
 922			default:
 923				return -EINVAL;
 924			}
 925		}
 926		if (w->no_wname_in_kcontrol_name)
 927			wname_in_long_name = false;
 928
 929		if (wname_in_long_name && kcname_in_long_name) {
 930			/*
 931			 * The control will get a prefix from the control
 932			 * creation process but we're also using the same
 933			 * prefix for widgets so cut the prefix off the
 934			 * front of the widget name.
 935			 */
 936			long_name = kasprintf(GFP_KERNEL, "%s %s",
 937				 w->name + prefix_len,
 938				 w->kcontrol_news[kci].name);
 939			if (long_name == NULL)
 940				return -ENOMEM;
 941
 942			name = long_name;
 943		} else if (wname_in_long_name) {
 944			long_name = NULL;
 945			name = w->name + prefix_len;
 946		} else {
 947			long_name = NULL;
 948			name = w->kcontrol_news[kci].name;
 949		}
 950
 951		kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
 952					prefix);
 953		if (!kcontrol) {
 954			ret = -ENOMEM;
 955			goto exit_free;
 956		}
 957
 958		kcontrol->private_free = dapm_kcontrol_free;
 959
 960		ret = dapm_kcontrol_data_alloc(w, kcontrol, name);
 961		if (ret) {
 962			snd_ctl_free_one(kcontrol);
 963			goto exit_free;
 964		}
 965
 966		ret = snd_ctl_add(card, kcontrol);
 967		if (ret < 0) {
 968			dev_err(dapm->dev,
 969				"ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
 970				w->name, name, ret);
 971			goto exit_free;
 972		}
 973	}
 974
 975	ret = dapm_kcontrol_add_widget(kcontrol, w);
 976	if (ret == 0)
 977		w->kcontrols[kci] = kcontrol;
 
 978
 979exit_free:
 980	kfree(long_name);
 
 
 
 
 
 
 
 
 
 981
 982	return ret;
 983}
 
 
 
 
 
 
 
 984
 985/* create new dapm mixer control */
 986static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
 987{
 988	int i, ret;
 989	struct snd_soc_dapm_path *path;
 990	struct dapm_kcontrol_data *data;
 991
 992	/* add kcontrol */
 993	for (i = 0; i < w->num_kcontrols; i++) {
 994		/* match name */
 995		snd_soc_dapm_widget_for_each_source_path(w, path) {
 996			/* mixer/mux paths name must match control name */
 997			if (path->name != (char *)w->kcontrol_news[i].name)
 998				continue;
 999
1000			if (!w->kcontrols[i]) {
1001				ret = dapm_create_or_share_kcontrol(w, i);
1002				if (ret < 0)
1003					return ret;
 
 
 
 
 
 
 
 
 
 
 
 
1004			}
1005
1006			dapm_kcontrol_add_path(w->kcontrols[i], path);
1007
1008			data = snd_kcontrol_chip(w->kcontrols[i]);
1009			if (data->widget)
1010				snd_soc_dapm_add_path(data->widget->dapm,
1011						      data->widget,
1012						      path->source,
1013						      NULL, NULL);
 
 
 
 
 
 
 
 
1014		}
1015	}
1016
1017	return 0;
1018}
1019
1020/* create new dapm mux control */
1021static int dapm_new_mux(struct snd_soc_dapm_widget *w)
1022{
1023	struct snd_soc_dapm_context *dapm = w->dapm;
1024	enum snd_soc_dapm_direction dir;
1025	struct snd_soc_dapm_path *path;
1026	const char *type;
 
 
1027	int ret;
1028
1029	switch (w->id) {
1030	case snd_soc_dapm_mux:
1031		dir = SND_SOC_DAPM_DIR_OUT;
1032		type = "mux";
1033		break;
1034	case snd_soc_dapm_demux:
1035		dir = SND_SOC_DAPM_DIR_IN;
1036		type = "demux";
1037		break;
1038	default:
1039		return -EINVAL;
1040	}
1041
1042	if (w->num_kcontrols != 1) {
1043		dev_err(dapm->dev,
1044			"ASoC: %s %s has incorrect number of controls\n", type,
1045			w->name);
1046		return -EINVAL;
1047	}
1048
1049	if (list_empty(&w->edges[dir])) {
1050		dev_err(dapm->dev, "ASoC: %s %s has no paths\n", type, w->name);
1051		return -EINVAL;
 
 
 
 
 
 
 
 
 
 
 
 
 
1052	}
 
 
1053
1054	ret = dapm_create_or_share_kcontrol(w, 0);
1055	if (ret < 0)
1056		return ret;
 
 
1057
1058	snd_soc_dapm_widget_for_each_path(w, dir, path) {
1059		if (path->name)
1060			dapm_kcontrol_add_path(w->kcontrols[0], path);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1061	}
1062
 
 
 
 
 
 
 
1063	return 0;
1064}
1065
1066/* create new dapm volume control */
1067static int dapm_new_pga(struct snd_soc_dapm_widget *w)
1068{
1069	int i;
1070
1071	for (i = 0; i < w->num_kcontrols; i++) {
1072		int ret = dapm_create_or_share_kcontrol(w, i);
1073		if (ret < 0)
1074			return ret;
1075	}
1076
1077	return 0;
1078}
1079
1080/* create new dapm dai link control */
1081static int dapm_new_dai_link(struct snd_soc_dapm_widget *w)
1082{
1083	int i;
1084	struct snd_soc_pcm_runtime *rtd = w->priv;
1085
1086	/* create control for links with > 1 config */
1087	if (rtd->dai_link->num_c2c_params <= 1)
1088		return 0;
1089
1090	/* add kcontrol */
1091	for (i = 0; i < w->num_kcontrols; i++) {
1092		struct snd_soc_dapm_context *dapm = w->dapm;
1093		struct snd_card *card = dapm->card->snd_card;
1094		struct snd_kcontrol *kcontrol = snd_soc_cnew(&w->kcontrol_news[i],
1095							     w, w->name, NULL);
1096		int ret = snd_ctl_add(card, kcontrol);
1097
1098		if (ret < 0) {
1099			dev_err(dapm->dev,
1100				"ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
1101				w->name, w->kcontrol_news[i].name, ret);
1102			return ret;
1103		}
1104		kcontrol->private_data = w;
1105		w->kcontrols[i] = kcontrol;
1106	}
1107
1108	return 0;
 
1109}
1110
1111/* We implement power down on suspend by checking the power state of
1112 * the ALSA card - when we are suspending the ALSA state for the card
1113 * is set to D3.
1114 */
1115static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
1116{
1117	int level = snd_power_get_state(widget->dapm->card->snd_card);
1118
1119	switch (level) {
1120	case SNDRV_CTL_POWER_D3hot:
1121	case SNDRV_CTL_POWER_D3cold:
1122		if (widget->ignore_suspend)
1123			dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
1124				widget->name);
1125		return widget->ignore_suspend;
1126	default:
1127		return 1;
1128	}
1129}
1130
1131static void dapm_widget_list_free(struct snd_soc_dapm_widget_list **list)
 
 
1132{
1133	kfree(*list);
1134}
1135
1136static int dapm_widget_list_create(struct snd_soc_dapm_widget_list **list,
1137	struct list_head *widgets)
1138{
1139	struct snd_soc_dapm_widget *w;
1140	struct list_head *it;
1141	unsigned int size = 0;
1142	unsigned int i = 0;
1143
1144	list_for_each(it, widgets)
1145		size++;
1146
1147	*list = kzalloc(struct_size(*list, widgets, size), GFP_KERNEL);
1148	if (*list == NULL)
1149		return -ENOMEM;
 
 
1150
1151	list_for_each_entry(w, widgets, work_list)
1152		(*list)->widgets[i++] = w;
 
 
 
 
 
 
 
 
 
1153
1154	(*list)->num_widgets = i;
 
 
1155
1156	return 0;
 
 
1157}
1158
1159/*
1160 * Recursively reset the cached number of inputs or outputs for the specified
1161 * widget and all widgets that can be reached via incoming or outcoming paths
1162 * from the widget.
1163 */
1164static void invalidate_paths_ep(struct snd_soc_dapm_widget *widget,
1165	enum snd_soc_dapm_direction dir)
1166{
1167	enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
1168	struct snd_soc_dapm_path *path;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1169
1170	widget->endpoints[dir] = -1;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1171
1172	snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
1173		if (path->weak || path->is_supply)
1174			continue;
1175
1176		if (path->walking)
1177			return;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1178
1179		if (path->connect) {
1180			path->walking = 1;
1181			invalidate_paths_ep(path->node[dir], dir);
1182			path->walking = 0;
1183		}
1184	}
 
 
 
 
1185}
1186
1187/*
1188 * Common implementation for is_connected_output_ep() and
1189 * is_connected_input_ep(). The function is inlined since the combined size of
1190 * the two specialized functions is only marginally larger then the size of the
1191 * generic function and at the same time the fast path of the specialized
1192 * functions is significantly smaller than the generic function.
1193 */
1194static __always_inline int is_connected_ep(struct snd_soc_dapm_widget *widget,
1195	struct list_head *list, enum snd_soc_dapm_direction dir,
1196	int (*fn)(struct snd_soc_dapm_widget *, struct list_head *,
1197		  bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1198						enum snd_soc_dapm_direction)),
1199	bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1200				      enum snd_soc_dapm_direction))
1201{
1202	enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
1203	struct snd_soc_dapm_path *path;
1204	int con = 0;
1205
1206	if (widget->endpoints[dir] >= 0)
1207		return widget->endpoints[dir];
1208
1209	DAPM_UPDATE_STAT(widget, path_checks);
1210
1211	/* do we need to add this widget to the list ? */
1212	if (list)
1213		list_add_tail(&widget->work_list, list);
 
 
 
 
1214
1215	if (custom_stop_condition && custom_stop_condition(widget, dir)) {
1216		list = NULL;
1217		custom_stop_condition = NULL;
 
 
 
 
 
 
 
 
1218	}
1219
1220	if ((widget->is_ep & SND_SOC_DAPM_DIR_TO_EP(dir)) && widget->connected) {
1221		widget->endpoints[dir] = snd_soc_dapm_suspend_check(widget);
1222		return widget->endpoints[dir];
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1223	}
1224
1225	snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
1226		DAPM_UPDATE_STAT(widget, neighbour_checks);
1227
1228		if (path->weak || path->is_supply)
1229			continue;
1230
1231		if (path->walking)
1232			return 1;
1233
1234		trace_snd_soc_dapm_path(widget, dir, path);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1235
1236		if (path->connect) {
1237			path->walking = 1;
1238			con += fn(path->node[dir], list, custom_stop_condition);
1239			path->walking = 0;
1240		}
1241	}
1242
1243	widget->endpoints[dir] = con;
1244
1245	return con;
1246}
1247
1248/*
1249 * Recursively check for a completed path to an active or physically connected
1250 * output widget. Returns number of complete paths.
1251 *
1252 * Optionally, can be supplied with a function acting as a stopping condition.
1253 * This function takes the dapm widget currently being examined and the walk
1254 * direction as an arguments, it should return true if widgets from that point
1255 * in the graph onwards should not be added to the widget list.
1256 */
1257static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
1258	struct list_head *list,
1259	bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1260				      enum snd_soc_dapm_direction))
1261{
1262	return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_OUT,
1263			is_connected_output_ep, custom_stop_condition);
1264}
1265
1266/*
1267 * Recursively check for a completed path to an active or physically connected
1268 * input widget. Returns number of complete paths.
1269 *
1270 * Optionally, can be supplied with a function acting as a stopping condition.
1271 * This function takes the dapm widget currently being examined and the walk
1272 * direction as an arguments, it should return true if the walk should be
1273 * stopped and false otherwise.
1274 */
1275static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
1276	struct list_head *list,
1277	bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1278				      enum snd_soc_dapm_direction))
1279{
1280	return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_IN,
1281			is_connected_input_ep, custom_stop_condition);
1282}
1283
1284/**
1285 * snd_soc_dapm_dai_get_connected_widgets - query audio path and it's widgets.
1286 * @dai: the soc DAI.
1287 * @stream: stream direction.
1288 * @list: list of active widgets for this stream.
1289 * @custom_stop_condition: (optional) a function meant to stop the widget graph
1290 *                         walk based on custom logic.
1291 *
1292 * Queries DAPM graph as to whether a valid audio stream path exists for
1293 * the initial stream specified by name. This takes into account
1294 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1295 *
1296 * Optionally, can be supplied with a function acting as a stopping condition.
1297 * This function takes the dapm widget currently being examined and the walk
1298 * direction as an arguments, it should return true if the walk should be
1299 * stopped and false otherwise.
1300 *
1301 * Returns the number of valid paths or negative error.
1302 */
1303int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1304	struct snd_soc_dapm_widget_list **list,
1305	bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1306				      enum snd_soc_dapm_direction))
1307{
1308	struct snd_soc_card *card = dai->component->card;
1309	struct snd_soc_dapm_widget *w = snd_soc_dai_get_widget(dai, stream);
1310	LIST_HEAD(widgets);
1311	int paths;
1312	int ret;
1313
1314	snd_soc_dapm_mutex_lock(card);
1315
1316	if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1317		invalidate_paths_ep(w, SND_SOC_DAPM_DIR_OUT);
1318		paths = is_connected_output_ep(w, &widgets,
1319				custom_stop_condition);
1320	} else {
1321		invalidate_paths_ep(w, SND_SOC_DAPM_DIR_IN);
1322		paths = is_connected_input_ep(w, &widgets,
1323				custom_stop_condition);
1324	}
1325
1326	/* Drop starting point */
1327	list_del(widgets.next);
1328
1329	ret = dapm_widget_list_create(list, &widgets);
1330	if (ret)
1331		paths = ret;
 
1332
1333	trace_snd_soc_dapm_connected(paths, stream);
1334	snd_soc_dapm_mutex_unlock(card);
 
1335
1336	return paths;
1337}
1338EXPORT_SYMBOL_GPL(snd_soc_dapm_dai_get_connected_widgets);
1339
1340void snd_soc_dapm_dai_free_widgets(struct snd_soc_dapm_widget_list **list)
1341{
1342	dapm_widget_list_free(list);
1343}
1344EXPORT_SYMBOL_GPL(snd_soc_dapm_dai_free_widgets);
1345
1346/*
1347 * Handler for regulator supply widget.
1348 */
1349int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1350		   struct snd_kcontrol *kcontrol, int event)
1351{
1352	int ret;
1353
1354	soc_dapm_async_complete(w->dapm);
1355
1356	if (SND_SOC_DAPM_EVENT_ON(event)) {
1357		if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1358			ret = regulator_allow_bypass(w->regulator, false);
1359			if (ret != 0)
1360				dev_warn(w->dapm->dev,
1361					 "ASoC: Failed to unbypass %s: %d\n",
1362					 w->name, ret);
1363		}
1364
1365		return regulator_enable(w->regulator);
1366	} else {
1367		if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1368			ret = regulator_allow_bypass(w->regulator, true);
1369			if (ret != 0)
1370				dev_warn(w->dapm->dev,
1371					 "ASoC: Failed to bypass %s: %d\n",
1372					 w->name, ret);
1373		}
1374
1375		return regulator_disable_deferred(w->regulator, w->shift);
1376	}
1377}
1378EXPORT_SYMBOL_GPL(dapm_regulator_event);
1379
1380/*
1381 * Handler for pinctrl widget.
1382 */
1383int dapm_pinctrl_event(struct snd_soc_dapm_widget *w,
1384		       struct snd_kcontrol *kcontrol, int event)
1385{
1386	struct snd_soc_dapm_pinctrl_priv *priv = w->priv;
1387	struct pinctrl *p = w->pinctrl;
1388	struct pinctrl_state *s;
1389
1390	if (!p || !priv)
1391		return -EIO;
1392
1393	if (SND_SOC_DAPM_EVENT_ON(event))
1394		s = pinctrl_lookup_state(p, priv->active_state);
1395	else
1396		s = pinctrl_lookup_state(p, priv->sleep_state);
1397
1398	if (IS_ERR(s))
1399		return PTR_ERR(s);
1400
1401	return pinctrl_select_state(p, s);
1402}
1403EXPORT_SYMBOL_GPL(dapm_pinctrl_event);
1404
1405/*
1406 * Handler for clock supply widget.
1407 */
1408int dapm_clock_event(struct snd_soc_dapm_widget *w,
1409		   struct snd_kcontrol *kcontrol, int event)
1410{
1411	if (!w->clk)
1412		return -EIO;
1413
1414	soc_dapm_async_complete(w->dapm);
1415
1416	if (SND_SOC_DAPM_EVENT_ON(event)) {
1417		return clk_prepare_enable(w->clk);
1418	} else {
1419		clk_disable_unprepare(w->clk);
1420		return 0;
1421	}
1422
1423	return 0;
1424}
1425EXPORT_SYMBOL_GPL(dapm_clock_event);
1426
1427static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1428{
1429	if (w->power_checked)
1430		return w->new_power;
1431
1432	if (w->force)
1433		w->new_power = 1;
1434	else
1435		w->new_power = w->power_check(w);
1436
1437	w->power_checked = true;
1438
1439	return w->new_power;
1440}
1441
1442/* Generic check to see if a widget should be powered. */
 
1443static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1444{
1445	int in, out;
1446
1447	DAPM_UPDATE_STAT(w, power_checks);
1448
1449	in = is_connected_input_ep(w, NULL, NULL);
1450	out = is_connected_output_ep(w, NULL, NULL);
 
 
1451	return out != 0 && in != 0;
1452}
1453
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1454/* Check to see if a power supply is needed */
1455static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1456{
1457	struct snd_soc_dapm_path *path;
1458
1459	DAPM_UPDATE_STAT(w, power_checks);
1460
1461	/* Check if one of our outputs is connected */
1462	snd_soc_dapm_widget_for_each_sink_path(w, path) {
1463		DAPM_UPDATE_STAT(w, neighbour_checks);
1464
1465		if (path->weak)
1466			continue;
1467
1468		if (path->connected &&
1469		    !path->connected(path->source, path->sink))
1470			continue;
1471
 
 
 
1472		if (dapm_widget_power_check(path->sink))
1473			return 1;
1474	}
1475
 
 
1476	return 0;
1477}
1478
1479static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1480{
1481	return w->connected;
1482}
1483
1484static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1485			    struct snd_soc_dapm_widget *b,
1486			    bool power_up)
1487{
1488	int *sort;
1489
1490	BUILD_BUG_ON(ARRAY_SIZE(dapm_up_seq) != SND_SOC_DAPM_TYPE_COUNT);
1491	BUILD_BUG_ON(ARRAY_SIZE(dapm_down_seq) != SND_SOC_DAPM_TYPE_COUNT);
1492
1493	if (power_up)
1494		sort = dapm_up_seq;
1495	else
1496		sort = dapm_down_seq;
1497
1498	WARN_ONCE(sort[a->id] == 0, "offset a->id %d not initialized\n", a->id);
1499	WARN_ONCE(sort[b->id] == 0, "offset b->id %d not initialized\n", b->id);
1500
1501	if (sort[a->id] != sort[b->id])
1502		return sort[a->id] - sort[b->id];
1503	if (a->subseq != b->subseq) {
1504		if (power_up)
1505			return a->subseq - b->subseq;
1506		else
1507			return b->subseq - a->subseq;
1508	}
1509	if (a->reg != b->reg)
1510		return a->reg - b->reg;
1511	if (a->dapm != b->dapm)
1512		return (unsigned long)a->dapm - (unsigned long)b->dapm;
1513
1514	return 0;
1515}
1516
1517/* Insert a widget in order into a DAPM power sequence. */
1518static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1519			    struct list_head *list,
1520			    bool power_up)
1521{
1522	struct snd_soc_dapm_widget *w;
1523
1524	list_for_each_entry(w, list, power_list)
1525		if (dapm_seq_compare(new_widget, w, power_up) < 0) {
1526			list_add_tail(&new_widget->power_list, &w->power_list);
1527			return;
1528		}
1529
1530	list_add_tail(&new_widget->power_list, list);
1531}
1532
1533static void dapm_seq_check_event(struct snd_soc_card *card,
1534				 struct snd_soc_dapm_widget *w, int event)
1535{
 
1536	const char *ev_name;
1537	int power;
1538
1539	switch (event) {
1540	case SND_SOC_DAPM_PRE_PMU:
1541		ev_name = "PRE_PMU";
1542		power = 1;
1543		break;
1544	case SND_SOC_DAPM_POST_PMU:
1545		ev_name = "POST_PMU";
1546		power = 1;
1547		break;
1548	case SND_SOC_DAPM_PRE_PMD:
1549		ev_name = "PRE_PMD";
1550		power = 0;
1551		break;
1552	case SND_SOC_DAPM_POST_PMD:
1553		ev_name = "POST_PMD";
1554		power = 0;
1555		break;
1556	case SND_SOC_DAPM_WILL_PMU:
1557		ev_name = "WILL_PMU";
1558		power = 1;
1559		break;
1560	case SND_SOC_DAPM_WILL_PMD:
1561		ev_name = "WILL_PMD";
1562		power = 0;
1563		break;
1564	default:
1565		WARN(1, "Unknown event %d\n", event);
1566		return;
1567	}
1568
1569	if (w->new_power != power)
1570		return;
1571
1572	if (w->event && (w->event_flags & event)) {
1573		int ret;
1574
1575		pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
1576			w->name, ev_name);
1577		soc_dapm_async_complete(w->dapm);
1578		trace_snd_soc_dapm_widget_event_start(w, event);
1579		ret = w->event(w, NULL, event);
1580		trace_snd_soc_dapm_widget_event_done(w, event);
1581		if (ret < 0)
1582			dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
1583			       ev_name, w->name, ret);
1584	}
1585}
1586
1587/* Apply the coalesced changes from a DAPM sequence */
1588static void dapm_seq_run_coalesced(struct snd_soc_card *card,
1589				   struct list_head *pending)
1590{
1591	struct snd_soc_dapm_context *dapm;
1592	struct snd_soc_dapm_widget *w;
1593	int reg;
1594	unsigned int value = 0;
1595	unsigned int mask = 0;
 
1596
1597	w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
1598	reg = w->reg;
1599	dapm = w->dapm;
1600
1601	list_for_each_entry(w, pending, power_list) {
1602		WARN_ON(reg != w->reg || dapm != w->dapm);
1603		w->power = w->new_power;
1604
1605		mask |= w->mask << w->shift;
1606		if (w->power)
1607			value |= w->on_val << w->shift;
1608		else
1609			value |= w->off_val << w->shift;
 
 
 
 
1610
1611		pop_dbg(dapm->dev, card->pop_time,
1612			"pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1613			w->name, reg, value, mask);
1614
1615		/* Check for events */
1616		dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1617		dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
1618	}
1619
1620	if (reg >= 0) {
1621		/* Any widget will do, they should all be updating the
1622		 * same register.
1623		 */
 
 
1624
1625		pop_dbg(dapm->dev, card->pop_time,
1626			"pop test : Applying 0x%x/0x%x to %x in %dms\n",
1627			value, mask, reg, card->pop_time);
1628		pop_wait(card->pop_time);
1629		soc_dapm_update_bits(dapm, reg, mask, value);
1630	}
1631
1632	list_for_each_entry(w, pending, power_list) {
1633		dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1634		dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
1635	}
1636}
1637
1638/* Apply a DAPM power sequence.
1639 *
1640 * We walk over a pre-sorted list of widgets to apply power to.  In
1641 * order to minimise the number of writes to the device required
1642 * multiple widgets will be updated in a single write where possible.
1643 * Currently anything that requires more than a single write is not
1644 * handled.
1645 */
1646static void dapm_seq_run(struct snd_soc_card *card,
1647	struct list_head *list, int event, bool power_up)
1648{
1649	struct snd_soc_dapm_widget *w, *n;
1650	struct snd_soc_dapm_context *d;
1651	LIST_HEAD(pending);
1652	int cur_sort = -1;
1653	int cur_subseq = -1;
1654	int cur_reg = SND_SOC_NOPM;
1655	struct snd_soc_dapm_context *cur_dapm = NULL;
1656	int i;
1657	int *sort;
1658
1659	if (power_up)
1660		sort = dapm_up_seq;
1661	else
1662		sort = dapm_down_seq;
1663
1664	list_for_each_entry_safe(w, n, list, power_list) {
1665		int ret = 0;
1666
1667		/* Do we need to apply any queued changes? */
1668		if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1669		    w->dapm != cur_dapm || w->subseq != cur_subseq) {
1670			if (!list_empty(&pending))
1671				dapm_seq_run_coalesced(card, &pending);
1672
1673			if (cur_dapm && cur_dapm->component) {
1674				for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1675					if (sort[i] == cur_sort)
1676						snd_soc_component_seq_notifier(
1677							cur_dapm->component,
1678							i, cur_subseq);
1679			}
1680
1681			if (cur_dapm && w->dapm != cur_dapm)
1682				soc_dapm_async_complete(cur_dapm);
1683
1684			INIT_LIST_HEAD(&pending);
1685			cur_sort = -1;
1686			cur_subseq = INT_MIN;
1687			cur_reg = SND_SOC_NOPM;
1688			cur_dapm = NULL;
1689		}
1690
1691		switch (w->id) {
1692		case snd_soc_dapm_pre:
1693			if (!w->event)
1694				continue;
 
1695
1696			if (event == SND_SOC_DAPM_STREAM_START)
1697				ret = w->event(w,
1698					       NULL, SND_SOC_DAPM_PRE_PMU);
1699			else if (event == SND_SOC_DAPM_STREAM_STOP)
1700				ret = w->event(w,
1701					       NULL, SND_SOC_DAPM_PRE_PMD);
1702			break;
1703
1704		case snd_soc_dapm_post:
1705			if (!w->event)
1706				continue;
 
1707
1708			if (event == SND_SOC_DAPM_STREAM_START)
1709				ret = w->event(w,
1710					       NULL, SND_SOC_DAPM_POST_PMU);
1711			else if (event == SND_SOC_DAPM_STREAM_STOP)
1712				ret = w->event(w,
1713					       NULL, SND_SOC_DAPM_POST_PMD);
1714			break;
1715
1716		default:
1717			/* Queue it up for application */
1718			cur_sort = sort[w->id];
1719			cur_subseq = w->subseq;
1720			cur_reg = w->reg;
1721			cur_dapm = w->dapm;
1722			list_move(&w->power_list, &pending);
1723			break;
1724		}
1725
1726		if (ret < 0)
1727			dev_err(w->dapm->dev,
1728				"ASoC: Failed to apply widget power: %d\n", ret);
1729	}
1730
1731	if (!list_empty(&pending))
1732		dapm_seq_run_coalesced(card, &pending);
1733
1734	if (cur_dapm && cur_dapm->component) {
1735		for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1736			if (sort[i] == cur_sort)
1737				snd_soc_component_seq_notifier(
1738					cur_dapm->component,
1739					i, cur_subseq);
1740	}
1741
1742	for_each_card_dapms(card, d)
1743		soc_dapm_async_complete(d);
1744}
1745
1746static void dapm_widget_update(struct snd_soc_card *card)
1747{
1748	struct snd_soc_dapm_update *update = card->update;
1749	struct snd_soc_dapm_widget_list *wlist;
1750	struct snd_soc_dapm_widget *w = NULL;
1751	unsigned int wi;
1752	int ret;
1753
1754	if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
1755		return;
1756
1757	wlist = dapm_kcontrol_get_wlist(update->kcontrol);
1758
1759	for_each_dapm_widgets(wlist, wi, w) {
1760		if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1761			ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1762			if (ret != 0)
1763				dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
1764					   w->name, ret);
1765		}
1766	}
1767
1768	if (!w)
1769		return;
1770
1771	ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask,
1772		update->val);
1773	if (ret < 0)
1774		dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
1775			w->name, ret);
1776
1777	if (update->has_second_set) {
1778		ret = soc_dapm_update_bits(w->dapm, update->reg2,
1779					   update->mask2, update->val2);
1780		if (ret < 0)
1781			dev_err(w->dapm->dev,
1782				"ASoC: %s DAPM update failed: %d\n",
1783				w->name, ret);
1784	}
1785
1786	for_each_dapm_widgets(wlist, wi, w) {
1787		if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1788			ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1789			if (ret != 0)
1790				dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
1791					   w->name, ret);
1792		}
1793	}
1794}
1795
1796/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1797 * they're changing state.
1798 */
1799static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1800{
1801	struct snd_soc_dapm_context *d = data;
1802	int ret;
1803
1804	/* If we're off and we're not supposed to go into STANDBY */
1805	if (d->bias_level == SND_SOC_BIAS_OFF &&
1806	    d->target_bias_level != SND_SOC_BIAS_OFF) {
1807		if (d->dev && cookie)
1808			pm_runtime_get_sync(d->dev);
1809
1810		ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1811		if (ret != 0)
1812			dev_err(d->dev,
1813				"ASoC: Failed to turn on bias: %d\n", ret);
1814	}
1815
1816	/* Prepare for a transition to ON or away from ON */
1817	if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1818	     d->bias_level != SND_SOC_BIAS_ON) ||
1819	    (d->target_bias_level != SND_SOC_BIAS_ON &&
1820	     d->bias_level == SND_SOC_BIAS_ON)) {
1821		ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1822		if (ret != 0)
1823			dev_err(d->dev,
1824				"ASoC: Failed to prepare bias: %d\n", ret);
1825	}
1826}
1827
1828/* Async callback run prior to DAPM sequences - brings to their final
1829 * state.
1830 */
1831static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1832{
1833	struct snd_soc_dapm_context *d = data;
1834	int ret;
1835
1836	/* If we just powered the last thing off drop to standby bias */
1837	if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1838	    (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1839	     d->target_bias_level == SND_SOC_BIAS_OFF)) {
1840		ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1841		if (ret != 0)
1842			dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
1843				ret);
1844	}
1845
1846	/* If we're in standby and can support bias off then do that */
1847	if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1848	    d->target_bias_level == SND_SOC_BIAS_OFF) {
1849		ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1850		if (ret != 0)
1851			dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1852				ret);
1853
1854		if (d->dev && cookie)
1855			pm_runtime_put(d->dev);
1856	}
1857
1858	/* If we just powered up then move to active bias */
1859	if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1860	    d->target_bias_level == SND_SOC_BIAS_ON) {
1861		ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1862		if (ret != 0)
1863			dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
1864				ret);
1865	}
1866}
1867
1868static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1869				       bool power, bool connect)
1870{
1871	/* If a connection is being made or broken then that update
1872	 * will have marked the peer dirty, otherwise the widgets are
1873	 * not connected and this update has no impact. */
1874	if (!connect)
1875		return;
1876
1877	/* If the peer is already in the state we're moving to then we
1878	 * won't have an impact on it. */
1879	if (power != peer->power)
1880		dapm_mark_dirty(peer, "peer state change");
1881}
1882
1883static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1884				  struct list_head *up_list,
1885				  struct list_head *down_list)
1886{
1887	struct snd_soc_dapm_path *path;
1888	int power;
1889
1890	switch (w->id) {
1891	case snd_soc_dapm_pre:
1892		power = 0;
1893		goto end;
1894	case snd_soc_dapm_post:
1895		power = 1;
1896		goto end;
1897	default:
1898		break;
1899	}
1900
1901	power = dapm_widget_power_check(w);
1902
1903	if (w->power == power)
1904		return;
1905
1906	trace_snd_soc_dapm_widget_power(w, power);
1907
1908	/*
1909	 * If we changed our power state perhaps our neigbours
1910	 * changed also.
1911	 */
1912	snd_soc_dapm_widget_for_each_source_path(w, path)
1913		dapm_widget_set_peer_power(path->source, power, path->connect);
1914
1915	/*
1916	 * Supplies can't affect their outputs, only their inputs
1917	 */
1918	if (!w->is_supply)
1919		snd_soc_dapm_widget_for_each_sink_path(w, path)
1920			dapm_widget_set_peer_power(path->sink, power, path->connect);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1921
1922end:
1923	if (power)
1924		dapm_seq_insert(w, up_list, true);
1925	else
1926		dapm_seq_insert(w, down_list, false);
 
 
1927}
1928
1929static bool dapm_idle_bias_off(struct snd_soc_dapm_context *dapm)
 
 
1930{
1931	if (dapm->idle_bias_off)
1932		return true;
 
 
 
 
 
 
 
1933
1934	switch (snd_power_get_state(dapm->card->snd_card)) {
1935	case SNDRV_CTL_POWER_D3hot:
1936	case SNDRV_CTL_POWER_D3cold:
1937		return dapm->suspend_bias_off;
1938	default:
 
 
 
1939		break;
1940	}
1941
1942	return false;
1943}
1944
1945/*
1946 * Scan each dapm widget for complete audio path.
1947 * A complete path is a route that has valid endpoints i.e.:-
1948 *
1949 *  o DAC to output pin.
1950 *  o Input pin to ADC.
1951 *  o Input pin to Output pin (bypass, sidetone)
1952 *  o DAC to ADC (loopback).
1953 */
1954static int dapm_power_widgets(struct snd_soc_card *card, int event)
1955{
 
1956	struct snd_soc_dapm_widget *w;
1957	struct snd_soc_dapm_context *d;
1958	LIST_HEAD(up_list);
1959	LIST_HEAD(down_list);
1960	ASYNC_DOMAIN_EXCLUSIVE(async_domain);
1961	enum snd_soc_bias_level bias;
1962	int ret;
1963
1964	snd_soc_dapm_mutex_assert_held(card);
1965
1966	trace_snd_soc_dapm_start(card, event);
1967
1968	for_each_card_dapms(card, d) {
1969		if (dapm_idle_bias_off(d))
1970			d->target_bias_level = SND_SOC_BIAS_OFF;
1971		else
1972			d->target_bias_level = SND_SOC_BIAS_STANDBY;
1973	}
1974
1975	dapm_reset(card);
1976
1977	/* Check which widgets we need to power and store them in
1978	 * lists indicating if they should be powered up or down.  We
1979	 * only check widgets that have been flagged as dirty but note
1980	 * that new widgets may be added to the dirty list while we
1981	 * iterate.
1982	 */
1983	list_for_each_entry(w, &card->dapm_dirty, dirty) {
1984		dapm_power_one_widget(w, &up_list, &down_list);
1985	}
1986
1987	for_each_card_widgets(card, w) {
1988		switch (w->id) {
1989		case snd_soc_dapm_pre:
1990		case snd_soc_dapm_post:
1991			/* These widgets always need to be powered */
1992			break;
1993		default:
1994			list_del_init(&w->dirty);
1995			break;
1996		}
1997
1998		if (w->new_power) {
1999			d = w->dapm;
2000
2001			/* Supplies and micbiases only bring the
2002			 * context up to STANDBY as unless something
2003			 * else is active and passing audio they
2004			 * generally don't require full power.  Signal
2005			 * generators are virtual pins and have no
2006			 * power impact themselves.
2007			 */
2008			switch (w->id) {
2009			case snd_soc_dapm_siggen:
2010			case snd_soc_dapm_vmid:
2011				break;
2012			case snd_soc_dapm_supply:
2013			case snd_soc_dapm_regulator_supply:
2014			case snd_soc_dapm_pinctrl:
2015			case snd_soc_dapm_clock_supply:
2016			case snd_soc_dapm_micbias:
2017				if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
2018					d->target_bias_level = SND_SOC_BIAS_STANDBY;
2019				break;
2020			default:
2021				d->target_bias_level = SND_SOC_BIAS_ON;
2022				break;
2023			}
2024		}
2025
2026	}
2027
2028	/* Force all contexts in the card to the same bias state if
2029	 * they're not ground referenced.
2030	 */
2031	bias = SND_SOC_BIAS_OFF;
2032	for_each_card_dapms(card, d)
2033		if (d->target_bias_level > bias)
2034			bias = d->target_bias_level;
2035	for_each_card_dapms(card, d)
2036		if (!dapm_idle_bias_off(d))
2037			d->target_bias_level = bias;
2038
2039	trace_snd_soc_dapm_walk_done(card);
2040
2041	/* Run card bias changes at first */
2042	dapm_pre_sequence_async(&card->dapm, 0);
2043	/* Run other bias changes in parallel */
2044	for_each_card_dapms(card, d) {
2045		if (d != &card->dapm && d->bias_level != d->target_bias_level)
2046			async_schedule_domain(dapm_pre_sequence_async, d,
2047						&async_domain);
2048	}
2049	async_synchronize_full_domain(&async_domain);
2050
2051	list_for_each_entry(w, &down_list, power_list) {
2052		dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
2053	}
2054
2055	list_for_each_entry(w, &up_list, power_list) {
2056		dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
2057	}
2058
2059	/* Power down widgets first; try to avoid amplifying pops. */
2060	dapm_seq_run(card, &down_list, event, false);
2061
2062	dapm_widget_update(card);
2063
2064	/* Now power up. */
2065	dapm_seq_run(card, &up_list, event, true);
2066
2067	/* Run all the bias changes in parallel */
2068	for_each_card_dapms(card, d) {
2069		if (d != &card->dapm && d->bias_level != d->target_bias_level)
2070			async_schedule_domain(dapm_post_sequence_async, d,
2071						&async_domain);
2072	}
2073	async_synchronize_full_domain(&async_domain);
2074	/* Run card bias changes at last */
2075	dapm_post_sequence_async(&card->dapm, 0);
2076
2077	/* do we need to notify any clients that DAPM event is complete */
2078	for_each_card_dapms(card, d) {
2079		if (!d->component)
2080			continue;
2081
2082		ret = snd_soc_component_stream_event(d->component, event);
2083		if (ret < 0)
2084			return ret;
2085	}
2086
2087	pop_dbg(card->dev, card->pop_time,
2088		"DAPM sequencing finished, waiting %dms\n", card->pop_time);
2089	pop_wait(card->pop_time);
2090
2091	trace_snd_soc_dapm_done(card, event);
2092
2093	return 0;
2094}
2095
2096#ifdef CONFIG_DEBUG_FS
2097static ssize_t dapm_widget_power_read_file(struct file *file,
2098					   char __user *user_buf,
2099					   size_t count, loff_t *ppos)
2100{
2101	struct snd_soc_dapm_widget *w = file->private_data;
2102	enum snd_soc_dapm_direction dir, rdir;
2103	char *buf;
2104	int in, out;
2105	ssize_t ret;
2106	struct snd_soc_dapm_path *p = NULL;
2107
2108	buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2109	if (!buf)
2110		return -ENOMEM;
2111
2112	snd_soc_dapm_mutex_lock_root(w->dapm);
 
 
 
2113
2114	/* Supply widgets are not handled by is_connected_{input,output}_ep() */
2115	if (w->is_supply) {
2116		in = 0;
2117		out = 0;
2118	} else {
2119		in = is_connected_input_ep(w, NULL, NULL);
2120		out = is_connected_output_ep(w, NULL, NULL);
2121	}
2122
2123	ret = scnprintf(buf, PAGE_SIZE, "%s: %s%s  in %d out %d",
2124		       w->name, w->power ? "On" : "Off",
2125		       w->force ? " (forced)" : "", in, out);
2126
2127	if (w->reg >= 0)
2128		ret += scnprintf(buf + ret, PAGE_SIZE - ret,
2129				" - R%d(0x%x) mask 0x%x",
2130				w->reg, w->reg, w->mask << w->shift);
2131
2132	ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
2133
2134	if (w->sname)
2135		ret += scnprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
2136				w->sname,
2137				w->active ? "active" : "inactive");
2138
2139	snd_soc_dapm_for_each_direction(dir) {
2140		rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
2141		snd_soc_dapm_widget_for_each_path(w, dir, p) {
2142			if (p->connected && !p->connected(p->source, p->sink))
2143				continue;
2144
2145			if (!p->connect)
2146				continue;
2147
2148			ret += scnprintf(buf + ret, PAGE_SIZE - ret,
2149					" %s  \"%s\" \"%s\"\n",
2150					(rdir == SND_SOC_DAPM_DIR_IN) ? "in" : "out",
2151					p->name ? p->name : "static",
2152					p->node[rdir]->name);
2153		}
2154	}
 
 
 
2155
2156	snd_soc_dapm_mutex_unlock(w->dapm);
 
 
 
 
 
2157
2158	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
2159
2160	kfree(buf);
2161	return ret;
2162}
2163
2164static const struct file_operations dapm_widget_power_fops = {
2165	.open = simple_open,
2166	.read = dapm_widget_power_read_file,
2167	.llseek = default_llseek,
2168};
2169
2170static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
2171				   size_t count, loff_t *ppos)
2172{
2173	struct snd_soc_dapm_context *dapm = file->private_data;
2174	char *level;
2175
2176	switch (dapm->bias_level) {
2177	case SND_SOC_BIAS_ON:
2178		level = "On\n";
2179		break;
2180	case SND_SOC_BIAS_PREPARE:
2181		level = "Prepare\n";
2182		break;
2183	case SND_SOC_BIAS_STANDBY:
2184		level = "Standby\n";
2185		break;
2186	case SND_SOC_BIAS_OFF:
2187		level = "Off\n";
2188		break;
2189	default:
2190		WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
2191		level = "Unknown\n";
2192		break;
2193	}
2194
2195	return simple_read_from_buffer(user_buf, count, ppos, level,
2196				       strlen(level));
2197}
2198
2199static const struct file_operations dapm_bias_fops = {
2200	.open = simple_open,
2201	.read = dapm_bias_read_file,
2202	.llseek = default_llseek,
2203};
2204
2205void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2206	struct dentry *parent)
2207{
2208	if (!parent || IS_ERR(parent))
2209		return;
2210
2211	dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2212
2213	debugfs_create_file("bias_level", 0444, dapm->debugfs_dapm, dapm,
2214			    &dapm_bias_fops);
2215}
2216
2217static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2218{
2219	struct snd_soc_dapm_context *dapm = w->dapm;
2220
2221	if (!dapm->debugfs_dapm || !w->name)
2222		return;
 
2223
2224	debugfs_create_file(w->name, 0444, dapm->debugfs_dapm, w,
2225			    &dapm_widget_power_fops);
 
 
 
 
2226}
2227
2228static void dapm_debugfs_free_widget(struct snd_soc_dapm_widget *w)
2229{
2230	struct snd_soc_dapm_context *dapm = w->dapm;
 
2231
2232	if (!dapm->debugfs_dapm || !w->name)
2233		return;
2234
2235	debugfs_lookup_and_remove(w->name, dapm->debugfs_dapm);
 
 
 
 
 
 
2236}
2237
2238static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2239{
2240	debugfs_remove_recursive(dapm->debugfs_dapm);
2241	dapm->debugfs_dapm = NULL;
2242}
2243
2244#else
2245void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2246	struct dentry *parent)
2247{
2248}
2249
2250static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2251{
2252}
2253
2254static inline void dapm_debugfs_free_widget(struct snd_soc_dapm_widget *w)
2255{
2256}
2257
2258static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2259{
2260}
2261
2262#endif
2263
2264/*
2265 * soc_dapm_connect_path() - Connects or disconnects a path
2266 * @path: The path to update
2267 * @connect: The new connect state of the path. True if the path is connected,
2268 *  false if it is disconnected.
2269 * @reason: The reason why the path changed (for debugging only)
2270 */
2271static void soc_dapm_connect_path(struct snd_soc_dapm_path *path,
2272	bool connect, const char *reason)
2273{
2274	if (path->connect == connect)
2275		return;
2276
2277	path->connect = connect;
2278	dapm_mark_dirty(path->source, reason);
2279	dapm_mark_dirty(path->sink, reason);
2280	dapm_path_invalidate(path);
2281}
2282
2283/* test and update the power status of a mux widget */
2284static int soc_dapm_mux_update_power(struct snd_soc_card *card,
2285				 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
2286{
2287	struct snd_soc_dapm_path *path;
2288	int found = 0;
2289	bool connect;
2290
2291	snd_soc_dapm_mutex_assert_held(card);
 
 
 
2292
2293	/* find dapm widget path assoc with kcontrol */
2294	dapm_kcontrol_for_each_path(path, kcontrol) {
 
 
 
 
 
 
2295		found = 1;
2296		/* we now need to match the string in the enum to the path */
2297		if (e && !(strcmp(path->name, e->texts[mux])))
2298			connect = true;
2299		else
2300			connect = false;
2301
2302		soc_dapm_connect_path(path, connect, "mux update");
 
 
 
2303	}
2304
2305	if (found)
2306		dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
 
 
2307
2308	return found;
2309}
2310
2311int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
2312	struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2313	struct snd_soc_dapm_update *update)
2314{
2315	struct snd_soc_card *card = dapm->card;
2316	int ret;
2317
2318	snd_soc_dapm_mutex_lock(card);
2319	card->update = update;
2320	ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
2321	card->update = NULL;
2322	snd_soc_dapm_mutex_unlock(card);
2323	if (ret > 0)
2324		snd_soc_dpcm_runtime_update(card);
2325	return ret;
2326}
2327EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
2328
2329/* test and update the power status of a mixer or switch widget */
2330static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
2331				       struct snd_kcontrol *kcontrol,
2332				       int connect, int rconnect)
2333{
2334	struct snd_soc_dapm_path *path;
2335	int found = 0;
2336
2337	snd_soc_dapm_mutex_assert_held(card);
 
 
 
2338
2339	/* find dapm widget path assoc with kcontrol */
2340	dapm_kcontrol_for_each_path(path, kcontrol) {
2341		/*
2342		 * Ideally this function should support any number of
2343		 * paths and channels. But since kcontrols only come
2344		 * in mono and stereo variants, we are limited to 2
2345		 * channels.
2346		 *
2347		 * The following code assumes for stereo controls the
2348		 * first path (when 'found == 0') is the left channel,
2349		 * and all remaining paths (when 'found == 1') are the
2350		 * right channel.
2351		 *
2352		 * A stereo control is signified by a valid 'rconnect'
2353		 * value, either 0 for unconnected, or >= 0 for connected.
2354		 * This is chosen instead of using snd_soc_volsw_is_stereo,
2355		 * so that the behavior of snd_soc_dapm_mixer_update_power
2356		 * doesn't change even when the kcontrol passed in is
2357		 * stereo.
2358		 *
2359		 * It passes 'connect' as the path connect status for
2360		 * the left channel, and 'rconnect' for the right
2361		 * channel.
2362		 */
2363		if (found && rconnect >= 0)
2364			soc_dapm_connect_path(path, rconnect, "mixer update");
2365		else
2366			soc_dapm_connect_path(path, connect, "mixer update");
2367		found = 1;
 
 
2368	}
2369
2370	if (found)
2371		dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
 
 
2372
2373	return found;
2374}
2375
2376int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
2377	struct snd_kcontrol *kcontrol, int connect,
2378	struct snd_soc_dapm_update *update)
2379{
2380	struct snd_soc_card *card = dapm->card;
2381	int ret;
2382
2383	snd_soc_dapm_mutex_lock(card);
2384	card->update = update;
2385	ret = soc_dapm_mixer_update_power(card, kcontrol, connect, -1);
2386	card->update = NULL;
2387	snd_soc_dapm_mutex_unlock(card);
2388	if (ret > 0)
2389		snd_soc_dpcm_runtime_update(card);
2390	return ret;
2391}
2392EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
2393
2394static ssize_t dapm_widget_show_component(struct snd_soc_component *cmpnt,
2395					  char *buf, int count)
 
2396{
2397	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt);
 
2398	struct snd_soc_dapm_widget *w;
 
2399	char *state = "not set";
2400
2401	/* card won't be set for the dummy component, as a spot fix
2402	 * we're checking for that case specifically here but in future
2403	 * we will ensure that the dummy component looks like others.
2404	 */
2405	if (!cmpnt->card)
2406		return 0;
2407
2408	for_each_card_widgets(cmpnt->card, w) {
2409		if (w->dapm != dapm)
2410			continue;
2411
2412		/* only display widgets that burn power */
2413		switch (w->id) {
2414		case snd_soc_dapm_hp:
2415		case snd_soc_dapm_mic:
2416		case snd_soc_dapm_spk:
2417		case snd_soc_dapm_line:
2418		case snd_soc_dapm_micbias:
2419		case snd_soc_dapm_dac:
2420		case snd_soc_dapm_adc:
2421		case snd_soc_dapm_pga:
2422		case snd_soc_dapm_effect:
2423		case snd_soc_dapm_out_drv:
2424		case snd_soc_dapm_mixer:
2425		case snd_soc_dapm_mixer_named_ctl:
2426		case snd_soc_dapm_supply:
2427		case snd_soc_dapm_regulator_supply:
2428		case snd_soc_dapm_pinctrl:
2429		case snd_soc_dapm_clock_supply:
2430			if (w->name)
2431				count += sysfs_emit_at(buf, count, "%s: %s\n",
2432					w->name, w->power ? "On":"Off");
2433		break;
2434		default:
2435		break;
2436		}
2437	}
2438
2439	switch (snd_soc_dapm_get_bias_level(dapm)) {
2440	case SND_SOC_BIAS_ON:
2441		state = "On";
2442		break;
2443	case SND_SOC_BIAS_PREPARE:
2444		state = "Prepare";
2445		break;
2446	case SND_SOC_BIAS_STANDBY:
2447		state = "Standby";
2448		break;
2449	case SND_SOC_BIAS_OFF:
2450		state = "Off";
2451		break;
2452	}
2453	count += sysfs_emit_at(buf, count, "PM State: %s\n", state);
2454
2455	return count;
2456}
2457
2458/* show dapm widget status in sys fs */
2459static ssize_t dapm_widget_show(struct device *dev,
2460	struct device_attribute *attr, char *buf)
2461{
2462	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2463	struct snd_soc_dai *codec_dai;
2464	int i, count = 0;
2465
2466	snd_soc_dapm_mutex_lock_root(rtd->card);
2467
2468	for_each_rtd_codec_dais(rtd, i, codec_dai) {
2469		struct snd_soc_component *cmpnt = codec_dai->component;
2470
2471		count = dapm_widget_show_component(cmpnt, buf, count);
2472	}
2473
2474	snd_soc_dapm_mutex_unlock(rtd->card);
2475
2476	return count;
2477}
2478
2479static DEVICE_ATTR_RO(dapm_widget);
2480
2481struct attribute *soc_dapm_dev_attrs[] = {
2482	&dev_attr_dapm_widget.attr,
2483	NULL
2484};
2485
2486static void dapm_free_path(struct snd_soc_dapm_path *path)
2487{
2488	list_del(&path->list_node[SND_SOC_DAPM_DIR_IN]);
2489	list_del(&path->list_node[SND_SOC_DAPM_DIR_OUT]);
2490	list_del(&path->list_kcontrol);
2491	list_del(&path->list);
2492	kfree(path);
2493}
2494
2495/**
2496 * snd_soc_dapm_free_widget - Free specified widget
2497 * @w: widget to free
2498 *
2499 * Removes widget from all paths and frees memory occupied by it.
2500 */
2501void snd_soc_dapm_free_widget(struct snd_soc_dapm_widget *w)
2502{
2503	struct snd_soc_dapm_path *p, *next_p;
2504	enum snd_soc_dapm_direction dir;
2505
2506	if (!w)
2507		return;
2508
2509	list_del(&w->list);
2510	list_del(&w->dirty);
2511	/*
2512	 * remove source and sink paths associated to this widget.
2513	 * While removing the path, remove reference to it from both
2514	 * source and sink widgets so that path is removed only once.
2515	 */
2516	snd_soc_dapm_for_each_direction(dir) {
2517		snd_soc_dapm_widget_for_each_path_safe(w, dir, p, next_p)
2518			dapm_free_path(p);
2519	}
2520
2521	dapm_debugfs_free_widget(w);
2522
2523	kfree(w->kcontrols);
2524	kfree_const(w->name);
2525	kfree_const(w->sname);
2526	kfree(w);
2527}
2528EXPORT_SYMBOL_GPL(snd_soc_dapm_free_widget);
2529
2530/* free all dapm widgets and resources */
2531static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
2532{
2533	struct snd_soc_dapm_widget *w, *next_w;
 
2534
2535	for_each_card_widgets_safe(dapm->card, w, next_w) {
2536		if (w->dapm != dapm)
2537			continue;
2538		snd_soc_dapm_free_widget(w);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2539	}
2540
2541	dapm->wcache_sink	= NULL;
2542	dapm->wcache_source	= NULL;
2543}
2544
2545static struct snd_soc_dapm_widget *dapm_find_widget(
2546			struct snd_soc_dapm_context *dapm, const char *pin,
2547			bool search_other_contexts)
2548{
2549	struct snd_soc_dapm_widget *w;
2550	struct snd_soc_dapm_widget *fallback = NULL;
2551	char prefixed_pin[80];
2552	const char *pin_name;
2553	const char *prefix = soc_dapm_prefix(dapm);
2554
2555	if (prefix) {
2556		snprintf(prefixed_pin, sizeof(prefixed_pin), "%s %s",
2557			 prefix, pin);
2558		pin_name = prefixed_pin;
2559	} else {
2560		pin_name = pin;
2561	}
2562
2563	for_each_card_widgets(dapm->card, w) {
2564		if (!strcmp(w->name, pin_name)) {
2565			if (w->dapm == dapm)
2566				return w;
2567			else
2568				fallback = w;
2569		}
2570	}
2571
2572	if (search_other_contexts)
2573		return fallback;
2574
2575	return NULL;
2576}
2577
2578/*
2579 * set the DAPM pin status:
2580 * returns 1 when the value has been updated, 0 when unchanged, or a negative
2581 * error code; called from kcontrol put callback
2582 */
2583static int __snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2584				  const char *pin, int status)
2585{
2586	struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2587	int ret = 0;
2588
2589	dapm_assert_locked(dapm);
2590
2591	if (!w) {
2592		dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
2593		return -EINVAL;
2594	}
2595
2596	if (w->connected != status) {
2597		dapm_mark_dirty(w, "pin configuration");
2598		dapm_widget_invalidate_input_paths(w);
2599		dapm_widget_invalidate_output_paths(w);
2600		ret = 1;
2601	}
2602
2603	w->connected = status;
2604	if (status == 0)
2605		w->force = 0;
2606
2607	return ret;
2608}
2609
2610/*
2611 * similar as __snd_soc_dapm_set_pin(), but returns 0 when successful;
2612 * called from several API functions below
2613 */
2614static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2615				const char *pin, int status)
2616{
2617	int ret = __snd_soc_dapm_set_pin(dapm, pin, status);
2618
2619	return ret < 0 ? ret : 0;
2620}
2621
2622/**
2623 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2624 * @dapm: DAPM context
2625 *
2626 * Walks all dapm audio paths and powers widgets according to their
2627 * stream or path usage.
2628 *
2629 * Requires external locking.
2630 *
2631 * Returns 0 for success.
2632 */
2633int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2634{
 
 
2635	/*
2636	 * Suppress early reports (eg, jacks syncing their state) to avoid
2637	 * silly DAPM runs during card startup.
2638	 */
2639	if (!snd_soc_card_is_instantiated(dapm->card))
2640		return 0;
2641
2642	return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2643}
2644EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2645
2646/**
2647 * snd_soc_dapm_sync - scan and power dapm paths
2648 * @dapm: DAPM context
2649 *
2650 * Walks all dapm audio paths and powers widgets according to their
2651 * stream or path usage.
2652 *
2653 * Returns 0 for success.
2654 */
2655int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
2656{
2657	int ret;
2658
2659	snd_soc_dapm_mutex_lock(dapm);
2660	ret = snd_soc_dapm_sync_unlocked(dapm);
2661	snd_soc_dapm_mutex_unlock(dapm);
2662	return ret;
2663}
2664EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
2665
2666static int dapm_update_dai_chan(struct snd_soc_dapm_path *p,
2667				struct snd_soc_dapm_widget *w,
2668				int channels)
2669{
2670	switch (w->id) {
2671	case snd_soc_dapm_aif_out:
2672	case snd_soc_dapm_aif_in:
2673		break;
2674	default:
2675		return 0;
2676	}
2677
2678	dev_dbg(w->dapm->dev, "%s DAI route %s -> %s\n",
2679		w->channel < channels ? "Connecting" : "Disconnecting",
2680		p->source->name, p->sink->name);
2681
2682	if (w->channel < channels)
2683		soc_dapm_connect_path(p, true, "dai update");
2684	else
2685		soc_dapm_connect_path(p, false, "dai update");
2686
2687	return 0;
2688}
2689
2690static int dapm_update_dai_unlocked(struct snd_pcm_substream *substream,
2691				    struct snd_pcm_hw_params *params,
2692				    struct snd_soc_dai *dai)
2693{
2694	int dir = substream->stream;
2695	int channels = params_channels(params);
2696	struct snd_soc_dapm_path *p;
2697	struct snd_soc_dapm_widget *w;
2698	int ret;
2699
2700	w = snd_soc_dai_get_widget(dai, dir);
2701
2702	if (!w)
2703		return 0;
2704
2705	dev_dbg(dai->dev, "Update DAI routes for %s %s\n", dai->name,
2706		dir == SNDRV_PCM_STREAM_PLAYBACK ? "playback" : "capture");
2707
2708	snd_soc_dapm_widget_for_each_sink_path(w, p) {
2709		ret = dapm_update_dai_chan(p, p->sink, channels);
2710		if (ret < 0)
2711			return ret;
2712	}
2713
2714	snd_soc_dapm_widget_for_each_source_path(w, p) {
2715		ret = dapm_update_dai_chan(p, p->source, channels);
2716		if (ret < 0)
2717			return ret;
2718	}
2719
2720	return 0;
2721}
2722
2723int snd_soc_dapm_update_dai(struct snd_pcm_substream *substream,
2724			    struct snd_pcm_hw_params *params,
2725			    struct snd_soc_dai *dai)
2726{
2727	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
2728	int ret;
2729
2730	snd_soc_dapm_mutex_lock(rtd->card);
2731	ret = dapm_update_dai_unlocked(substream, params, dai);
2732	snd_soc_dapm_mutex_unlock(rtd->card);
2733
2734	return ret;
2735}
2736EXPORT_SYMBOL_GPL(snd_soc_dapm_update_dai);
2737
2738int snd_soc_dapm_widget_name_cmp(struct snd_soc_dapm_widget *widget, const char *s)
2739{
2740	struct snd_soc_component *component = snd_soc_dapm_to_component(widget->dapm);
2741	const char *wname = widget->name;
2742
2743	if (component->name_prefix)
2744		wname += strlen(component->name_prefix) + 1; /* plus space */
2745
2746	return strcmp(wname, s);
2747}
2748EXPORT_SYMBOL_GPL(snd_soc_dapm_widget_name_cmp);
2749
2750/*
2751 * dapm_update_widget_flags() - Re-compute widget sink and source flags
2752 * @w: The widget for which to update the flags
2753 *
2754 * Some widgets have a dynamic category which depends on which neighbors they
2755 * are connected to. This function update the category for these widgets.
2756 *
2757 * This function must be called whenever a path is added or removed to a widget.
2758 */
2759static void dapm_update_widget_flags(struct snd_soc_dapm_widget *w)
2760{
2761	enum snd_soc_dapm_direction dir;
2762	struct snd_soc_dapm_path *p;
2763	unsigned int ep;
2764
2765	switch (w->id) {
2766	case snd_soc_dapm_input:
2767		/* On a fully routed card an input is never a source */
2768		if (w->dapm->card->fully_routed)
2769			return;
2770		ep = SND_SOC_DAPM_EP_SOURCE;
2771		snd_soc_dapm_widget_for_each_source_path(w, p) {
2772			if (p->source->id == snd_soc_dapm_micbias ||
2773				p->source->id == snd_soc_dapm_mic ||
2774				p->source->id == snd_soc_dapm_line ||
2775				p->source->id == snd_soc_dapm_output) {
2776					ep = 0;
2777					break;
2778			}
2779		}
2780		break;
2781	case snd_soc_dapm_output:
2782		/* On a fully routed card a output is never a sink */
2783		if (w->dapm->card->fully_routed)
2784			return;
2785		ep = SND_SOC_DAPM_EP_SINK;
2786		snd_soc_dapm_widget_for_each_sink_path(w, p) {
2787			if (p->sink->id == snd_soc_dapm_spk ||
2788				p->sink->id == snd_soc_dapm_hp ||
2789				p->sink->id == snd_soc_dapm_line ||
2790				p->sink->id == snd_soc_dapm_input) {
2791					ep = 0;
2792					break;
2793			}
2794		}
2795		break;
2796	case snd_soc_dapm_line:
2797		ep = 0;
2798		snd_soc_dapm_for_each_direction(dir) {
2799			if (!list_empty(&w->edges[dir]))
2800				ep |= SND_SOC_DAPM_DIR_TO_EP(dir);
2801		}
2802		break;
2803	default:
2804		return;
2805	}
2806
2807	w->is_ep = ep;
2808}
2809
2810static int snd_soc_dapm_check_dynamic_path(struct snd_soc_dapm_context *dapm,
2811	struct snd_soc_dapm_widget *source, struct snd_soc_dapm_widget *sink,
2812	const char *control)
2813{
2814	bool dynamic_source = false;
2815	bool dynamic_sink = false;
2816
2817	if (!control)
2818		return 0;
2819
2820	switch (source->id) {
2821	case snd_soc_dapm_demux:
2822		dynamic_source = true;
2823		break;
2824	default:
2825		break;
2826	}
2827
2828	switch (sink->id) {
2829	case snd_soc_dapm_mux:
2830	case snd_soc_dapm_switch:
2831	case snd_soc_dapm_mixer:
2832	case snd_soc_dapm_mixer_named_ctl:
2833		dynamic_sink = true;
2834		break;
2835	default:
2836		break;
2837	}
2838
2839	if (dynamic_source && dynamic_sink) {
2840		dev_err(dapm->dev,
2841			"Direct connection between demux and mixer/mux not supported for path %s -> [%s] -> %s\n",
2842			source->name, control, sink->name);
2843		return -EINVAL;
2844	} else if (!dynamic_source && !dynamic_sink) {
2845		dev_err(dapm->dev,
2846			"Control not supported for path %s -> [%s] -> %s\n",
2847			source->name, control, sink->name);
2848		return -EINVAL;
2849	}
2850
2851	return 0;
2852}
2853
2854static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2855	struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2856	const char *control,
2857	int (*connected)(struct snd_soc_dapm_widget *source,
2858			 struct snd_soc_dapm_widget *sink))
2859{
2860	enum snd_soc_dapm_direction dir;
2861	struct snd_soc_dapm_path *path;
2862	int ret;
2863
2864	if (wsink->is_supply && !wsource->is_supply) {
2865		dev_err(dapm->dev,
2866			"Connecting non-supply widget to supply widget is not supported (%s -> %s)\n",
2867			wsource->name, wsink->name);
2868		return -EINVAL;
2869	}
2870
2871	if (connected && !wsource->is_supply) {
2872		dev_err(dapm->dev,
2873			"connected() callback only supported for supply widgets (%s -> %s)\n",
2874			wsource->name, wsink->name);
2875		return -EINVAL;
2876	}
2877
2878	if (wsource->is_supply && control) {
2879		dev_err(dapm->dev,
2880			"Conditional paths are not supported for supply widgets (%s -> [%s] -> %s)\n",
2881			wsource->name, control, wsink->name);
2882		return -EINVAL;
2883	}
2884
2885	ret = snd_soc_dapm_check_dynamic_path(dapm, wsource, wsink, control);
2886	if (ret)
2887		return ret;
2888
2889	path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2890	if (!path)
2891		return -ENOMEM;
2892
2893	path->node[SND_SOC_DAPM_DIR_IN] = wsource;
2894	path->node[SND_SOC_DAPM_DIR_OUT] = wsink;
2895
2896	path->connected = connected;
2897	INIT_LIST_HEAD(&path->list);
2898	INIT_LIST_HEAD(&path->list_kcontrol);
2899
2900	if (wsource->is_supply || wsink->is_supply)
2901		path->is_supply = 1;
2902
2903	/* connect static paths */
2904	if (control == NULL) {
2905		path->connect = 1;
2906	} else {
2907		switch (wsource->id) {
2908		case snd_soc_dapm_demux:
2909			ret = dapm_connect_mux(dapm, path, control, wsource);
2910			if (ret)
2911				goto err;
2912			break;
2913		default:
2914			break;
2915		}
2916
2917		switch (wsink->id) {
2918		case snd_soc_dapm_mux:
2919			ret = dapm_connect_mux(dapm, path, control, wsink);
2920			if (ret != 0)
2921				goto err;
2922			break;
2923		case snd_soc_dapm_switch:
2924		case snd_soc_dapm_mixer:
2925		case snd_soc_dapm_mixer_named_ctl:
2926			ret = dapm_connect_mixer(dapm, path, control);
2927			if (ret != 0)
2928				goto err;
2929			break;
2930		default:
2931			break;
2932		}
2933	}
2934
2935	list_add(&path->list, &dapm->card->paths);
2936
2937	snd_soc_dapm_for_each_direction(dir)
2938		list_add(&path->list_node[dir], &path->node[dir]->edges[dir]);
2939
2940	snd_soc_dapm_for_each_direction(dir) {
2941		dapm_update_widget_flags(path->node[dir]);
2942		dapm_mark_dirty(path->node[dir], "Route added");
2943	}
2944
2945	if (snd_soc_card_is_instantiated(dapm->card) && path->connect)
2946		dapm_path_invalidate(path);
2947
2948	return 0;
2949err:
2950	kfree(path);
2951	return ret;
2952}
2953
2954static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
2955				  const struct snd_soc_dapm_route *route)
2956{
 
2957	struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2958	struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2959	const char *sink;
 
2960	const char *source;
2961	char prefixed_sink[80];
2962	char prefixed_source[80];
2963	const char *prefix;
2964	unsigned int sink_ref = 0;
2965	unsigned int source_ref = 0;
2966	int ret;
2967
2968	prefix = soc_dapm_prefix(dapm);
2969	if (prefix) {
2970		snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2971			 prefix, route->sink);
2972		sink = prefixed_sink;
2973		snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2974			 prefix, route->source);
2975		source = prefixed_source;
2976	} else {
2977		sink = route->sink;
2978		source = route->source;
2979	}
2980
2981	wsource	= dapm_wcache_lookup(dapm->wcache_source, source);
2982	wsink	= dapm_wcache_lookup(dapm->wcache_sink,   sink);
2983
2984	if (wsink && wsource)
2985		goto skip_search;
2986
2987	/*
2988	 * find src and dest widgets over all widgets but favor a widget from
2989	 * current DAPM context
2990	 */
2991	for_each_card_widgets(dapm->card, w) {
2992		if (!wsink && !(strcmp(w->name, sink))) {
2993			wtsink = w;
2994			if (w->dapm == dapm) {
2995				wsink = w;
2996				if (wsource)
2997					break;
2998			}
2999			sink_ref++;
3000			if (sink_ref > 1)
3001				dev_warn(dapm->dev,
3002					"ASoC: sink widget %s overwritten\n",
3003					w->name);
3004			continue;
3005		}
3006		if (!wsource && !(strcmp(w->name, source))) {
3007			wtsource = w;
3008			if (w->dapm == dapm) {
3009				wsource = w;
3010				if (wsink)
3011					break;
3012			}
3013			source_ref++;
3014			if (source_ref > 1)
3015				dev_warn(dapm->dev,
3016					"ASoC: source widget %s overwritten\n",
3017					w->name);
3018		}
3019	}
3020	/* use widget from another DAPM context if not found from this */
3021	if (!wsink)
3022		wsink = wtsink;
3023	if (!wsource)
3024		wsource = wtsource;
3025
3026	ret = -ENODEV;
3027	if (!wsource)
3028		goto err;
3029	if (!wsink)
3030		goto err;
3031
3032skip_search:
3033	/* update cache */
3034	dapm->wcache_sink	= wsink;
3035	dapm->wcache_source	= wsource;
3036
3037	ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
3038		route->connected);
3039err:
3040	if (ret)
3041		dev_err(dapm->dev, "ASoC: Failed to add route %s%s -%s%s%s> %s%s\n",
3042			source, !wsource ? "(*)" : "",
3043			!route->control ? "" : "> [",
3044			!route->control ? "" : route->control,
3045			!route->control ? "" : "] -",
3046			sink,  !wsink ? "(*)" : "");
3047	return ret;
3048}
3049
3050static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
3051				  const struct snd_soc_dapm_route *route)
3052{
3053	struct snd_soc_dapm_path *path, *p;
3054	const char *sink;
3055	const char *source;
3056	char prefixed_sink[80];
3057	char prefixed_source[80];
3058	const char *prefix;
3059
3060	if (route->control) {
3061		dev_err(dapm->dev,
3062			"ASoC: Removal of routes with controls not supported\n");
3063		return -EINVAL;
 
 
 
 
 
 
 
 
 
 
3064	}
3065
3066	prefix = soc_dapm_prefix(dapm);
3067	if (prefix) {
3068		snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
3069			 prefix, route->sink);
3070		sink = prefixed_sink;
3071		snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
3072			 prefix, route->source);
3073		source = prefixed_source;
3074	} else {
3075		sink = route->sink;
3076		source = route->source;
3077	}
3078
3079	path = NULL;
3080	list_for_each_entry(p, &dapm->card->paths, list) {
3081		if (strcmp(p->source->name, source) != 0)
3082			continue;
3083		if (strcmp(p->sink->name, sink) != 0)
3084			continue;
3085		path = p;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3086		break;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3087	}
3088
3089	if (path) {
3090		struct snd_soc_dapm_widget *wsource = path->source;
3091		struct snd_soc_dapm_widget *wsink = path->sink;
3092
3093		dapm_mark_dirty(wsource, "Route removed");
3094		dapm_mark_dirty(wsink, "Route removed");
3095		if (path->connect)
3096			dapm_path_invalidate(path);
3097
3098		dapm_free_path(path);
3099
3100		/* Update any path related flags */
3101		dapm_update_widget_flags(wsource);
3102		dapm_update_widget_flags(wsink);
3103	} else {
3104		dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
3105			 source, sink);
3106	}
3107
3108	return 0;
 
 
 
 
 
 
3109}
3110
3111/**
3112 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
3113 * @dapm: DAPM context
3114 * @route: audio routes
3115 * @num: number of routes
3116 *
3117 * Connects 2 dapm widgets together via a named audio path. The sink is
3118 * the widget receiving the audio signal, whilst the source is the sender
3119 * of the audio signal.
3120 *
3121 * Returns 0 for success else error. On error all resources can be freed
3122 * with a call to snd_soc_card_free().
3123 */
3124int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
3125			    const struct snd_soc_dapm_route *route, int num)
3126{
3127	int i, ret = 0;
3128
3129	snd_soc_dapm_mutex_lock(dapm);
3130	for (i = 0; i < num; i++) {
3131		int r = snd_soc_dapm_add_route(dapm, route);
3132		if (r < 0)
3133			ret = r;
 
 
 
3134		route++;
3135	}
3136	snd_soc_dapm_mutex_unlock(dapm);
3137
3138	return ret;
3139}
3140EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
3141
3142/**
3143 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
3144 * @dapm: DAPM context
3145 * @route: audio routes
3146 * @num: number of routes
3147 *
3148 * Removes routes from the DAPM context.
3149 */
3150int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
3151			    const struct snd_soc_dapm_route *route, int num)
3152{
3153	int i;
3154
3155	snd_soc_dapm_mutex_lock(dapm);
3156	for (i = 0; i < num; i++) {
3157		snd_soc_dapm_del_route(dapm, route);
3158		route++;
3159	}
3160	snd_soc_dapm_mutex_unlock(dapm);
3161
3162	return 0;
3163}
3164EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
3165
3166static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
3167				   const struct snd_soc_dapm_route *route)
3168{
3169	struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
3170							      route->source,
3171							      true);
3172	struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
3173							    route->sink,
3174							    true);
3175	struct snd_soc_dapm_path *path;
3176	int count = 0;
3177
3178	if (!source) {
3179		dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
3180			route->source);
3181		return -ENODEV;
3182	}
3183
3184	if (!sink) {
3185		dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
3186			route->sink);
3187		return -ENODEV;
3188	}
3189
3190	if (route->control || route->connected)
3191		dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
3192			 route->source, route->sink);
3193
3194	snd_soc_dapm_widget_for_each_sink_path(source, path) {
3195		if (path->sink == sink) {
3196			path->weak = 1;
3197			count++;
3198		}
3199	}
3200
3201	if (count == 0)
3202		dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
3203			route->source, route->sink);
3204	if (count > 1)
3205		dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
3206			 count, route->source, route->sink);
3207
3208	return 0;
3209}
3210
3211/**
3212 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
3213 * @dapm: DAPM context
3214 * @route: audio routes
3215 * @num: number of routes
3216 *
3217 * Mark existing routes matching those specified in the passed array
3218 * as being weak, meaning that they are ignored for the purpose of
3219 * power decisions.  The main intended use case is for sidetone paths
3220 * which couple audio between other independent paths if they are both
3221 * active in order to make the combination work better at the user
3222 * level but which aren't intended to be "used".
3223 *
3224 * Note that CODEC drivers should not use this as sidetone type paths
3225 * can frequently also be used as bypass paths.
3226 */
3227int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
3228			     const struct snd_soc_dapm_route *route, int num)
3229{
3230	int i;
3231	int ret = 0;
3232
3233	snd_soc_dapm_mutex_lock_root(dapm);
3234	for (i = 0; i < num; i++) {
3235		int err = snd_soc_dapm_weak_route(dapm, route);
3236		if (err)
3237			ret = err;
3238		route++;
3239	}
3240	snd_soc_dapm_mutex_unlock(dapm);
3241
3242	return ret;
3243}
3244EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
3245
3246/**
3247 * snd_soc_dapm_new_widgets - add new dapm widgets
3248 * @card: card to be checked for new dapm widgets
3249 *
3250 * Checks the codec for any new dapm widgets and creates them if found.
3251 *
3252 * Returns 0 for success.
3253 */
3254int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
3255{
3256	struct snd_soc_dapm_widget *w;
3257	unsigned int val;
3258
3259	snd_soc_dapm_mutex_lock_root(card);
3260
3261	for_each_card_widgets(card, w)
3262	{
3263		if (w->new)
3264			continue;
3265
3266		if (w->num_kcontrols) {
3267			w->kcontrols = kcalloc(w->num_kcontrols,
3268						sizeof(struct snd_kcontrol *),
3269						GFP_KERNEL);
3270			if (!w->kcontrols) {
3271				snd_soc_dapm_mutex_unlock(card);
3272				return -ENOMEM;
3273			}
3274		}
3275
3276		switch(w->id) {
3277		case snd_soc_dapm_switch:
3278		case snd_soc_dapm_mixer:
3279		case snd_soc_dapm_mixer_named_ctl:
3280			dapm_new_mixer(w);
3281			break;
3282		case snd_soc_dapm_mux:
3283		case snd_soc_dapm_demux:
 
3284			dapm_new_mux(w);
3285			break;
3286		case snd_soc_dapm_pga:
3287		case snd_soc_dapm_effect:
3288		case snd_soc_dapm_out_drv:
3289			dapm_new_pga(w);
3290			break;
3291		case snd_soc_dapm_dai_link:
3292			dapm_new_dai_link(w);
3293			break;
3294		default:
3295			break;
3296		}
3297
3298		/* Read the initial power state from the device */
3299		if (w->reg >= 0) {
3300			val = soc_dapm_read(w->dapm, w->reg);
3301			val = val >> w->shift;
3302			val &= w->mask;
3303			if (val == w->on_val)
 
 
3304				w->power = 1;
3305		}
3306
3307		w->new = 1;
3308
3309		dapm_mark_dirty(w, "new widget");
3310		dapm_debugfs_add_widget(w);
3311	}
3312
3313	dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
3314	snd_soc_dapm_mutex_unlock(card);
3315	return 0;
3316}
3317EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
3318
3319/**
3320 * snd_soc_dapm_get_volsw - dapm mixer get callback
3321 * @kcontrol: mixer control
3322 * @ucontrol: control element information
3323 *
3324 * Callback to get the value of a dapm mixer control.
3325 *
3326 * Returns 0 for success.
3327 */
3328int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
3329	struct snd_ctl_elem_value *ucontrol)
3330{
3331	struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
 
3332	struct soc_mixer_control *mc =
3333		(struct soc_mixer_control *)kcontrol->private_value;
3334	int reg = mc->reg;
3335	unsigned int shift = mc->shift;
 
3336	int max = mc->max;
3337	unsigned int width = fls(max);
3338	unsigned int mask = (1 << fls(max)) - 1;
3339	unsigned int invert = mc->invert;
3340	unsigned int reg_val, val, rval = 0;
3341
3342	snd_soc_dapm_mutex_lock(dapm);
3343	if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
3344		reg_val = soc_dapm_read(dapm, reg);
3345		val = (reg_val >> shift) & mask;
3346
3347		if (reg != mc->rreg)
3348			reg_val = soc_dapm_read(dapm, mc->rreg);
3349
3350		if (snd_soc_volsw_is_stereo(mc))
3351			rval = (reg_val >> mc->rshift) & mask;
3352	} else {
3353		reg_val = dapm_kcontrol_get_value(kcontrol);
3354		val = reg_val & mask;
3355
3356		if (snd_soc_volsw_is_stereo(mc))
3357			rval = (reg_val >> width) & mask;
3358	}
3359	snd_soc_dapm_mutex_unlock(dapm);
3360
3361	if (invert)
3362		ucontrol->value.integer.value[0] = max - val;
3363	else
3364		ucontrol->value.integer.value[0] = val;
3365
3366	if (snd_soc_volsw_is_stereo(mc)) {
3367		if (invert)
3368			ucontrol->value.integer.value[1] = max - rval;
3369		else
3370			ucontrol->value.integer.value[1] = rval;
 
 
 
 
 
 
3371	}
3372
3373	return 0;
3374}
3375EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
3376
3377/**
3378 * snd_soc_dapm_put_volsw - dapm mixer set callback
3379 * @kcontrol: mixer control
3380 * @ucontrol: control element information
3381 *
3382 * Callback to set the value of a dapm mixer control.
3383 *
3384 * Returns 0 for success.
3385 */
3386int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
3387	struct snd_ctl_elem_value *ucontrol)
3388{
3389	struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3390	struct snd_soc_card *card = dapm->card;
 
 
3391	struct soc_mixer_control *mc =
3392		(struct soc_mixer_control *)kcontrol->private_value;
3393	int reg = mc->reg;
3394	unsigned int shift = mc->shift;
3395	int max = mc->max;
3396	unsigned int width = fls(max);
3397	unsigned int mask = (1 << width) - 1;
3398	unsigned int invert = mc->invert;
3399	unsigned int val, rval = 0;
3400	int connect, rconnect = -1, change, reg_change = 0;
3401	struct snd_soc_dapm_update update = {};
3402	int ret = 0;
3403
3404	val = (ucontrol->value.integer.value[0] & mask);
3405	connect = !!val;
3406
3407	if (invert)
3408		val = max - val;
 
 
3409
3410	if (snd_soc_volsw_is_stereo(mc)) {
3411		rval = (ucontrol->value.integer.value[1] & mask);
3412		rconnect = !!rval;
3413		if (invert)
3414			rval = max - rval;
3415	}
3416
3417	snd_soc_dapm_mutex_lock(card);
 
 
 
 
 
 
 
3418
3419	/* This assumes field width < (bits in unsigned int / 2) */
3420	if (width > sizeof(unsigned int) * 8 / 2)
3421		dev_warn(dapm->dev,
3422			 "ASoC: control %s field width limit exceeded\n",
3423			 kcontrol->id.name);
3424	change = dapm_kcontrol_set_value(kcontrol, val | (rval << width));
3425
3426	if (reg != SND_SOC_NOPM) {
3427		val = val << shift;
3428		rval = rval << mc->rshift;
3429
3430		reg_change = soc_dapm_test_bits(dapm, reg, mask << shift, val);
3431
3432		if (snd_soc_volsw_is_stereo(mc))
3433			reg_change |= soc_dapm_test_bits(dapm, mc->rreg,
3434							 mask << mc->rshift,
3435							 rval);
3436	}
3437
3438	if (change || reg_change) {
3439		if (reg_change) {
3440			if (snd_soc_volsw_is_stereo(mc)) {
3441				update.has_second_set = true;
3442				update.reg2 = mc->rreg;
3443				update.mask2 = mask << mc->rshift;
3444				update.val2 = rval;
3445			}
3446			update.kcontrol = kcontrol;
 
3447			update.reg = reg;
3448			update.mask = mask << shift;
3449			update.val = val;
3450			card->update = &update;
3451		}
3452
3453		ret = soc_dapm_mixer_update_power(card, kcontrol, connect,
3454						  rconnect);
3455
3456		card->update = NULL;
 
3457	}
3458
3459	snd_soc_dapm_mutex_unlock(card);
3460
3461	if (ret > 0)
3462		snd_soc_dpcm_runtime_update(card);
3463
3464	return change;
3465}
3466EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
3467
3468/**
3469 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
3470 * @kcontrol: mixer control
3471 * @ucontrol: control element information
3472 *
3473 * Callback to get the value of a dapm enumerated double mixer control.
3474 *
3475 * Returns 0 for success.
3476 */
3477int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
3478	struct snd_ctl_elem_value *ucontrol)
3479{
3480	struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
 
3481	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3482	unsigned int reg_val, val;
3483
3484	snd_soc_dapm_mutex_lock(dapm);
3485	if (e->reg != SND_SOC_NOPM && dapm_kcontrol_is_powered(kcontrol)) {
3486		reg_val = soc_dapm_read(dapm, e->reg);
3487	} else {
3488		reg_val = dapm_kcontrol_get_value(kcontrol);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3489	}
3490	snd_soc_dapm_mutex_unlock(dapm);
3491
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3492	val = (reg_val >> e->shift_l) & e->mask;
3493	ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
 
 
 
 
3494	if (e->shift_l != e->shift_r) {
3495		val = (reg_val >> e->shift_r) & e->mask;
3496		val = snd_soc_enum_val_to_item(e, val);
3497		ucontrol->value.enumerated.item[1] = val;
 
 
 
3498	}
3499
3500	return 0;
3501}
3502EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
3503
3504/**
3505 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
 
3506 * @kcontrol: mixer control
3507 * @ucontrol: control element information
3508 *
3509 * Callback to set the value of a dapm enumerated double mixer control.
 
 
 
3510 *
3511 * Returns 0 for success.
3512 */
3513int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
3514	struct snd_ctl_elem_value *ucontrol)
3515{
3516	struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3517	struct snd_soc_card *card = dapm->card;
 
 
3518	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3519	unsigned int *item = ucontrol->value.enumerated.item;
3520	unsigned int val, change, reg_change = 0;
3521	unsigned int mask;
3522	struct snd_soc_dapm_update update = {};
3523	int ret = 0;
3524
3525	if (item[0] >= e->items)
3526		return -EINVAL;
3527
3528	val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
3529	mask = e->mask << e->shift_l;
3530	if (e->shift_l != e->shift_r) {
3531		if (item[1] > e->items)
3532			return -EINVAL;
3533		val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
3534		mask |= e->mask << e->shift_r;
3535	}
3536
3537	snd_soc_dapm_mutex_lock(card);
3538
3539	change = dapm_kcontrol_set_value(kcontrol, val);
 
 
 
3540
3541	if (e->reg != SND_SOC_NOPM)
3542		reg_change = soc_dapm_test_bits(dapm, e->reg, mask, val);
3543
3544	if (change || reg_change) {
3545		if (reg_change) {
3546			update.kcontrol = kcontrol;
 
3547			update.reg = e->reg;
3548			update.mask = mask;
3549			update.val = val;
3550			card->update = &update;
3551		}
3552
3553		ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
3554
3555		card->update = NULL;
 
3556	}
3557
3558	snd_soc_dapm_mutex_unlock(card);
3559
3560	if (ret > 0)
3561		snd_soc_dpcm_runtime_update(card);
3562
3563	return change;
3564}
3565EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
3566
3567/**
3568 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3569 *
3570 * @kcontrol: mixer control
3571 * @uinfo: control element information
3572 *
3573 * Callback to provide information about a pin switch control.
3574 */
3575int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3576				 struct snd_ctl_elem_info *uinfo)
3577{
3578	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3579	uinfo->count = 1;
3580	uinfo->value.integer.min = 0;
3581	uinfo->value.integer.max = 1;
3582
3583	return 0;
3584}
3585EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3586
3587/**
3588 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3589 *
3590 * @kcontrol: mixer control
3591 * @ucontrol: Value
3592 */
3593int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3594				struct snd_ctl_elem_value *ucontrol)
3595{
3596	struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3597	const char *pin = (const char *)kcontrol->private_value;
3598
3599	snd_soc_dapm_mutex_lock(card);
3600
3601	ucontrol->value.integer.value[0] =
3602		snd_soc_dapm_get_pin_status(&card->dapm, pin);
3603
3604	snd_soc_dapm_mutex_unlock(card);
3605
3606	return 0;
3607}
3608EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3609
3610/**
3611 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3612 *
3613 * @kcontrol: mixer control
3614 * @ucontrol: Value
3615 */
3616int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3617				struct snd_ctl_elem_value *ucontrol)
3618{
3619	struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3620	const char *pin = (const char *)kcontrol->private_value;
3621	int ret;
3622
3623	snd_soc_dapm_mutex_lock(card);
3624	ret = __snd_soc_dapm_set_pin(&card->dapm, pin,
3625				     !!ucontrol->value.integer.value[0]);
3626	snd_soc_dapm_mutex_unlock(card);
 
 
 
 
3627
3628	snd_soc_dapm_sync(&card->dapm);
3629	return ret;
3630}
3631EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3632
3633struct snd_soc_dapm_widget *
3634snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
3635			 const struct snd_soc_dapm_widget *widget)
3636{
3637	enum snd_soc_dapm_direction dir;
3638	struct snd_soc_dapm_widget *w;
3639	int ret = -ENOMEM;
 
3640
3641	w = dapm_cnew_widget(widget, soc_dapm_prefix(dapm));
3642	if (!w)
3643		goto cnew_failed;
3644
3645	switch (w->id) {
3646	case snd_soc_dapm_regulator_supply:
3647		w->regulator = devm_regulator_get(dapm->dev, widget->name);
3648		if (IS_ERR(w->regulator)) {
3649			ret = PTR_ERR(w->regulator);
3650			goto request_failed;
3651		}
3652
3653		if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
3654			ret = regulator_allow_bypass(w->regulator, true);
3655			if (ret != 0)
3656				dev_warn(dapm->dev,
3657					 "ASoC: Failed to bypass %s: %d\n",
3658					 w->name, ret);
3659		}
3660		break;
3661	case snd_soc_dapm_pinctrl:
3662		w->pinctrl = devm_pinctrl_get(dapm->dev);
3663		if (IS_ERR(w->pinctrl)) {
3664			ret = PTR_ERR(w->pinctrl);
3665			goto request_failed;
3666		}
3667
3668		/* set to sleep_state when initializing */
3669		dapm_pinctrl_event(w, NULL, SND_SOC_DAPM_POST_PMD);
3670		break;
3671	case snd_soc_dapm_clock_supply:
3672		w->clk = devm_clk_get(dapm->dev, widget->name);
3673		if (IS_ERR(w->clk)) {
3674			ret = PTR_ERR(w->clk);
3675			goto request_failed;
3676		}
3677		break;
3678	default:
3679		break;
3680	}
3681
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3682	switch (w->id) {
3683	case snd_soc_dapm_mic:
3684		w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3685		w->power_check = dapm_generic_check_power;
3686		break;
3687	case snd_soc_dapm_input:
3688		if (!dapm->card->fully_routed)
3689			w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3690		w->power_check = dapm_generic_check_power;
3691		break;
3692	case snd_soc_dapm_spk:
3693	case snd_soc_dapm_hp:
3694		w->is_ep = SND_SOC_DAPM_EP_SINK;
3695		w->power_check = dapm_generic_check_power;
3696		break;
3697	case snd_soc_dapm_output:
3698		if (!dapm->card->fully_routed)
3699			w->is_ep = SND_SOC_DAPM_EP_SINK;
3700		w->power_check = dapm_generic_check_power;
3701		break;
3702	case snd_soc_dapm_vmid:
3703	case snd_soc_dapm_siggen:
3704		w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3705		w->power_check = dapm_always_on_check_power;
3706		break;
3707	case snd_soc_dapm_sink:
3708		w->is_ep = SND_SOC_DAPM_EP_SINK;
3709		w->power_check = dapm_always_on_check_power;
3710		break;
3711
3712	case snd_soc_dapm_mux:
3713	case snd_soc_dapm_demux:
3714	case snd_soc_dapm_switch:
3715	case snd_soc_dapm_mixer:
3716	case snd_soc_dapm_mixer_named_ctl:
3717	case snd_soc_dapm_adc:
3718	case snd_soc_dapm_aif_out:
 
 
3719	case snd_soc_dapm_dac:
3720	case snd_soc_dapm_aif_in:
 
 
3721	case snd_soc_dapm_pga:
3722	case snd_soc_dapm_buffer:
3723	case snd_soc_dapm_scheduler:
3724	case snd_soc_dapm_effect:
3725	case snd_soc_dapm_src:
3726	case snd_soc_dapm_asrc:
3727	case snd_soc_dapm_encoder:
3728	case snd_soc_dapm_decoder:
3729	case snd_soc_dapm_out_drv:
 
 
3730	case snd_soc_dapm_micbias:
 
 
 
3731	case snd_soc_dapm_line:
3732	case snd_soc_dapm_dai_link:
3733	case snd_soc_dapm_dai_out:
3734	case snd_soc_dapm_dai_in:
3735		w->power_check = dapm_generic_check_power;
3736		break;
3737	case snd_soc_dapm_supply:
3738	case snd_soc_dapm_regulator_supply:
3739	case snd_soc_dapm_pinctrl:
3740	case snd_soc_dapm_clock_supply:
3741	case snd_soc_dapm_kcontrol:
3742		w->is_supply = 1;
3743		w->power_check = dapm_supply_check_power;
3744		break;
 
 
 
3745	default:
3746		w->power_check = dapm_always_on_check_power;
3747		break;
3748	}
3749
 
3750	w->dapm = dapm;
 
 
 
 
3751	INIT_LIST_HEAD(&w->list);
3752	INIT_LIST_HEAD(&w->dirty);
3753	/* see for_each_card_widgets */
3754	list_add_tail(&w->list, &dapm->card->widgets);
3755
3756	snd_soc_dapm_for_each_direction(dir) {
3757		INIT_LIST_HEAD(&w->edges[dir]);
3758		w->endpoints[dir] = -1;
3759	}
3760
3761	/* machine layer sets up unconnected pins and insertions */
3762	w->connected = 1;
3763	return w;
3764
3765request_failed:
3766	dev_err_probe(dapm->dev, ret, "ASoC: Failed to request %s\n",
3767		      w->name);
3768	kfree_const(w->name);
3769	kfree_const(w->sname);
3770	kfree(w);
3771cnew_failed:
3772	return ERR_PTR(ret);
3773}
3774
3775/**
3776 * snd_soc_dapm_new_control - create new dapm control
3777 * @dapm: DAPM context
3778 * @widget: widget template
3779 *
3780 * Creates new DAPM control based upon a template.
3781 *
3782 * Returns a widget pointer on success or an error pointer on failure
3783 */
3784struct snd_soc_dapm_widget *
3785snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3786			 const struct snd_soc_dapm_widget *widget)
3787{
3788	struct snd_soc_dapm_widget *w;
3789
3790	snd_soc_dapm_mutex_lock(dapm);
3791	w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3792	snd_soc_dapm_mutex_unlock(dapm);
3793
3794	return w;
3795}
3796EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
3797
3798/**
3799 * snd_soc_dapm_new_controls - create new dapm controls
3800 * @dapm: DAPM context
3801 * @widget: widget array
3802 * @num: number of widgets
3803 *
3804 * Creates new DAPM controls based upon the templates.
3805 *
3806 * Returns 0 for success else error.
3807 */
3808int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
3809	const struct snd_soc_dapm_widget *widget,
3810	int num)
3811{
 
3812	int i;
3813	int ret = 0;
3814
3815	snd_soc_dapm_mutex_lock_root(dapm);
3816	for (i = 0; i < num; i++) {
3817		struct snd_soc_dapm_widget *w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3818		if (IS_ERR(w)) {
3819			ret = PTR_ERR(w);
 
 
 
3820			break;
3821		}
3822		widget++;
3823	}
3824	snd_soc_dapm_mutex_unlock(dapm);
3825	return ret;
3826}
3827EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3828
3829static int
3830snd_soc_dai_link_event_pre_pmu(struct snd_soc_dapm_widget *w,
3831			       struct snd_pcm_substream *substream)
3832{
3833	struct snd_soc_dapm_path *path;
3834	struct snd_soc_dai *source, *sink;
3835	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
 
3836	struct snd_pcm_hw_params *params = NULL;
3837	const struct snd_soc_pcm_stream *config = NULL;
3838	struct snd_pcm_runtime *runtime = NULL;
3839	unsigned int fmt;
3840	int ret = 0;
3841
3842	/*
3843	 * NOTE
3844	 *
3845	 * snd_pcm_hw_params is quite large (608 bytes on arm64) and is
3846	 * starting to get a bit excessive for allocation on the stack,
3847	 * especially when you're building with some of the KASAN type
3848	 * stuff that increases stack usage.
3849	 * So, we use kzalloc()/kfree() for params in this function.
3850	 */
3851	params = kzalloc(sizeof(*params), GFP_KERNEL);
3852	if (!params)
3853		return -ENOMEM;
3854
3855	runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
3856	if (!runtime) {
3857		ret = -ENOMEM;
3858		goto out;
3859	}
3860
3861	substream->runtime = runtime;
3862
3863	substream->stream = SNDRV_PCM_STREAM_CAPTURE;
3864	snd_soc_dapm_widget_for_each_source_path(w, path) {
3865		source = path->source->priv;
3866
3867		ret = snd_soc_dai_startup(source, substream);
3868		if (ret < 0)
3869			goto out;
3870
3871		snd_soc_dai_activate(source, substream->stream);
3872	}
3873
3874	substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
3875	snd_soc_dapm_widget_for_each_sink_path(w, path) {
3876		sink = path->sink->priv;
3877
3878		ret = snd_soc_dai_startup(sink, substream);
3879		if (ret < 0)
3880			goto out;
3881
3882		snd_soc_dai_activate(sink, substream->stream);
3883	}
 
 
 
 
 
 
 
3884
3885	substream->hw_opened = 1;
3886
3887	/*
3888	 * Note: getting the config after .startup() gives a chance to
3889	 * either party on the link to alter the configuration if
3890	 * necessary
3891	 */
3892	config = rtd->dai_link->c2c_params + rtd->c2c_params_select;
3893	if (!config) {
3894		dev_err(w->dapm->dev, "ASoC: link config missing\n");
3895		ret = -EINVAL;
3896		goto out;
3897	}
3898
3899	/* Be a little careful as we don't want to overflow the mask array */
3900	if (!config->formats) {
3901		dev_warn(w->dapm->dev, "ASoC: Invalid format was specified\n");
 
 
 
 
 
3902
3903		ret = -EINVAL;
 
 
 
3904		goto out;
3905	}
3906
3907	fmt = ffs(config->formats) - 1;
3908
3909	snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
 
3910	hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
3911		config->rate_min;
3912	hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
3913		config->rate_max;
 
3914	hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
3915		= config->channels_min;
3916	hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
3917		= config->channels_max;
3918
3919	substream->stream = SNDRV_PCM_STREAM_CAPTURE;
3920	snd_soc_dapm_widget_for_each_source_path(w, path) {
3921		source = path->source->priv;
3922
3923		ret = snd_soc_dai_hw_params(source, substream, params);
3924		if (ret < 0)
3925			goto out;
3926
3927		dapm_update_dai_unlocked(substream, params, source);
3928	}
3929
3930	substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
3931	snd_soc_dapm_widget_for_each_sink_path(w, path) {
3932		sink = path->sink->priv;
3933
3934		ret = snd_soc_dai_hw_params(sink, substream, params);
3935		if (ret < 0)
3936			goto out;
3937
3938		dapm_update_dai_unlocked(substream, params, sink);
3939	}
3940
3941	runtime->format = params_format(params);
3942	runtime->subformat = params_subformat(params);
3943	runtime->channels = params_channels(params);
3944	runtime->rate = params_rate(params);
3945
3946out:
3947	/* see above NOTE */
3948	kfree(params);
3949
3950	return ret;
3951}
3952
3953static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3954				  struct snd_kcontrol *kcontrol, int event)
3955{
3956	struct snd_soc_dapm_path *path;
3957	struct snd_soc_dai *source, *sink;
3958	struct snd_pcm_substream *substream = w->priv;
3959	int ret = 0, saved_stream = substream->stream;
3960
3961	if (WARN_ON(list_empty(&w->edges[SND_SOC_DAPM_DIR_OUT]) ||
3962		    list_empty(&w->edges[SND_SOC_DAPM_DIR_IN])))
3963		return -EINVAL;
3964
3965	switch (event) {
3966	case SND_SOC_DAPM_PRE_PMU:
3967		ret = snd_soc_dai_link_event_pre_pmu(w, substream);
3968		if (ret < 0)
3969			goto out;
 
 
 
 
 
 
 
3970
 
 
 
 
 
 
 
 
 
 
3971		break;
3972
3973	case SND_SOC_DAPM_POST_PMU:
3974		snd_soc_dapm_widget_for_each_sink_path(w, path) {
3975			sink = path->sink->priv;
3976
3977			snd_soc_dai_digital_mute(sink, 0, SNDRV_PCM_STREAM_PLAYBACK);
3978			ret = 0;
3979		}
3980		break;
3981
3982	case SND_SOC_DAPM_PRE_PMD:
3983		snd_soc_dapm_widget_for_each_sink_path(w, path) {
3984			sink = path->sink->priv;
3985
3986			snd_soc_dai_digital_mute(sink, 1, SNDRV_PCM_STREAM_PLAYBACK);
3987			ret = 0;
3988		}
3989
3990		substream->stream = SNDRV_PCM_STREAM_CAPTURE;
3991		snd_soc_dapm_widget_for_each_source_path(w, path) {
3992			source = path->source->priv;
3993			snd_soc_dai_hw_free(source, substream, 0);
3994		}
3995
3996		substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
3997		snd_soc_dapm_widget_for_each_sink_path(w, path) {
3998			sink = path->sink->priv;
3999			snd_soc_dai_hw_free(sink, substream, 0);
4000		}
4001
4002		substream->stream = SNDRV_PCM_STREAM_CAPTURE;
4003		snd_soc_dapm_widget_for_each_source_path(w, path) {
4004			source = path->source->priv;
4005			snd_soc_dai_deactivate(source, substream->stream);
4006			snd_soc_dai_shutdown(source, substream, 0);
4007		}
4008
4009		substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
4010		snd_soc_dapm_widget_for_each_sink_path(w, path) {
4011			sink = path->sink->priv;
4012			snd_soc_dai_deactivate(sink, substream->stream);
4013			snd_soc_dai_shutdown(sink, substream, 0);
4014		}
4015		break;
4016
4017	case SND_SOC_DAPM_POST_PMD:
4018		kfree(substream->runtime);
4019		break;
4020
4021	default:
4022		WARN(1, "Unknown event %d\n", event);
4023		ret = -EINVAL;
4024	}
4025
4026out:
4027	/* Restore the substream direction */
4028	substream->stream = saved_stream;
4029	return ret;
4030}
4031
4032static int snd_soc_dapm_dai_link_get(struct snd_kcontrol *kcontrol,
4033			  struct snd_ctl_elem_value *ucontrol)
4034{
4035	struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
4036	struct snd_soc_pcm_runtime *rtd = w->priv;
4037
4038	ucontrol->value.enumerated.item[0] = rtd->c2c_params_select;
4039
4040	return 0;
4041}
4042
4043static int snd_soc_dapm_dai_link_put(struct snd_kcontrol *kcontrol,
4044			  struct snd_ctl_elem_value *ucontrol)
4045{
4046	struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
4047	struct snd_soc_pcm_runtime *rtd = w->priv;
4048
4049	/* Can't change the config when widget is already powered */
4050	if (w->power)
4051		return -EBUSY;
4052
4053	if (ucontrol->value.enumerated.item[0] == rtd->c2c_params_select)
4054		return 0;
4055
4056	if (ucontrol->value.enumerated.item[0] >= rtd->dai_link->num_c2c_params)
4057		return -EINVAL;
4058
4059	rtd->c2c_params_select = ucontrol->value.enumerated.item[0];
4060
4061	return 1;
4062}
4063
4064static void
4065snd_soc_dapm_free_kcontrol(struct snd_soc_card *card,
4066			unsigned long *private_value,
4067			int num_c2c_params,
4068			const char **w_param_text)
4069{
4070	int count;
4071
4072	devm_kfree(card->dev, (void *)*private_value);
4073
4074	if (!w_param_text)
4075		return;
4076
4077	for (count = 0 ; count < num_c2c_params; count++)
4078		devm_kfree(card->dev, (void *)w_param_text[count]);
4079	devm_kfree(card->dev, w_param_text);
4080}
4081
4082static struct snd_kcontrol_new *
4083snd_soc_dapm_alloc_kcontrol(struct snd_soc_card *card,
4084			char *link_name,
4085			const struct snd_soc_pcm_stream *c2c_params,
4086			int num_c2c_params, const char **w_param_text,
4087			unsigned long *private_value)
4088{
4089	struct soc_enum w_param_enum[] = {
4090		SOC_ENUM_SINGLE(0, 0, 0, NULL),
4091	};
4092	struct snd_kcontrol_new kcontrol_dai_link[] = {
4093		SOC_ENUM_EXT(NULL, w_param_enum[0],
4094			     snd_soc_dapm_dai_link_get,
4095			     snd_soc_dapm_dai_link_put),
4096	};
4097	struct snd_kcontrol_new *kcontrol_news;
4098	const struct snd_soc_pcm_stream *config = c2c_params;
4099	int count;
4100
4101	for (count = 0 ; count < num_c2c_params; count++) {
4102		if (!config->stream_name) {
4103			dev_warn(card->dapm.dev,
4104				"ASoC: anonymous config %d for dai link %s\n",
4105				count, link_name);
4106			w_param_text[count] =
4107				devm_kasprintf(card->dev, GFP_KERNEL,
4108					       "Anonymous Configuration %d",
4109					       count);
4110		} else {
4111			w_param_text[count] = devm_kmemdup(card->dev,
4112						config->stream_name,
4113						strlen(config->stream_name) + 1,
4114						GFP_KERNEL);
4115		}
4116		if (!w_param_text[count])
4117			goto outfree_w_param;
4118		config++;
4119	}
4120
4121	w_param_enum[0].items = num_c2c_params;
4122	w_param_enum[0].texts = w_param_text;
4123
4124	*private_value =
4125		(unsigned long) devm_kmemdup(card->dev,
4126			(void *)(kcontrol_dai_link[0].private_value),
4127			sizeof(struct soc_enum), GFP_KERNEL);
4128	if (!*private_value) {
4129		dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
4130			link_name);
4131		goto outfree_w_param;
4132	}
4133	kcontrol_dai_link[0].private_value = *private_value;
4134	/* duplicate kcontrol_dai_link on heap so that memory persists */
4135	kcontrol_news = devm_kmemdup(card->dev, &kcontrol_dai_link[0],
4136					sizeof(struct snd_kcontrol_new),
4137					GFP_KERNEL);
4138	if (!kcontrol_news) {
4139		dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
4140			link_name);
4141		goto outfree_w_param;
4142	}
4143	return kcontrol_news;
4144
4145outfree_w_param:
4146	snd_soc_dapm_free_kcontrol(card, private_value, num_c2c_params, w_param_text);
4147	return NULL;
4148}
4149
4150static struct snd_soc_dapm_widget *
4151snd_soc_dapm_new_dai(struct snd_soc_card *card,
4152		     struct snd_pcm_substream *substream,
4153		     char *id)
4154{
4155	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
4156	struct snd_soc_dapm_widget template;
4157	struct snd_soc_dapm_widget *w;
4158	const struct snd_kcontrol_new *kcontrol_news;
4159	int num_kcontrols;
4160	const char **w_param_text;
4161	unsigned long private_value = 0;
4162	char *link_name;
4163	int ret = -ENOMEM;
4164
4165	link_name = devm_kasprintf(card->dev, GFP_KERNEL, "%s-%s",
4166				   rtd->dai_link->name, id);
4167	if (!link_name)
4168		goto name_fail;
4169
4170	/* allocate memory for control, only in case of multiple configs */
4171	w_param_text	= NULL;
4172	kcontrol_news	= NULL;
4173	num_kcontrols	= 0;
4174	if (rtd->dai_link->num_c2c_params > 1) {
4175		w_param_text = devm_kcalloc(card->dev,
4176					    rtd->dai_link->num_c2c_params,
4177					    sizeof(char *), GFP_KERNEL);
4178		if (!w_param_text)
4179			goto param_fail;
4180
4181		num_kcontrols = 1;
4182		kcontrol_news = snd_soc_dapm_alloc_kcontrol(card, link_name,
4183							    rtd->dai_link->c2c_params,
4184							    rtd->dai_link->num_c2c_params,
4185							    w_param_text, &private_value);
4186		if (!kcontrol_news)
4187			goto param_fail;
4188	}
4189
4190	memset(&template, 0, sizeof(template));
4191	template.reg		= SND_SOC_NOPM;
4192	template.id		= snd_soc_dapm_dai_link;
4193	template.name		= link_name;
4194	template.event		= snd_soc_dai_link_event;
4195	template.event_flags	= SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
4196				  SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD;
4197	template.kcontrol_news	= kcontrol_news;
4198	template.num_kcontrols	= num_kcontrols;
4199
4200	dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
4201
4202	w = snd_soc_dapm_new_control_unlocked(&card->dapm, &template);
4203	if (IS_ERR(w)) {
4204		ret = PTR_ERR(w);
4205		goto outfree_kcontrol_news;
4206	}
4207
4208	w->priv = substream;
4209
4210	return w;
4211
4212outfree_kcontrol_news:
4213	devm_kfree(card->dev, (void *)template.kcontrol_news);
4214	snd_soc_dapm_free_kcontrol(card, &private_value,
4215				   rtd->dai_link->num_c2c_params, w_param_text);
4216param_fail:
4217	devm_kfree(card->dev, link_name);
4218name_fail:
4219	dev_err(rtd->dev, "ASoC: Failed to create %s-%s widget: %d\n",
4220		rtd->dai_link->name, id, ret);
4221	return ERR_PTR(ret);
4222}
4223
4224/**
4225 * snd_soc_dapm_new_dai_widgets - Create new DAPM widgets
4226 * @dapm: DAPM context
4227 * @dai: parent DAI
4228 *
4229 * Returns 0 on success, error code otherwise.
4230 */
4231int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
4232				 struct snd_soc_dai *dai)
4233{
4234	struct snd_soc_dapm_widget template;
4235	struct snd_soc_dapm_widget *w;
4236
4237	WARN_ON(dapm->dev != dai->dev);
4238
4239	memset(&template, 0, sizeof(template));
4240	template.reg = SND_SOC_NOPM;
4241
4242	if (dai->driver->playback.stream_name) {
4243		template.id = snd_soc_dapm_dai_in;
4244		template.name = dai->driver->playback.stream_name;
4245		template.sname = dai->driver->playback.stream_name;
4246
4247		dev_dbg(dai->dev, "ASoC: adding %s widget\n",
4248			template.name);
4249
4250		w = snd_soc_dapm_new_control_unlocked(dapm, &template);
4251		if (IS_ERR(w))
4252			return PTR_ERR(w);
 
 
4253
4254		w->priv = dai;
4255		snd_soc_dai_set_widget_playback(dai, w);
4256	}
4257
4258	if (dai->driver->capture.stream_name) {
4259		template.id = snd_soc_dapm_dai_out;
4260		template.name = dai->driver->capture.stream_name;
4261		template.sname = dai->driver->capture.stream_name;
4262
4263		dev_dbg(dai->dev, "ASoC: adding %s widget\n",
4264			template.name);
4265
4266		w = snd_soc_dapm_new_control_unlocked(dapm, &template);
4267		if (IS_ERR(w))
4268			return PTR_ERR(w);
 
 
4269
4270		w->priv = dai;
4271		snd_soc_dai_set_widget_capture(dai, w);
4272	}
4273
4274	return 0;
4275}
4276EXPORT_SYMBOL_GPL(snd_soc_dapm_new_dai_widgets);
4277
4278int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
4279{
4280	struct snd_soc_dapm_widget *dai_w, *w;
4281	struct snd_soc_dapm_widget *src, *sink;
4282	struct snd_soc_dai *dai;
 
4283
4284	/* For each DAI widget... */
4285	for_each_card_widgets(card, dai_w) {
4286		switch (dai_w->id) {
4287		case snd_soc_dapm_dai_in:
4288		case snd_soc_dapm_dai_out:
4289			break;
4290		default:
4291			continue;
4292		}
4293
4294		/* let users know there is no DAI to link */
4295		if (!dai_w->priv) {
4296			dev_dbg(card->dev, "dai widget %s has no DAI\n",
4297				dai_w->name);
4298			continue;
4299		}
4300
4301		dai = dai_w->priv;
4302
4303		/* ...find all widgets with the same stream and link them */
4304		for_each_card_widgets(card, w) {
4305			if (w->dapm != dai_w->dapm)
4306				continue;
4307
4308			switch (w->id) {
4309			case snd_soc_dapm_dai_in:
4310			case snd_soc_dapm_dai_out:
4311				continue;
4312			default:
4313				break;
4314			}
4315
4316			if (!w->sname || !strstr(w->sname, dai_w->sname))
4317				continue;
4318
4319			if (dai_w->id == snd_soc_dapm_dai_in) {
4320				src = dai_w;
4321				sink = w;
4322			} else {
4323				src = w;
4324				sink = dai_w;
4325			}
4326			dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
4327			snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
4328		}
4329	}
4330
4331	return 0;
4332}
4333
4334static void dapm_connect_dai_routes(struct snd_soc_dapm_context *dapm,
4335				    struct snd_soc_dai *src_dai,
4336				    struct snd_soc_dapm_widget *src,
4337				    struct snd_soc_dapm_widget *dai,
4338				    struct snd_soc_dai *sink_dai,
4339				    struct snd_soc_dapm_widget *sink)
4340{
4341	dev_dbg(dapm->dev, "connected DAI link %s:%s -> %s:%s\n",
4342		src_dai->component->name, src->name,
4343		sink_dai->component->name, sink->name);
4344
4345	if (dai) {
4346		snd_soc_dapm_add_path(dapm, src, dai, NULL, NULL);
4347		src = dai;
4348	}
4349
4350	snd_soc_dapm_add_path(dapm, src, sink, NULL, NULL);
4351}
4352
4353static void dapm_connect_dai_pair(struct snd_soc_card *card,
4354				  struct snd_soc_pcm_runtime *rtd,
4355				  struct snd_soc_dai *codec_dai,
4356				  struct snd_soc_dai *cpu_dai)
4357{
4358	struct snd_soc_dai_link *dai_link = rtd->dai_link;
4359	struct snd_soc_dapm_widget *codec, *cpu;
4360	struct snd_soc_dai *src_dai[]		= { cpu_dai,	codec_dai };
4361	struct snd_soc_dai *sink_dai[]		= { codec_dai,	cpu_dai };
4362	struct snd_soc_dapm_widget **src[]	= { &cpu,	&codec };
4363	struct snd_soc_dapm_widget **sink[]	= { &codec,	&cpu };
4364	char *widget_name[]			= { "playback",	"capture" };
4365	int stream;
4366
4367	for_each_pcm_streams(stream) {
4368		int stream_cpu, stream_codec;
4369
4370		stream_cpu	= snd_soc_get_stream_cpu(dai_link, stream);
4371		stream_codec	= stream;
4372
4373		/* connect BE DAI playback if widgets are valid */
4374		cpu	= snd_soc_dai_get_widget(cpu_dai,	stream_cpu);
4375		codec	= snd_soc_dai_get_widget(codec_dai,	stream_codec);
4376
4377		if (!cpu || !codec)
4378			continue;
4379
4380		/* special handling for [Codec2Codec] */
4381		if (dai_link->c2c_params && !rtd->c2c_widget[stream]) {
4382			struct snd_pcm_substream *substream = rtd->pcm->streams[stream].substream;
4383			struct snd_soc_dapm_widget *dai = snd_soc_dapm_new_dai(card, substream,
4384									       widget_name[stream]);
4385
4386			if (IS_ERR(dai))
4387				continue;
 
 
 
 
 
4388
4389			rtd->c2c_widget[stream] = dai;
 
4390		}
4391
4392		dapm_connect_dai_routes(&card->dapm, src_dai[stream], *src[stream],
4393					rtd->c2c_widget[stream],
4394					sink_dai[stream], *sink[stream]);
4395	}
 
 
4396}
4397
4398static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
4399	int event)
4400{
4401	struct snd_soc_dapm_widget *w;
4402
4403	w = snd_soc_dai_get_widget(dai, stream);
 
 
4404
4405	if (w) {
4406		unsigned int ep;
 
 
 
 
 
4407
4408		dapm_mark_dirty(w, "stream event");
4409
4410		if (w->id == snd_soc_dapm_dai_in) {
4411			ep = SND_SOC_DAPM_EP_SOURCE;
4412			dapm_widget_invalidate_input_paths(w);
4413		} else {
4414			ep = SND_SOC_DAPM_EP_SINK;
4415			dapm_widget_invalidate_output_paths(w);
4416		}
4417
4418		switch (event) {
4419		case SND_SOC_DAPM_STREAM_START:
4420			w->active = 1;
4421			w->is_ep = ep;
4422			break;
4423		case SND_SOC_DAPM_STREAM_STOP:
4424			w->active = 0;
4425			w->is_ep = 0;
4426			break;
4427		case SND_SOC_DAPM_STREAM_SUSPEND:
4428		case SND_SOC_DAPM_STREAM_RESUME:
4429		case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
4430		case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
4431			break;
4432		}
4433	}
4434}
4435
4436void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
4437{
4438	struct snd_soc_pcm_runtime *rtd;
4439	struct snd_soc_dai *cpu_dai;
4440	struct snd_soc_dai *codec_dai;
4441
4442	/* for each BE DAI link... */
4443	for_each_card_rtds(card, rtd)  {
4444		struct snd_soc_dai_link_ch_map *ch_maps;
4445		int i;
4446
4447		/*
4448		 * dynamic FE links have no fixed DAI mapping.
4449		 * CODEC<->CODEC links have no direct connection.
4450		 */
4451		if (rtd->dai_link->dynamic)
4452			continue;
4453
4454		/*
4455		 * see
4456		 *	soc.h :: [dai_link->ch_maps Image sample]
4457		 */
4458		for_each_rtd_ch_maps(rtd, i, ch_maps) {
4459			cpu_dai   = snd_soc_rtd_to_cpu(rtd,   ch_maps->cpu);
4460			codec_dai = snd_soc_rtd_to_codec(rtd, ch_maps->codec);
4461
4462			dapm_connect_dai_pair(card, rtd, codec_dai, cpu_dai);
 
 
 
 
 
 
 
 
 
 
 
4463		}
4464	}
4465}
4466
4467static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4468	int event)
4469{
4470	struct snd_soc_dai *dai;
4471	int i;
4472
4473	for_each_rtd_dais(rtd, i, dai)
4474		soc_dapm_dai_stream_event(dai, stream, event);
4475
4476	dapm_power_widgets(rtd->card, event);
4477}
4478
4479/**
4480 * snd_soc_dapm_stream_event - send a stream event to the dapm core
4481 * @rtd: PCM runtime data
4482 * @stream: stream name
4483 * @event: stream event
4484 *
4485 * Sends a stream event to the dapm core. The core then makes any
4486 * necessary widget power changes.
4487 *
4488 * Returns 0 for success else error.
4489 */
4490void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4491			      int event)
4492{
4493	struct snd_soc_card *card = rtd->card;
4494
4495	snd_soc_dapm_mutex_lock(card);
4496	soc_dapm_stream_event(rtd, stream, event);
4497	snd_soc_dapm_mutex_unlock(card);
4498}
4499
4500void snd_soc_dapm_stream_stop(struct snd_soc_pcm_runtime *rtd, int stream)
4501{
4502	if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
4503		if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
4504			/* powered down playback stream now */
4505			snd_soc_dapm_stream_event(rtd,
4506						  SNDRV_PCM_STREAM_PLAYBACK,
4507						  SND_SOC_DAPM_STREAM_STOP);
4508		} else {
4509			/* start delayed pop wq here for playback streams */
4510			rtd->pop_wait = 1;
4511			queue_delayed_work(system_power_efficient_wq,
4512					   &rtd->delayed_work,
4513					   msecs_to_jiffies(rtd->pmdown_time));
4514		}
4515	} else {
4516		/* capture streams can be powered down now */
4517		snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
4518					  SND_SOC_DAPM_STREAM_STOP);
4519	}
4520}
4521EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_stop);
4522
4523/**
4524 * snd_soc_dapm_enable_pin_unlocked - enable pin.
4525 * @dapm: DAPM context
4526 * @pin: pin name
4527 *
4528 * Enables input/output pin and its parents or children widgets iff there is
4529 * a valid audio route and active audio stream.
4530 *
4531 * Requires external locking.
4532 *
4533 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4534 * do any widget power switching.
4535 */
4536int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4537				   const char *pin)
4538{
4539	return snd_soc_dapm_set_pin(dapm, pin, 1);
4540}
4541EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
4542
4543/**
4544 * snd_soc_dapm_enable_pin - enable pin.
4545 * @dapm: DAPM context
4546 * @pin: pin name
4547 *
4548 * Enables input/output pin and its parents or children widgets iff there is
4549 * a valid audio route and active audio stream.
4550 *
4551 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4552 * do any widget power switching.
4553 */
4554int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
4555{
4556	int ret;
4557
4558	snd_soc_dapm_mutex_lock(dapm);
4559
4560	ret = snd_soc_dapm_set_pin(dapm, pin, 1);
4561
4562	snd_soc_dapm_mutex_unlock(dapm);
4563
4564	return ret;
4565}
4566EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
4567
4568/**
4569 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
4570 * @dapm: DAPM context
4571 * @pin: pin name
4572 *
4573 * Enables input/output pin regardless of any other state.  This is
4574 * intended for use with microphone bias supplies used in microphone
4575 * jack detection.
4576 *
4577 * Requires external locking.
4578 *
4579 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4580 * do any widget power switching.
4581 */
4582int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4583					 const char *pin)
4584{
4585	struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4586
4587	if (!w) {
4588		dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4589		return -EINVAL;
4590	}
4591
4592	dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
4593	if (!w->connected) {
4594		/*
4595		 * w->force does not affect the number of input or output paths,
4596		 * so we only have to recheck if w->connected is changed
4597		 */
4598		dapm_widget_invalidate_input_paths(w);
4599		dapm_widget_invalidate_output_paths(w);
4600		w->connected = 1;
4601	}
4602	w->force = 1;
4603	dapm_mark_dirty(w, "force enable");
4604
4605	return 0;
4606}
4607EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
4608
4609/**
4610 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
4611 * @dapm: DAPM context
4612 * @pin: pin name
4613 *
4614 * Enables input/output pin regardless of any other state.  This is
4615 * intended for use with microphone bias supplies used in microphone
4616 * jack detection.
4617 *
4618 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4619 * do any widget power switching.
4620 */
4621int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
4622				  const char *pin)
4623{
4624	int ret;
4625
4626	snd_soc_dapm_mutex_lock(dapm);
4627
4628	ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
4629
4630	snd_soc_dapm_mutex_unlock(dapm);
4631
4632	return ret;
4633}
4634EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
4635
4636/**
4637 * snd_soc_dapm_disable_pin_unlocked - disable pin.
4638 * @dapm: DAPM context
4639 * @pin: pin name
4640 *
4641 * Disables input/output pin and its parents or children widgets.
4642 *
4643 * Requires external locking.
4644 *
4645 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4646 * do any widget power switching.
4647 */
4648int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4649				    const char *pin)
4650{
4651	return snd_soc_dapm_set_pin(dapm, pin, 0);
4652}
4653EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
4654
4655/**
4656 * snd_soc_dapm_disable_pin - disable pin.
4657 * @dapm: DAPM context
4658 * @pin: pin name
4659 *
4660 * Disables input/output pin and its parents or children widgets.
4661 *
4662 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4663 * do any widget power switching.
4664 */
4665int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
4666			     const char *pin)
4667{
4668	int ret;
4669
4670	snd_soc_dapm_mutex_lock(dapm);
4671
4672	ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4673
4674	snd_soc_dapm_mutex_unlock(dapm);
4675
4676	return ret;
4677}
4678EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
4679
4680/**
4681 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
4682 * @dapm: DAPM context
4683 * @pin: pin name
4684 *
4685 * Marks the specified pin as being not connected, disabling it along
4686 * any parent or child widgets.  At present this is identical to
4687 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4688 * additional things such as disabling controls which only affect
4689 * paths through the pin.
4690 *
4691 * Requires external locking.
4692 *
4693 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4694 * do any widget power switching.
4695 */
4696int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
4697			       const char *pin)
4698{
4699	return snd_soc_dapm_set_pin(dapm, pin, 0);
4700}
4701EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
4702
4703/**
4704 * snd_soc_dapm_nc_pin - permanently disable pin.
4705 * @dapm: DAPM context
4706 * @pin: pin name
4707 *
4708 * Marks the specified pin as being not connected, disabling it along
4709 * any parent or child widgets.  At present this is identical to
4710 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4711 * additional things such as disabling controls which only affect
4712 * paths through the pin.
4713 *
4714 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4715 * do any widget power switching.
4716 */
4717int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
4718{
4719	int ret;
4720
4721	snd_soc_dapm_mutex_lock(dapm);
4722
4723	ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4724
4725	snd_soc_dapm_mutex_unlock(dapm);
4726
4727	return ret;
4728}
4729EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
4730
4731/**
4732 * snd_soc_dapm_get_pin_status - get audio pin status
4733 * @dapm: DAPM context
4734 * @pin: audio signal pin endpoint (or start point)
4735 *
4736 * Get audio pin status - connected or disconnected.
4737 *
4738 * Returns 1 for connected otherwise 0.
4739 */
4740int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
4741				const char *pin)
4742{
4743	struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4744
4745	if (w)
4746		return w->connected;
4747
4748	return 0;
4749}
4750EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
4751
4752/**
4753 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
4754 * @dapm: DAPM context
4755 * @pin: audio signal pin endpoint (or start point)
4756 *
4757 * Mark the given endpoint or pin as ignoring suspend.  When the
4758 * system is disabled a path between two endpoints flagged as ignoring
4759 * suspend will not be disabled.  The path must already be enabled via
4760 * normal means at suspend time, it will not be turned on if it was not
4761 * already enabled.
4762 */
4763int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
4764				const char *pin)
4765{
4766	struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
4767
4768	if (!w) {
4769		dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4770		return -EINVAL;
4771	}
4772
4773	w->ignore_suspend = 1;
4774
4775	return 0;
4776}
4777EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
4778
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4779/**
4780 * snd_soc_dapm_free - free dapm resources
4781 * @dapm: DAPM context
4782 *
4783 * Free all dapm widgets and resources.
4784 */
4785void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
4786{
 
4787	dapm_debugfs_cleanup(dapm);
4788	dapm_free_widgets(dapm);
4789	list_del(&dapm->list);
4790}
4791EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
4792
4793void snd_soc_dapm_init(struct snd_soc_dapm_context *dapm,
4794		       struct snd_soc_card *card,
4795		       struct snd_soc_component *component)
4796{
4797	dapm->card		= card;
4798	dapm->component		= component;
4799	dapm->bias_level	= SND_SOC_BIAS_OFF;
4800
4801	if (component) {
4802		dapm->dev		= component->dev;
4803		dapm->idle_bias_off	= !component->driver->idle_bias_on;
4804		dapm->suspend_bias_off	= component->driver->suspend_bias_off;
4805	} else {
4806		dapm->dev		= card->dev;
4807	}
4808
4809	INIT_LIST_HEAD(&dapm->list);
4810	/* see for_each_card_dapms */
4811	list_add(&dapm->list, &card->dapm_list);
4812}
4813EXPORT_SYMBOL_GPL(snd_soc_dapm_init);
4814
4815static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
4816{
4817	struct snd_soc_card *card = dapm->card;
4818	struct snd_soc_dapm_widget *w;
4819	LIST_HEAD(down_list);
4820	int powerdown = 0;
4821
4822	snd_soc_dapm_mutex_lock_root(card);
4823
4824	for_each_card_widgets(dapm->card, w) {
4825		if (w->dapm != dapm)
4826			continue;
4827		if (w->power) {
4828			dapm_seq_insert(w, &down_list, false);
4829			w->new_power = 0;
4830			powerdown = 1;
4831		}
4832	}
4833
4834	/* If there were no widgets to power down we're already in
4835	 * standby.
4836	 */
4837	if (powerdown) {
4838		if (dapm->bias_level == SND_SOC_BIAS_ON)
4839			snd_soc_dapm_set_bias_level(dapm,
4840						    SND_SOC_BIAS_PREPARE);
4841		dapm_seq_run(card, &down_list, 0, false);
4842		if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
4843			snd_soc_dapm_set_bias_level(dapm,
4844						    SND_SOC_BIAS_STANDBY);
4845	}
4846
4847	snd_soc_dapm_mutex_unlock(card);
4848}
4849
4850/*
4851 * snd_soc_dapm_shutdown - callback for system shutdown
4852 */
4853void snd_soc_dapm_shutdown(struct snd_soc_card *card)
4854{
4855	struct snd_soc_dapm_context *dapm;
4856
4857	for_each_card_dapms(card, dapm) {
4858		if (dapm != &card->dapm) {
4859			soc_dapm_shutdown_dapm(dapm);
4860			if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
4861				snd_soc_dapm_set_bias_level(dapm,
4862							    SND_SOC_BIAS_OFF);
4863		}
4864	}
4865
4866	soc_dapm_shutdown_dapm(&card->dapm);
4867	if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
4868		snd_soc_dapm_set_bias_level(&card->dapm,
4869					    SND_SOC_BIAS_OFF);
4870}
4871
4872/* Module information */
4873MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4874MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
4875MODULE_LICENSE("GPL");
v3.5.6
   1/*
   2 * soc-dapm.c  --  ALSA SoC Dynamic Audio Power Management
   3 *
   4 * Copyright 2005 Wolfson Microelectronics PLC.
   5 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
   6 *
   7 *  This program is free software; you can redistribute  it and/or modify it
   8 *  under  the terms of  the GNU General  Public License as published by the
   9 *  Free Software Foundation;  either version 2 of the  License, or (at your
  10 *  option) any later version.
  11 *
  12 *  Features:
  13 *    o Changes power status of internal codec blocks depending on the
  14 *      dynamic configuration of codec internal audio paths and active
  15 *      DACs/ADCs.
  16 *    o Platform power domain - can support external components i.e. amps and
  17 *      mic/headphone insertion events.
  18 *    o Automatic Mic Bias support
  19 *    o Jack insertion power event initiation - e.g. hp insertion will enable
  20 *      sinks, dacs, etc
  21 *    o Delayed power down of audio subsystem to reduce pops between a quick
  22 *      device reopen.
  23 *
  24 */
  25
  26#include <linux/module.h>
  27#include <linux/moduleparam.h>
  28#include <linux/init.h>
  29#include <linux/async.h>
  30#include <linux/delay.h>
  31#include <linux/pm.h>
  32#include <linux/bitops.h>
  33#include <linux/platform_device.h>
  34#include <linux/jiffies.h>
  35#include <linux/debugfs.h>
  36#include <linux/pm_runtime.h>
  37#include <linux/regulator/consumer.h>
 
 
  38#include <linux/slab.h>
  39#include <sound/core.h>
  40#include <sound/pcm.h>
  41#include <sound/pcm_params.h>
  42#include <sound/soc.h>
  43#include <sound/initval.h>
  44
  45#include <trace/events/asoc.h>
  46
  47#define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
  48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  49/* dapm power sequences - make this per codec in the future */
  50static int dapm_up_seq[] = {
  51	[snd_soc_dapm_pre] = 0,
  52	[snd_soc_dapm_supply] = 1,
  53	[snd_soc_dapm_regulator_supply] = 1,
  54	[snd_soc_dapm_micbias] = 2,
  55	[snd_soc_dapm_dai_link] = 2,
  56	[snd_soc_dapm_dai] = 3,
  57	[snd_soc_dapm_aif_in] = 3,
  58	[snd_soc_dapm_aif_out] = 3,
  59	[snd_soc_dapm_mic] = 4,
  60	[snd_soc_dapm_mux] = 5,
  61	[snd_soc_dapm_virt_mux] = 5,
  62	[snd_soc_dapm_value_mux] = 5,
  63	[snd_soc_dapm_dac] = 6,
  64	[snd_soc_dapm_mixer] = 7,
  65	[snd_soc_dapm_mixer_named_ctl] = 7,
  66	[snd_soc_dapm_pga] = 8,
  67	[snd_soc_dapm_adc] = 9,
  68	[snd_soc_dapm_out_drv] = 10,
  69	[snd_soc_dapm_hp] = 10,
  70	[snd_soc_dapm_spk] = 10,
  71	[snd_soc_dapm_line] = 10,
  72	[snd_soc_dapm_post] = 11,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  73};
  74
  75static int dapm_down_seq[] = {
  76	[snd_soc_dapm_pre] = 0,
  77	[snd_soc_dapm_adc] = 1,
  78	[snd_soc_dapm_hp] = 2,
  79	[snd_soc_dapm_spk] = 2,
  80	[snd_soc_dapm_line] = 2,
  81	[snd_soc_dapm_out_drv] = 2,
  82	[snd_soc_dapm_pga] = 4,
  83	[snd_soc_dapm_mixer_named_ctl] = 5,
  84	[snd_soc_dapm_mixer] = 5,
  85	[snd_soc_dapm_dac] = 6,
  86	[snd_soc_dapm_mic] = 7,
  87	[snd_soc_dapm_micbias] = 8,
  88	[snd_soc_dapm_mux] = 9,
  89	[snd_soc_dapm_virt_mux] = 9,
  90	[snd_soc_dapm_value_mux] = 9,
  91	[snd_soc_dapm_aif_in] = 10,
  92	[snd_soc_dapm_aif_out] = 10,
  93	[snd_soc_dapm_dai] = 10,
  94	[snd_soc_dapm_dai_link] = 11,
  95	[snd_soc_dapm_regulator_supply] = 12,
  96	[snd_soc_dapm_supply] = 12,
  97	[snd_soc_dapm_post] = 13,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  98};
  99
 
 
 
 
 
 
 100static void pop_wait(u32 pop_time)
 101{
 102	if (pop_time)
 103		schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
 104}
 105
 
 106static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
 107{
 108	va_list args;
 109	char *buf;
 110
 111	if (!pop_time)
 112		return;
 113
 114	buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
 115	if (buf == NULL)
 116		return;
 117
 118	va_start(args, fmt);
 119	vsnprintf(buf, PAGE_SIZE, fmt, args);
 120	dev_info(dev, "%s", buf);
 121	va_end(args);
 122
 123	kfree(buf);
 124}
 125
 126static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
 127{
 128	return !list_empty(&w->dirty);
 129}
 130
 131void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
 132{
 
 
 133	if (!dapm_dirty_widget(w)) {
 134		dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
 135			 w->name, reason);
 136		list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
 137	}
 138}
 139EXPORT_SYMBOL_GPL(dapm_mark_dirty);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 140
 141/* create a new dapm widget */
 142static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
 143	const struct snd_soc_dapm_widget *_widget)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 144{
 145	return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
 
 
 146}
 147
 148/* get snd_card from DAPM context */
 149static inline struct snd_card *dapm_get_snd_card(
 150	struct snd_soc_dapm_context *dapm)
 151{
 152	if (dapm->codec)
 153		return dapm->codec->card->snd_card;
 154	else if (dapm->platform)
 155		return dapm->platform->card->snd_card;
 
 
 156	else
 157		BUG();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 158
 159	/* unreachable */
 160	return NULL;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 161}
 162
 163/* get soc_card from DAPM context */
 164static inline struct snd_soc_card *dapm_get_soc_card(
 165		struct snd_soc_dapm_context *dapm)
 166{
 167	if (dapm->codec)
 168		return dapm->codec->card;
 169	else if (dapm->platform)
 170		return dapm->platform->card;
 171	else
 172		BUG();
 
 173
 174	/* unreachable */
 175	return NULL;
 
 
 
 
 
 
 
 
 
 
 176}
 
 177
 178static void dapm_reset(struct snd_soc_card *card)
 179{
 180	struct snd_soc_dapm_widget *w;
 181
 
 
 182	memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
 183
 184	list_for_each_entry(w, &card->widgets, list) {
 
 185		w->power_checked = false;
 186		w->inputs = -1;
 187		w->outputs = -1;
 188	}
 189}
 190
 191static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg)
 192{
 193	if (w->codec)
 194		return snd_soc_read(w->codec, reg);
 195	else if (w->platform)
 196		return snd_soc_platform_read(w->platform, reg);
 197
 198	dev_err(w->dapm->dev, "no valid widget read method\n");
 199	return -1;
 
 
 
 200}
 201
 202static int soc_widget_write(struct snd_soc_dapm_widget *w, int reg, int val)
 
 203{
 204	if (w->codec)
 205		return snd_soc_write(w->codec, reg, val);
 206	else if (w->platform)
 207		return snd_soc_platform_write(w->platform, reg, val);
 208
 209	dev_err(w->dapm->dev, "no valid widget write method\n");
 210	return -1;
 211}
 212
 213static inline void soc_widget_lock(struct snd_soc_dapm_widget *w)
 
 214{
 215	if (w->codec && !w->codec->using_regmap)
 216		mutex_lock(&w->codec->mutex);
 217	else if (w->platform)
 218		mutex_lock(&w->platform->mutex);
 219}
 220
 221static inline void soc_widget_unlock(struct snd_soc_dapm_widget *w)
 222{
 223	if (w->codec && !w->codec->using_regmap)
 224		mutex_unlock(&w->codec->mutex);
 225	else if (w->platform)
 226		mutex_unlock(&w->platform->mutex);
 227}
 228
 229static int soc_widget_update_bits_locked(struct snd_soc_dapm_widget *w,
 230	unsigned short reg, unsigned int mask, unsigned int value)
 231{
 232	bool change;
 233	unsigned int old, new;
 234	int ret;
 
 235
 236	if (w->codec && w->codec->using_regmap) {
 237		ret = regmap_update_bits_check(w->codec->control_data,
 238					       reg, mask, value, &change);
 239		if (ret != 0)
 240			return ret;
 241	} else {
 242		soc_widget_lock(w);
 243		ret = soc_widget_read(w, reg);
 244		if (ret < 0) {
 245			soc_widget_unlock(w);
 246			return ret;
 247		}
 248
 249		old = ret;
 250		new = (old & ~mask) | (value & mask);
 251		change = old != new;
 252		if (change) {
 253			ret = soc_widget_write(w, reg, new);
 254			if (ret < 0) {
 255				soc_widget_unlock(w);
 256				return ret;
 257			}
 258		}
 259		soc_widget_unlock(w);
 260	}
 261
 262	return change;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 263}
 
 264
 265/**
 266 * snd_soc_dapm_set_bias_level - set the bias level for the system
 267 * @dapm: DAPM context
 268 * @level: level to configure
 269 *
 270 * Configure the bias (power) levels for the SoC audio device.
 271 *
 272 * Returns 0 for success else error.
 273 */
 274static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
 275				       enum snd_soc_bias_level level)
 276{
 277	struct snd_soc_card *card = dapm->card;
 278	int ret = 0;
 279
 280	trace_snd_soc_bias_level_start(card, level);
 281
 282	if (card && card->set_bias_level)
 283		ret = card->set_bias_level(card, dapm, level);
 284	if (ret != 0)
 285		goto out;
 286
 287	if (dapm->codec) {
 288		if (dapm->codec->driver->set_bias_level)
 289			ret = dapm->codec->driver->set_bias_level(dapm->codec,
 290								  level);
 291		else
 292			dapm->bias_level = level;
 293	}
 294	if (ret != 0)
 295		goto out;
 296
 297	if (card && card->set_bias_level_post)
 298		ret = card->set_bias_level_post(card, dapm, level);
 299out:
 300	trace_snd_soc_bias_level_done(card, level);
 301
 302	return ret;
 303}
 304
 305/* set up initial codec paths */
 306static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
 307	struct snd_soc_dapm_path *p, int i)
 
 308{
 309	switch (w->id) {
 310	case snd_soc_dapm_switch:
 311	case snd_soc_dapm_mixer:
 312	case snd_soc_dapm_mixer_named_ctl: {
 313		int val;
 314		struct soc_mixer_control *mc = (struct soc_mixer_control *)
 315			w->kcontrol_news[i].private_value;
 316		unsigned int reg = mc->reg;
 317		unsigned int shift = mc->shift;
 318		int max = mc->max;
 319		unsigned int mask = (1 << fls(max)) - 1;
 320		unsigned int invert = mc->invert;
 321
 322		val = soc_widget_read(w, reg);
 323		val = (val >> shift) & mask;
 324
 325		if ((invert && !val) || (!invert && val))
 326			p->connect = 1;
 327		else
 328			p->connect = 0;
 329	}
 330	break;
 331	case snd_soc_dapm_mux: {
 332		struct soc_enum *e = (struct soc_enum *)
 333			w->kcontrol_news[i].private_value;
 334		int val, item, bitmask;
 335
 336		for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
 337			;
 338		val = soc_widget_read(w, e->reg);
 339		item = (val >> e->shift_l) & (bitmask - 1);
 340
 341		p->connect = 0;
 342		for (i = 0; i < e->max; i++) {
 343			if (!(strcmp(p->name, e->texts[i])) && item == i)
 344				p->connect = 1;
 345		}
 346	}
 347	break;
 348	case snd_soc_dapm_virt_mux: {
 349		struct soc_enum *e = (struct soc_enum *)
 350			w->kcontrol_news[i].private_value;
 351
 352		p->connect = 0;
 353		/* since a virtual mux has no backing registers to
 354		 * decide which path to connect, it will try to match
 355		 * with the first enumeration.  This is to ensure
 356		 * that the default mux choice (the first) will be
 357		 * correctly powered up during initialization.
 358		 */
 359		if (!strcmp(p->name, e->texts[0]))
 360			p->connect = 1;
 361	}
 362	break;
 363	case snd_soc_dapm_value_mux: {
 364		struct soc_enum *e = (struct soc_enum *)
 365			w->kcontrol_news[i].private_value;
 366		int val, item;
 367
 368		val = soc_widget_read(w, e->reg);
 369		val = (val >> e->shift_l) & e->mask;
 370		for (item = 0; item < e->max; item++) {
 371			if (val == e->values[item])
 372				break;
 373		}
 
 374
 375		p->connect = 0;
 376		for (i = 0; i < e->max; i++) {
 377			if (!(strcmp(p->name, e->texts[i])) && item == i)
 378				p->connect = 1;
 379		}
 380	}
 381	break;
 382	/* does not affect routing - always connected */
 383	case snd_soc_dapm_pga:
 384	case snd_soc_dapm_out_drv:
 385	case snd_soc_dapm_output:
 386	case snd_soc_dapm_adc:
 387	case snd_soc_dapm_input:
 388	case snd_soc_dapm_siggen:
 389	case snd_soc_dapm_dac:
 390	case snd_soc_dapm_micbias:
 391	case snd_soc_dapm_vmid:
 392	case snd_soc_dapm_supply:
 393	case snd_soc_dapm_regulator_supply:
 394	case snd_soc_dapm_aif_in:
 395	case snd_soc_dapm_aif_out:
 396	case snd_soc_dapm_dai:
 397	case snd_soc_dapm_hp:
 398	case snd_soc_dapm_mic:
 399	case snd_soc_dapm_spk:
 400	case snd_soc_dapm_line:
 401	case snd_soc_dapm_dai_link:
 402		p->connect = 1;
 403	break;
 404	/* does affect routing - dynamically connected */
 405	case snd_soc_dapm_pre:
 406	case snd_soc_dapm_post:
 407		p->connect = 0;
 408	break;
 409	}
 410}
 411
 412/* connect mux widget to its interconnecting audio paths */
 413static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
 414	struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
 415	struct snd_soc_dapm_path *path, const char *control_name,
 416	const struct snd_kcontrol_new *kcontrol)
 417{
 418	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
 419	int i;
 
 
 
 
 
 
 
 
 420
 421	for (i = 0; i < e->max; i++) {
 422		if (!(strcmp(control_name, e->texts[i]))) {
 423			list_add(&path->list, &dapm->card->paths);
 424			list_add(&path->list_sink, &dest->sources);
 425			list_add(&path->list_source, &src->sinks);
 426			path->name = (char*)e->texts[i];
 427			dapm_set_path_status(dest, path, 0);
 428			return 0;
 
 
 
 
 
 
 
 
 
 
 429		}
 
 
 
 
 
 
 
 
 
 
 
 430	}
 431
 432	return -ENODEV;
 433}
 434
 435/* connect mixer widget to its interconnecting audio paths */
 436static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
 437	struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
 438	struct snd_soc_dapm_path *path, const char *control_name)
 439{
 440	int i;
 441
 442	/* search for mixer kcontrol */
 443	for (i = 0; i < dest->num_kcontrols; i++) {
 444		if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
 445			list_add(&path->list, &dapm->card->paths);
 446			list_add(&path->list_sink, &dest->sources);
 447			list_add(&path->list_source, &src->sinks);
 448			path->name = dest->kcontrol_news[i].name;
 449			dapm_set_path_status(dest, path, i);
 450			return 0;
 451		}
 452	}
 453	return -ENODEV;
 454}
 455
 456static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
 457	struct snd_soc_dapm_widget *kcontrolw,
 458	const struct snd_kcontrol_new *kcontrol_new,
 459	struct snd_kcontrol **kcontrol)
 460{
 461	struct snd_soc_dapm_widget *w;
 462	int i;
 463
 464	*kcontrol = NULL;
 465
 466	list_for_each_entry(w, &dapm->card->widgets, list) {
 467		if (w == kcontrolw || w->dapm != kcontrolw->dapm)
 468			continue;
 469		for (i = 0; i < w->num_kcontrols; i++) {
 470			if (&w->kcontrol_news[i] == kcontrol_new) {
 471				if (w->kcontrols)
 472					*kcontrol = w->kcontrols[i];
 473				return 1;
 474			}
 475		}
 476	}
 477
 478	return 0;
 479}
 480
 481/* create new dapm mixer control */
 482static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
 
 
 
 
 483{
 484	struct snd_soc_dapm_context *dapm = w->dapm;
 485	int i, ret = 0;
 486	size_t name_len, prefix_len;
 487	struct snd_soc_dapm_path *path;
 488	struct snd_card *card = dapm->card->snd_card;
 489	const char *prefix;
 490	struct snd_soc_dapm_widget_list *wlist;
 491	size_t wlistsize;
 492
 493	if (dapm->codec)
 494		prefix = dapm->codec->name_prefix;
 495	else
 496		prefix = NULL;
 497
 
 498	if (prefix)
 499		prefix_len = strlen(prefix) + 1;
 500	else
 501		prefix_len = 0;
 502
 503	/* add kcontrol */
 504	for (i = 0; i < w->num_kcontrols; i++) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 505
 506		/* match name */
 507		list_for_each_entry(path, &w->sources, list_sink) {
 
 
 
 508
 509			/* mixer/mux paths name must match control name */
 510			if (path->name != (char *)w->kcontrol_news[i].name)
 511				continue;
 
 
 
 
 
 512
 513			if (w->kcontrols[i]) {
 514				path->kcontrol = w->kcontrols[i];
 515				continue;
 516			}
 517
 518			wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
 519				    sizeof(struct snd_soc_dapm_widget *),
 520			wlist = kzalloc(wlistsize, GFP_KERNEL);
 521			if (wlist == NULL) {
 522				dev_err(dapm->dev,
 523					"asoc: can't allocate widget list for %s\n",
 524					w->name);
 525				return -ENOMEM;
 526			}
 527			wlist->num_widgets = 1;
 528			wlist->widgets[0] = w;
 529
 530			/* add dapm control with long name.
 531			 * for dapm_mixer this is the concatenation of the
 532			 * mixer and kcontrol name.
 533			 * for dapm_mixer_named_ctl this is simply the
 534			 * kcontrol name.
 535			 */
 536			name_len = strlen(w->kcontrol_news[i].name) + 1;
 537			if (w->id != snd_soc_dapm_mixer_named_ctl)
 538				name_len += 1 + strlen(w->name);
 539
 540			path->long_name = kmalloc(name_len, GFP_KERNEL);
 
 
 
 
 
 541
 542			if (path->long_name == NULL) {
 543				kfree(wlist);
 544				return -ENOMEM;
 545			}
 
 
 
 546
 547			switch (w->id) {
 548			default:
 549				/* The control will get a prefix from
 550				 * the control creation process but
 551				 * we're also using the same prefix
 552				 * for widgets so cut the prefix off
 553				 * the front of the widget name.
 554				 */
 555				snprintf((char *)path->long_name, name_len,
 556					 "%s %s", w->name + prefix_len,
 557					 w->kcontrol_news[i].name);
 558				break;
 559			case snd_soc_dapm_mixer_named_ctl:
 560				snprintf((char *)path->long_name, name_len,
 561					 "%s", w->kcontrol_news[i].name);
 562				break;
 563			}
 564
 565			((char *)path->long_name)[name_len - 1] = '\0';
 566
 567			path->kcontrol = snd_soc_cnew(&w->kcontrol_news[i],
 568						      wlist, path->long_name,
 569						      prefix);
 570			ret = snd_ctl_add(card, path->kcontrol);
 571			if (ret < 0) {
 572				dev_err(dapm->dev,
 573					"asoc: failed to add dapm kcontrol %s: %d\n",
 574					path->long_name, ret);
 575				kfree(wlist);
 576				kfree(path->long_name);
 577				path->long_name = NULL;
 578				return ret;
 579			}
 580			w->kcontrols[i] = path->kcontrol;
 581		}
 582	}
 583	return ret;
 
 584}
 585
 586/* create new dapm mux control */
 587static int dapm_new_mux(struct snd_soc_dapm_widget *w)
 588{
 589	struct snd_soc_dapm_context *dapm = w->dapm;
 590	struct snd_soc_dapm_path *path = NULL;
 591	struct snd_kcontrol *kcontrol;
 592	struct snd_card *card = dapm->card->snd_card;
 593	const char *prefix;
 594	size_t prefix_len;
 595	int ret;
 596	struct snd_soc_dapm_widget_list *wlist;
 597	int shared, wlistentries;
 598	size_t wlistsize;
 599	const char *name;
 
 
 
 
 
 
 
 
 
 600
 601	if (w->num_kcontrols != 1) {
 602		dev_err(dapm->dev,
 603			"asoc: mux %s has incorrect number of controls\n",
 604			w->name);
 605		return -EINVAL;
 606	}
 607
 608	shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[0],
 609					 &kcontrol);
 610	if (kcontrol) {
 611		wlist = kcontrol->private_data;
 612		wlistentries = wlist->num_widgets + 1;
 613	} else {
 614		wlist = NULL;
 615		wlistentries = 1;
 616	}
 617	wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
 618		wlistentries * sizeof(struct snd_soc_dapm_widget *),
 619	wlist = krealloc(wlist, wlistsize, GFP_KERNEL);
 620	if (wlist == NULL) {
 621		dev_err(dapm->dev,
 622			"asoc: can't allocate widget list for %s\n", w->name);
 623		return -ENOMEM;
 624	}
 625	wlist->num_widgets = wlistentries;
 626	wlist->widgets[wlistentries - 1] = w;
 627
 628	if (!kcontrol) {
 629		if (dapm->codec)
 630			prefix = dapm->codec->name_prefix;
 631		else
 632			prefix = NULL;
 633
 634		if (shared) {
 635			name = w->kcontrol_news[0].name;
 636			prefix_len = 0;
 637		} else {
 638			name = w->name;
 639			if (prefix)
 640				prefix_len = strlen(prefix) + 1;
 641			else
 642				prefix_len = 0;
 643		}
 644
 645		/*
 646		 * The control will get a prefix from the control creation
 647		 * process but we're also using the same prefix for widgets so
 648		 * cut the prefix off the front of the widget name.
 649		 */
 650		kcontrol = snd_soc_cnew(&w->kcontrol_news[0], wlist,
 651					name + prefix_len, prefix);
 652		ret = snd_ctl_add(card, kcontrol);
 653		if (ret < 0) {
 654			dev_err(dapm->dev, "failed to add kcontrol %s: %d\n",
 655				w->name, ret);
 656			kfree(wlist);
 657			return ret;
 658		}
 659	}
 660
 661	kcontrol->private_data = wlist;
 662
 663	w->kcontrols[0] = kcontrol;
 664
 665	list_for_each_entry(path, &w->sources, list_sink)
 666		path->kcontrol = kcontrol;
 667
 668	return 0;
 669}
 670
 671/* create new dapm volume control */
 672static int dapm_new_pga(struct snd_soc_dapm_widget *w)
 673{
 674	if (w->num_kcontrols)
 675		dev_err(w->dapm->dev,
 676			"asoc: PGA controls not supported: '%s'\n", w->name);
 
 
 
 
 677
 678	return 0;
 679}
 680
 681/* reset 'walked' bit for each dapm path */
 682static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
 683{
 684	struct snd_soc_dapm_path *p;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 685
 686	list_for_each_entry(p, &dapm->card->paths, list)
 687		p->walked = 0;
 688}
 689
 690/* We implement power down on suspend by checking the power state of
 691 * the ALSA card - when we are suspending the ALSA state for the card
 692 * is set to D3.
 693 */
 694static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
 695{
 696	int level = snd_power_get_state(widget->dapm->card->snd_card);
 697
 698	switch (level) {
 699	case SNDRV_CTL_POWER_D3hot:
 700	case SNDRV_CTL_POWER_D3cold:
 701		if (widget->ignore_suspend)
 702			dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
 703				widget->name);
 704		return widget->ignore_suspend;
 705	default:
 706		return 1;
 707	}
 708}
 709
 710/* add widget to list if it's not already in the list */
 711static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
 712	struct snd_soc_dapm_widget *w)
 713{
 714	struct snd_soc_dapm_widget_list *wlist;
 715	int wlistsize, wlistentries, i;
 716
 717	if (*list == NULL)
 718		return -EINVAL;
 
 
 
 
 
 719
 720	wlist = *list;
 
 721
 722	/* is this widget already in the list */
 723	for (i = 0; i < wlist->num_widgets; i++) {
 724		if (wlist->widgets[i] == w)
 725			return 0;
 726	}
 727
 728	/* allocate some new space */
 729	wlistentries = wlist->num_widgets + 1;
 730	wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
 731			wlistentries * sizeof(struct snd_soc_dapm_widget *);
 732	*list = krealloc(wlist, wlistsize, GFP_KERNEL);
 733	if (*list == NULL) {
 734		dev_err(w->dapm->dev, "can't allocate widget list for %s\n",
 735			w->name);
 736		return -ENOMEM;
 737	}
 738	wlist = *list;
 739
 740	/* insert the widget */
 741	dev_dbg(w->dapm->dev, "added %s in widget list pos %d\n",
 742			w->name, wlist->num_widgets);
 743
 744	wlist->widgets[wlist->num_widgets] = w;
 745	wlist->num_widgets++;
 746	return 1;
 747}
 748
 749/*
 750 * Recursively check for a completed path to an active or physically connected
 751 * output widget. Returns number of complete paths.
 
 752 */
 753static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
 754	struct snd_soc_dapm_widget_list **list)
 755{
 
 756	struct snd_soc_dapm_path *path;
 757	int con = 0;
 758
 759	if (widget->outputs >= 0)
 760		return widget->outputs;
 761
 762	DAPM_UPDATE_STAT(widget, path_checks);
 763
 764	switch (widget->id) {
 765	case snd_soc_dapm_supply:
 766	case snd_soc_dapm_regulator_supply:
 767		return 0;
 768	default:
 769		break;
 770	}
 771
 772	switch (widget->id) {
 773	case snd_soc_dapm_adc:
 774	case snd_soc_dapm_aif_out:
 775	case snd_soc_dapm_dai:
 776		if (widget->active) {
 777			widget->outputs = snd_soc_dapm_suspend_check(widget);
 778			return widget->outputs;
 779		}
 780	default:
 781		break;
 782	}
 783
 784	if (widget->connected) {
 785		/* connected pin ? */
 786		if (widget->id == snd_soc_dapm_output && !widget->ext) {
 787			widget->outputs = snd_soc_dapm_suspend_check(widget);
 788			return widget->outputs;
 789		}
 790
 791		/* connected jack or spk ? */
 792		if (widget->id == snd_soc_dapm_hp ||
 793		    widget->id == snd_soc_dapm_spk ||
 794		    (widget->id == snd_soc_dapm_line &&
 795		     !list_empty(&widget->sources))) {
 796			widget->outputs = snd_soc_dapm_suspend_check(widget);
 797			return widget->outputs;
 798		}
 799	}
 800
 801	list_for_each_entry(path, &widget->sinks, list_source) {
 802		DAPM_UPDATE_STAT(widget, neighbour_checks);
 803
 804		if (path->weak)
 
 805			continue;
 806
 807		if (path->walked)
 808			continue;
 809
 810		trace_snd_soc_dapm_output_path(widget, path);
 811
 812		if (path->sink && path->connect) {
 813			path->walked = 1;
 814
 815			/* do we need to add this widget to the list ? */
 816			if (list) {
 817				int err;
 818				err = dapm_list_add_widget(list, path->sink);
 819				if (err < 0) {
 820					dev_err(widget->dapm->dev, "could not add widget %s\n",
 821						widget->name);
 822					return con;
 823				}
 824			}
 825
 826			con += is_connected_output_ep(path->sink, list);
 
 
 
 827		}
 828	}
 829
 830	widget->outputs = con;
 831
 832	return con;
 833}
 834
 835/*
 836 * Recursively check for a completed path to an active or physically connected
 837 * input widget. Returns number of complete paths.
 
 
 
 838 */
 839static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
 840	struct snd_soc_dapm_widget_list **list)
 
 
 
 
 
 841{
 
 842	struct snd_soc_dapm_path *path;
 843	int con = 0;
 844
 845	if (widget->inputs >= 0)
 846		return widget->inputs;
 847
 848	DAPM_UPDATE_STAT(widget, path_checks);
 849
 850	switch (widget->id) {
 851	case snd_soc_dapm_supply:
 852	case snd_soc_dapm_regulator_supply:
 853		return 0;
 854	default:
 855		break;
 856	}
 857
 858	/* active stream ? */
 859	switch (widget->id) {
 860	case snd_soc_dapm_dac:
 861	case snd_soc_dapm_aif_in:
 862	case snd_soc_dapm_dai:
 863		if (widget->active) {
 864			widget->inputs = snd_soc_dapm_suspend_check(widget);
 865			return widget->inputs;
 866		}
 867	default:
 868		break;
 869	}
 870
 871	if (widget->connected) {
 872		/* connected pin ? */
 873		if (widget->id == snd_soc_dapm_input && !widget->ext) {
 874			widget->inputs = snd_soc_dapm_suspend_check(widget);
 875			return widget->inputs;
 876		}
 877
 878		/* connected VMID/Bias for lower pops */
 879		if (widget->id == snd_soc_dapm_vmid) {
 880			widget->inputs = snd_soc_dapm_suspend_check(widget);
 881			return widget->inputs;
 882		}
 883
 884		/* connected jack ? */
 885		if (widget->id == snd_soc_dapm_mic ||
 886		    (widget->id == snd_soc_dapm_line &&
 887		     !list_empty(&widget->sinks))) {
 888			widget->inputs = snd_soc_dapm_suspend_check(widget);
 889			return widget->inputs;
 890		}
 891
 892		/* signal generator */
 893		if (widget->id == snd_soc_dapm_siggen) {
 894			widget->inputs = snd_soc_dapm_suspend_check(widget);
 895			return widget->inputs;
 896		}
 897	}
 898
 899	list_for_each_entry(path, &widget->sources, list_sink) {
 900		DAPM_UPDATE_STAT(widget, neighbour_checks);
 901
 902		if (path->weak)
 903			continue;
 904
 905		if (path->walked)
 906			continue;
 907
 908		trace_snd_soc_dapm_input_path(widget, path);
 909
 910		if (path->source && path->connect) {
 911			path->walked = 1;
 912
 913			/* do we need to add this widget to the list ? */
 914			if (list) {
 915				int err;
 916				err = dapm_list_add_widget(list, path->source);
 917				if (err < 0) {
 918					dev_err(widget->dapm->dev, "could not add widget %s\n",
 919						widget->name);
 920					return con;
 921				}
 922			}
 923
 924			con += is_connected_input_ep(path->source, list);
 
 
 
 925		}
 926	}
 927
 928	widget->inputs = con;
 929
 930	return con;
 931}
 932
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 933/**
 934 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
 935 * @dai: the soc DAI.
 936 * @stream: stream direction.
 937 * @list: list of active widgets for this stream.
 
 
 938 *
 939 * Queries DAPM graph as to whether an valid audio stream path exists for
 940 * the initial stream specified by name. This takes into account
 941 * current mixer and mux kcontrol settings. Creates list of valid widgets.
 942 *
 
 
 
 
 
 943 * Returns the number of valid paths or negative error.
 944 */
 945int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
 946	struct snd_soc_dapm_widget_list **list)
 947{
 948	struct snd_soc_card *card = dai->card;
 
 
 
 
 949	int paths;
 
 
 
 
 
 
 
 
 
 
 
 
 
 950
 951	mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
 952	dapm_reset(card);
 953
 954	if (stream == SNDRV_PCM_STREAM_PLAYBACK)
 955		paths = is_connected_output_ep(dai->playback_widget, list);
 956	else
 957		paths = is_connected_input_ep(dai->capture_widget, list);
 958
 959	trace_snd_soc_dapm_connected(paths, stream);
 960	dapm_clear_walk(&card->dapm);
 961	mutex_unlock(&card->dapm_mutex);
 962
 963	return paths;
 964}
 
 
 
 
 
 
 
 965
 966/*
 967 * Handler for generic register modifier widget.
 968 */
 969int dapm_reg_event(struct snd_soc_dapm_widget *w,
 970		   struct snd_kcontrol *kcontrol, int event)
 971{
 972	unsigned int val;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 973
 974	if (SND_SOC_DAPM_EVENT_ON(event))
 975		val = w->on_val;
 976	else
 977		val = w->off_val;
 978
 979	soc_widget_update_bits_locked(w, -(w->reg + 1),
 980			    w->mask << w->shift, val << w->shift);
 981
 982	return 0;
 983}
 984EXPORT_SYMBOL_GPL(dapm_reg_event);
 985
 986/*
 987 * Handler for regulator supply widget.
 988 */
 989int dapm_regulator_event(struct snd_soc_dapm_widget *w,
 990		   struct snd_kcontrol *kcontrol, int event)
 991{
 992	if (SND_SOC_DAPM_EVENT_ON(event))
 993		return regulator_enable(w->regulator);
 994	else
 995		return regulator_disable_deferred(w->regulator, w->shift);
 
 
 
 
 
 
 
 
 
 996}
 997EXPORT_SYMBOL_GPL(dapm_regulator_event);
 998
 999static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1000{
1001	if (w->power_checked)
1002		return w->new_power;
1003
1004	if (w->force)
1005		w->new_power = 1;
1006	else
1007		w->new_power = w->power_check(w);
1008
1009	w->power_checked = true;
1010
1011	return w->new_power;
1012}
1013
1014/* Generic check to see if a widget should be powered.
1015 */
1016static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1017{
1018	int in, out;
1019
1020	DAPM_UPDATE_STAT(w, power_checks);
1021
1022	in = is_connected_input_ep(w, NULL);
1023	dapm_clear_walk(w->dapm);
1024	out = is_connected_output_ep(w, NULL);
1025	dapm_clear_walk(w->dapm);
1026	return out != 0 && in != 0;
1027}
1028
1029static int dapm_dai_check_power(struct snd_soc_dapm_widget *w)
1030{
1031	DAPM_UPDATE_STAT(w, power_checks);
1032
1033	if (w->active)
1034		return w->active;
1035
1036	return dapm_generic_check_power(w);
1037}
1038
1039/* Check to see if an ADC has power */
1040static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
1041{
1042	int in;
1043
1044	DAPM_UPDATE_STAT(w, power_checks);
1045
1046	if (w->active) {
1047		in = is_connected_input_ep(w, NULL);
1048		dapm_clear_walk(w->dapm);
1049		return in != 0;
1050	} else {
1051		return dapm_generic_check_power(w);
1052	}
1053}
1054
1055/* Check to see if a DAC has power */
1056static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
1057{
1058	int out;
1059
1060	DAPM_UPDATE_STAT(w, power_checks);
1061
1062	if (w->active) {
1063		out = is_connected_output_ep(w, NULL);
1064		dapm_clear_walk(w->dapm);
1065		return out != 0;
1066	} else {
1067		return dapm_generic_check_power(w);
1068	}
1069}
1070
1071/* Check to see if a power supply is needed */
1072static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1073{
1074	struct snd_soc_dapm_path *path;
1075
1076	DAPM_UPDATE_STAT(w, power_checks);
1077
1078	/* Check if one of our outputs is connected */
1079	list_for_each_entry(path, &w->sinks, list_source) {
1080		DAPM_UPDATE_STAT(w, neighbour_checks);
1081
1082		if (path->weak)
1083			continue;
1084
1085		if (path->connected &&
1086		    !path->connected(path->source, path->sink))
1087			continue;
1088
1089		if (!path->sink)
1090			continue;
1091
1092		if (dapm_widget_power_check(path->sink))
1093			return 1;
1094	}
1095
1096	dapm_clear_walk(w->dapm);
1097
1098	return 0;
1099}
1100
1101static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1102{
1103	return 1;
1104}
1105
1106static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1107			    struct snd_soc_dapm_widget *b,
1108			    bool power_up)
1109{
1110	int *sort;
1111
 
 
 
1112	if (power_up)
1113		sort = dapm_up_seq;
1114	else
1115		sort = dapm_down_seq;
1116
 
 
 
1117	if (sort[a->id] != sort[b->id])
1118		return sort[a->id] - sort[b->id];
1119	if (a->subseq != b->subseq) {
1120		if (power_up)
1121			return a->subseq - b->subseq;
1122		else
1123			return b->subseq - a->subseq;
1124	}
1125	if (a->reg != b->reg)
1126		return a->reg - b->reg;
1127	if (a->dapm != b->dapm)
1128		return (unsigned long)a->dapm - (unsigned long)b->dapm;
1129
1130	return 0;
1131}
1132
1133/* Insert a widget in order into a DAPM power sequence. */
1134static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1135			    struct list_head *list,
1136			    bool power_up)
1137{
1138	struct snd_soc_dapm_widget *w;
1139
1140	list_for_each_entry(w, list, power_list)
1141		if (dapm_seq_compare(new_widget, w, power_up) < 0) {
1142			list_add_tail(&new_widget->power_list, &w->power_list);
1143			return;
1144		}
1145
1146	list_add_tail(&new_widget->power_list, list);
1147}
1148
1149static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
1150				 struct snd_soc_dapm_widget *w, int event)
1151{
1152	struct snd_soc_card *card = dapm->card;
1153	const char *ev_name;
1154	int power, ret;
1155
1156	switch (event) {
1157	case SND_SOC_DAPM_PRE_PMU:
1158		ev_name = "PRE_PMU";
1159		power = 1;
1160		break;
1161	case SND_SOC_DAPM_POST_PMU:
1162		ev_name = "POST_PMU";
1163		power = 1;
1164		break;
1165	case SND_SOC_DAPM_PRE_PMD:
1166		ev_name = "PRE_PMD";
1167		power = 0;
1168		break;
1169	case SND_SOC_DAPM_POST_PMD:
1170		ev_name = "POST_PMD";
1171		power = 0;
1172		break;
 
 
 
 
 
 
 
 
1173	default:
1174		BUG();
1175		return;
1176	}
1177
1178	if (w->power != power)
1179		return;
1180
1181	if (w->event && (w->event_flags & event)) {
1182		pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
 
 
1183			w->name, ev_name);
 
1184		trace_snd_soc_dapm_widget_event_start(w, event);
1185		ret = w->event(w, NULL, event);
1186		trace_snd_soc_dapm_widget_event_done(w, event);
1187		if (ret < 0)
1188			pr_err("%s: %s event failed: %d\n",
1189			       ev_name, w->name, ret);
1190	}
1191}
1192
1193/* Apply the coalesced changes from a DAPM sequence */
1194static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
1195				   struct list_head *pending)
1196{
1197	struct snd_soc_card *card = dapm->card;
1198	struct snd_soc_dapm_widget *w;
1199	int reg, power;
1200	unsigned int value = 0;
1201	unsigned int mask = 0;
1202	unsigned int cur_mask;
1203
1204	reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1205			       power_list)->reg;
 
1206
1207	list_for_each_entry(w, pending, power_list) {
1208		cur_mask = 1 << w->shift;
1209		BUG_ON(reg != w->reg);
1210
1211		if (w->invert)
1212			power = !w->power;
 
1213		else
1214			power = w->power;
1215
1216		mask |= cur_mask;
1217		if (power)
1218			value |= cur_mask;
1219
1220		pop_dbg(dapm->dev, card->pop_time,
1221			"pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1222			w->name, reg, value, mask);
1223
1224		/* Check for events */
1225		dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
1226		dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
1227	}
1228
1229	if (reg >= 0) {
1230		/* Any widget will do, they should all be updating the
1231		 * same register.
1232		 */
1233		w = list_first_entry(pending, struct snd_soc_dapm_widget,
1234				     power_list);
1235
1236		pop_dbg(dapm->dev, card->pop_time,
1237			"pop test : Applying 0x%x/0x%x to %x in %dms\n",
1238			value, mask, reg, card->pop_time);
1239		pop_wait(card->pop_time);
1240		soc_widget_update_bits_locked(w, reg, mask, value);
1241	}
1242
1243	list_for_each_entry(w, pending, power_list) {
1244		dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
1245		dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
1246	}
1247}
1248
1249/* Apply a DAPM power sequence.
1250 *
1251 * We walk over a pre-sorted list of widgets to apply power to.  In
1252 * order to minimise the number of writes to the device required
1253 * multiple widgets will be updated in a single write where possible.
1254 * Currently anything that requires more than a single write is not
1255 * handled.
1256 */
1257static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
1258			 struct list_head *list, int event, bool power_up)
1259{
1260	struct snd_soc_dapm_widget *w, *n;
 
1261	LIST_HEAD(pending);
1262	int cur_sort = -1;
1263	int cur_subseq = -1;
1264	int cur_reg = SND_SOC_NOPM;
1265	struct snd_soc_dapm_context *cur_dapm = NULL;
1266	int ret, i;
1267	int *sort;
1268
1269	if (power_up)
1270		sort = dapm_up_seq;
1271	else
1272		sort = dapm_down_seq;
1273
1274	list_for_each_entry_safe(w, n, list, power_list) {
1275		ret = 0;
1276
1277		/* Do we need to apply any queued changes? */
1278		if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1279		    w->dapm != cur_dapm || w->subseq != cur_subseq) {
1280			if (!list_empty(&pending))
1281				dapm_seq_run_coalesced(cur_dapm, &pending);
1282
1283			if (cur_dapm && cur_dapm->seq_notifier) {
1284				for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1285					if (sort[i] == cur_sort)
1286						cur_dapm->seq_notifier(cur_dapm,
1287								       i,
1288								       cur_subseq);
1289			}
1290
 
 
 
1291			INIT_LIST_HEAD(&pending);
1292			cur_sort = -1;
1293			cur_subseq = INT_MIN;
1294			cur_reg = SND_SOC_NOPM;
1295			cur_dapm = NULL;
1296		}
1297
1298		switch (w->id) {
1299		case snd_soc_dapm_pre:
1300			if (!w->event)
1301				list_for_each_entry_safe_continue(w, n, list,
1302								  power_list);
1303
1304			if (event == SND_SOC_DAPM_STREAM_START)
1305				ret = w->event(w,
1306					       NULL, SND_SOC_DAPM_PRE_PMU);
1307			else if (event == SND_SOC_DAPM_STREAM_STOP)
1308				ret = w->event(w,
1309					       NULL, SND_SOC_DAPM_PRE_PMD);
1310			break;
1311
1312		case snd_soc_dapm_post:
1313			if (!w->event)
1314				list_for_each_entry_safe_continue(w, n, list,
1315								  power_list);
1316
1317			if (event == SND_SOC_DAPM_STREAM_START)
1318				ret = w->event(w,
1319					       NULL, SND_SOC_DAPM_POST_PMU);
1320			else if (event == SND_SOC_DAPM_STREAM_STOP)
1321				ret = w->event(w,
1322					       NULL, SND_SOC_DAPM_POST_PMD);
1323			break;
1324
1325		default:
1326			/* Queue it up for application */
1327			cur_sort = sort[w->id];
1328			cur_subseq = w->subseq;
1329			cur_reg = w->reg;
1330			cur_dapm = w->dapm;
1331			list_move(&w->power_list, &pending);
1332			break;
1333		}
1334
1335		if (ret < 0)
1336			dev_err(w->dapm->dev,
1337				"Failed to apply widget power: %d\n", ret);
1338	}
1339
1340	if (!list_empty(&pending))
1341		dapm_seq_run_coalesced(cur_dapm, &pending);
1342
1343	if (cur_dapm && cur_dapm->seq_notifier) {
1344		for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1345			if (sort[i] == cur_sort)
1346				cur_dapm->seq_notifier(cur_dapm,
1347						       i, cur_subseq);
 
1348	}
 
 
 
1349}
1350
1351static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
1352{
1353	struct snd_soc_dapm_update *update = dapm->update;
1354	struct snd_soc_dapm_widget *w;
 
 
1355	int ret;
1356
1357	if (!update)
1358		return;
1359
1360	w = update->widget;
1361
1362	if (w->event &&
1363	    (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1364		ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1365		if (ret != 0)
1366			pr_err("%s DAPM pre-event failed: %d\n",
1367			       w->name, ret);
 
1368	}
1369
1370	ret = soc_widget_update_bits_locked(w, update->reg, update->mask,
1371				  update->val);
 
 
 
1372	if (ret < 0)
1373		pr_err("%s DAPM update failed: %d\n", w->name, ret);
 
 
 
 
 
 
 
 
 
 
1374
1375	if (w->event &&
1376	    (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1377		ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1378		if (ret != 0)
1379			pr_err("%s DAPM post-event failed: %d\n",
1380			       w->name, ret);
 
1381	}
1382}
1383
1384/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1385 * they're changing state.
1386 */
1387static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1388{
1389	struct snd_soc_dapm_context *d = data;
1390	int ret;
1391
1392	/* If we're off and we're not supposed to be go into STANDBY */
1393	if (d->bias_level == SND_SOC_BIAS_OFF &&
1394	    d->target_bias_level != SND_SOC_BIAS_OFF) {
1395		if (d->dev)
1396			pm_runtime_get_sync(d->dev);
1397
1398		ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1399		if (ret != 0)
1400			dev_err(d->dev,
1401				"Failed to turn on bias: %d\n", ret);
1402	}
1403
1404	/* Prepare for a STADDBY->ON or ON->STANDBY transition */
1405	if (d->bias_level != d->target_bias_level) {
 
 
 
1406		ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1407		if (ret != 0)
1408			dev_err(d->dev,
1409				"Failed to prepare bias: %d\n", ret);
1410	}
1411}
1412
1413/* Async callback run prior to DAPM sequences - brings to their final
1414 * state.
1415 */
1416static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1417{
1418	struct snd_soc_dapm_context *d = data;
1419	int ret;
1420
1421	/* If we just powered the last thing off drop to standby bias */
1422	if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1423	    (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1424	     d->target_bias_level == SND_SOC_BIAS_OFF)) {
1425		ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1426		if (ret != 0)
1427			dev_err(d->dev, "Failed to apply standby bias: %d\n",
1428				ret);
1429	}
1430
1431	/* If we're in standby and can support bias off then do that */
1432	if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1433	    d->target_bias_level == SND_SOC_BIAS_OFF) {
1434		ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1435		if (ret != 0)
1436			dev_err(d->dev, "Failed to turn off bias: %d\n", ret);
 
1437
1438		if (d->dev)
1439			pm_runtime_put(d->dev);
1440	}
1441
1442	/* If we just powered up then move to active bias */
1443	if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1444	    d->target_bias_level == SND_SOC_BIAS_ON) {
1445		ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1446		if (ret != 0)
1447			dev_err(d->dev, "Failed to apply active bias: %d\n",
1448				ret);
1449	}
1450}
1451
1452static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1453				       bool power, bool connect)
1454{
1455	/* If a connection is being made or broken then that update
1456	 * will have marked the peer dirty, otherwise the widgets are
1457	 * not connected and this update has no impact. */
1458	if (!connect)
1459		return;
1460
1461	/* If the peer is already in the state we're moving to then we
1462	 * won't have an impact on it. */
1463	if (power != peer->power)
1464		dapm_mark_dirty(peer, "peer state change");
1465}
1466
1467static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1468				  struct list_head *up_list,
1469				  struct list_head *down_list)
1470{
1471	struct snd_soc_dapm_path *path;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1472
1473	if (w->power == power)
1474		return;
1475
1476	trace_snd_soc_dapm_widget_power(w, power);
1477
1478	/* If we changed our power state perhaps our neigbours changed
1479	 * also.
 
 
 
 
 
 
 
1480	 */
1481	list_for_each_entry(path, &w->sources, list_sink) {
1482		if (path->source) {
1483			dapm_widget_set_peer_power(path->source, power,
1484						   path->connect);
1485		}
1486	}
1487	switch (w->id) {
1488	case snd_soc_dapm_supply:
1489	case snd_soc_dapm_regulator_supply:
1490		/* Supplies can't affect their outputs, only their inputs */
1491		break;
1492	default:
1493		list_for_each_entry(path, &w->sinks, list_source) {
1494			if (path->sink) {
1495				dapm_widget_set_peer_power(path->sink, power,
1496							   path->connect);
1497			}
1498		}
1499		break;
1500	}
1501
 
1502	if (power)
1503		dapm_seq_insert(w, up_list, true);
1504	else
1505		dapm_seq_insert(w, down_list, false);
1506
1507	w->power = power;
1508}
1509
1510static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1511				  struct list_head *up_list,
1512				  struct list_head *down_list)
1513{
1514	int power;
1515
1516	switch (w->id) {
1517	case snd_soc_dapm_pre:
1518		dapm_seq_insert(w, down_list, false);
1519		break;
1520	case snd_soc_dapm_post:
1521		dapm_seq_insert(w, up_list, true);
1522		break;
1523
 
 
 
 
1524	default:
1525		power = dapm_widget_power_check(w);
1526
1527		dapm_widget_set_power(w, power, up_list, down_list);
1528		break;
1529	}
 
 
1530}
1531
1532/*
1533 * Scan each dapm widget for complete audio path.
1534 * A complete path is a route that has valid endpoints i.e.:-
1535 *
1536 *  o DAC to output pin.
1537 *  o Input Pin to ADC.
1538 *  o Input pin to Output pin (bypass, sidetone)
1539 *  o DAC to ADC (loopback).
1540 */
1541static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
1542{
1543	struct snd_soc_card *card = dapm->card;
1544	struct snd_soc_dapm_widget *w;
1545	struct snd_soc_dapm_context *d;
1546	LIST_HEAD(up_list);
1547	LIST_HEAD(down_list);
1548	LIST_HEAD(async_domain);
1549	enum snd_soc_bias_level bias;
 
 
 
1550
1551	trace_snd_soc_dapm_start(card);
1552
1553	list_for_each_entry(d, &card->dapm_list, list) {
1554		if (d->idle_bias_off)
1555			d->target_bias_level = SND_SOC_BIAS_OFF;
1556		else
1557			d->target_bias_level = SND_SOC_BIAS_STANDBY;
1558	}
1559
1560	dapm_reset(card);
1561
1562	/* Check which widgets we need to power and store them in
1563	 * lists indicating if they should be powered up or down.  We
1564	 * only check widgets that have been flagged as dirty but note
1565	 * that new widgets may be added to the dirty list while we
1566	 * iterate.
1567	 */
1568	list_for_each_entry(w, &card->dapm_dirty, dirty) {
1569		dapm_power_one_widget(w, &up_list, &down_list);
1570	}
1571
1572	list_for_each_entry(w, &card->widgets, list) {
1573		switch (w->id) {
1574		case snd_soc_dapm_pre:
1575		case snd_soc_dapm_post:
1576			/* These widgets always need to be powered */
1577			break;
1578		default:
1579			list_del_init(&w->dirty);
1580			break;
1581		}
1582
1583		if (w->power) {
1584			d = w->dapm;
1585
1586			/* Supplies and micbiases only bring the
1587			 * context up to STANDBY as unless something
1588			 * else is active and passing audio they
1589			 * generally don't require full power.  Signal
1590			 * generators are virtual pins and have no
1591			 * power impact themselves.
1592			 */
1593			switch (w->id) {
1594			case snd_soc_dapm_siggen:
 
1595				break;
1596			case snd_soc_dapm_supply:
1597			case snd_soc_dapm_regulator_supply:
 
 
1598			case snd_soc_dapm_micbias:
1599				if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1600					d->target_bias_level = SND_SOC_BIAS_STANDBY;
1601				break;
1602			default:
1603				d->target_bias_level = SND_SOC_BIAS_ON;
1604				break;
1605			}
1606		}
1607
1608	}
1609
1610	/* Force all contexts in the card to the same bias state if
1611	 * they're not ground referenced.
1612	 */
1613	bias = SND_SOC_BIAS_OFF;
1614	list_for_each_entry(d, &card->dapm_list, list)
1615		if (d->target_bias_level > bias)
1616			bias = d->target_bias_level;
1617	list_for_each_entry(d, &card->dapm_list, list)
1618		if (!d->idle_bias_off)
1619			d->target_bias_level = bias;
1620
1621	trace_snd_soc_dapm_walk_done(card);
1622
1623	/* Run all the bias changes in parallel */
1624	list_for_each_entry(d, &dapm->card->dapm_list, list)
1625		async_schedule_domain(dapm_pre_sequence_async, d,
1626					&async_domain);
 
 
 
 
1627	async_synchronize_full_domain(&async_domain);
1628
 
 
 
 
 
 
 
 
1629	/* Power down widgets first; try to avoid amplifying pops. */
1630	dapm_seq_run(dapm, &down_list, event, false);
1631
1632	dapm_widget_update(dapm);
1633
1634	/* Now power up. */
1635	dapm_seq_run(dapm, &up_list, event, true);
1636
1637	/* Run all the bias changes in parallel */
1638	list_for_each_entry(d, &dapm->card->dapm_list, list)
1639		async_schedule_domain(dapm_post_sequence_async, d,
1640					&async_domain);
 
 
1641	async_synchronize_full_domain(&async_domain);
 
 
1642
1643	/* do we need to notify any clients that DAPM event is complete */
1644	list_for_each_entry(d, &card->dapm_list, list) {
1645		if (d->stream_event)
1646			d->stream_event(d, event);
 
 
 
 
1647	}
1648
1649	pop_dbg(dapm->dev, card->pop_time,
1650		"DAPM sequencing finished, waiting %dms\n", card->pop_time);
1651	pop_wait(card->pop_time);
1652
1653	trace_snd_soc_dapm_done(card);
1654
1655	return 0;
1656}
1657
1658#ifdef CONFIG_DEBUG_FS
1659static ssize_t dapm_widget_power_read_file(struct file *file,
1660					   char __user *user_buf,
1661					   size_t count, loff_t *ppos)
1662{
1663	struct snd_soc_dapm_widget *w = file->private_data;
 
1664	char *buf;
1665	int in, out;
1666	ssize_t ret;
1667	struct snd_soc_dapm_path *p = NULL;
1668
1669	buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1670	if (!buf)
1671		return -ENOMEM;
1672
1673	in = is_connected_input_ep(w, NULL);
1674	dapm_clear_walk(w->dapm);
1675	out = is_connected_output_ep(w, NULL);
1676	dapm_clear_walk(w->dapm);
1677
1678	ret = snprintf(buf, PAGE_SIZE, "%s: %s%s  in %d out %d",
 
 
 
 
 
 
 
 
 
1679		       w->name, w->power ? "On" : "Off",
1680		       w->force ? " (forced)" : "", in, out);
1681
1682	if (w->reg >= 0)
1683		ret += snprintf(buf + ret, PAGE_SIZE - ret,
1684				" - R%d(0x%x) bit %d",
1685				w->reg, w->reg, w->shift);
1686
1687	ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1688
1689	if (w->sname)
1690		ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1691				w->sname,
1692				w->active ? "active" : "inactive");
1693
1694	list_for_each_entry(p, &w->sources, list_sink) {
1695		if (p->connected && !p->connected(w, p->sink))
1696			continue;
 
 
 
 
 
1697
1698		if (p->connect)
1699			ret += snprintf(buf + ret, PAGE_SIZE - ret,
1700					" in  \"%s\" \"%s\"\n",
1701					p->name ? p->name : "static",
1702					p->source->name);
 
1703	}
1704	list_for_each_entry(p, &w->sinks, list_source) {
1705		if (p->connected && !p->connected(w, p->sink))
1706			continue;
1707
1708		if (p->connect)
1709			ret += snprintf(buf + ret, PAGE_SIZE - ret,
1710					" out \"%s\" \"%s\"\n",
1711					p->name ? p->name : "static",
1712					p->sink->name);
1713	}
1714
1715	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1716
1717	kfree(buf);
1718	return ret;
1719}
1720
1721static const struct file_operations dapm_widget_power_fops = {
1722	.open = simple_open,
1723	.read = dapm_widget_power_read_file,
1724	.llseek = default_llseek,
1725};
1726
1727static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1728				   size_t count, loff_t *ppos)
1729{
1730	struct snd_soc_dapm_context *dapm = file->private_data;
1731	char *level;
1732
1733	switch (dapm->bias_level) {
1734	case SND_SOC_BIAS_ON:
1735		level = "On\n";
1736		break;
1737	case SND_SOC_BIAS_PREPARE:
1738		level = "Prepare\n";
1739		break;
1740	case SND_SOC_BIAS_STANDBY:
1741		level = "Standby\n";
1742		break;
1743	case SND_SOC_BIAS_OFF:
1744		level = "Off\n";
1745		break;
1746	default:
1747		BUG();
1748		level = "Unknown\n";
1749		break;
1750	}
1751
1752	return simple_read_from_buffer(user_buf, count, ppos, level,
1753				       strlen(level));
1754}
1755
1756static const struct file_operations dapm_bias_fops = {
1757	.open = simple_open,
1758	.read = dapm_bias_read_file,
1759	.llseek = default_llseek,
1760};
1761
1762void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1763	struct dentry *parent)
1764{
1765	struct dentry *d;
 
1766
1767	dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1768
1769	if (!dapm->debugfs_dapm) {
1770		dev_warn(dapm->dev,
1771		       "Failed to create DAPM debugfs directory\n");
 
 
 
 
 
 
1772		return;
1773	}
1774
1775	d = debugfs_create_file("bias_level", 0444,
1776				dapm->debugfs_dapm, dapm,
1777				&dapm_bias_fops);
1778	if (!d)
1779		dev_warn(dapm->dev,
1780			 "ASoC: Failed to create bias level debugfs file\n");
1781}
1782
1783static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1784{
1785	struct snd_soc_dapm_context *dapm = w->dapm;
1786	struct dentry *d;
1787
1788	if (!dapm->debugfs_dapm || !w->name)
1789		return;
1790
1791	d = debugfs_create_file(w->name, 0444,
1792				dapm->debugfs_dapm, w,
1793				&dapm_widget_power_fops);
1794	if (!d)
1795		dev_warn(w->dapm->dev,
1796			"ASoC: Failed to create %s debugfs file\n",
1797			w->name);
1798}
1799
1800static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1801{
1802	debugfs_remove_recursive(dapm->debugfs_dapm);
 
1803}
1804
1805#else
1806void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1807	struct dentry *parent)
1808{
1809}
1810
1811static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1812{
1813}
1814
 
 
 
 
1815static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1816{
1817}
1818
1819#endif
1820
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1821/* test and update the power status of a mux widget */
1822static int soc_dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1823				 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
1824{
1825	struct snd_soc_dapm_path *path;
1826	int found = 0;
 
1827
1828	if (widget->id != snd_soc_dapm_mux &&
1829	    widget->id != snd_soc_dapm_virt_mux &&
1830	    widget->id != snd_soc_dapm_value_mux)
1831		return -ENODEV;
1832
1833	/* find dapm widget path assoc with kcontrol */
1834	list_for_each_entry(path, &widget->dapm->card->paths, list) {
1835		if (path->kcontrol != kcontrol)
1836			continue;
1837
1838		if (!path->name || !e->texts[mux])
1839			continue;
1840
1841		found = 1;
1842		/* we now need to match the string in the enum to the path */
1843		if (!(strcmp(path->name, e->texts[mux]))) {
1844			path->connect = 1; /* new connection */
1845			dapm_mark_dirty(path->source, "mux connection");
1846		} else {
1847			if (path->connect)
1848				dapm_mark_dirty(path->source,
1849						"mux disconnection");
1850			path->connect = 0; /* old connection must be powered down */
1851		}
1852	}
1853
1854	if (found) {
1855		dapm_mark_dirty(widget, "mux change");
1856		dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1857	}
1858
1859	return found;
1860}
1861
1862int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1863		struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
 
1864{
1865	struct snd_soc_card *card = widget->dapm->card;
1866	int ret;
1867
1868	mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1869	ret = soc_dapm_mux_update_power(widget, kcontrol, mux, e);
1870	mutex_unlock(&card->dapm_mutex);
 
 
1871	if (ret > 0)
1872		soc_dpcm_runtime_update(widget);
1873	return ret;
1874}
1875EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
1876
1877/* test and update the power status of a mixer or switch widget */
1878static int soc_dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
1879				   struct snd_kcontrol *kcontrol, int connect)
 
1880{
1881	struct snd_soc_dapm_path *path;
1882	int found = 0;
1883
1884	if (widget->id != snd_soc_dapm_mixer &&
1885	    widget->id != snd_soc_dapm_mixer_named_ctl &&
1886	    widget->id != snd_soc_dapm_switch)
1887		return -ENODEV;
1888
1889	/* find dapm widget path assoc with kcontrol */
1890	list_for_each_entry(path, &widget->dapm->card->paths, list) {
1891		if (path->kcontrol != kcontrol)
1892			continue;
1893
1894		/* found, now check type */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1895		found = 1;
1896		path->connect = connect;
1897		dapm_mark_dirty(path->source, "mixer connection");
1898	}
1899
1900	if (found) {
1901		dapm_mark_dirty(widget, "mixer update");
1902		dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1903	}
1904
1905	return found;
1906}
1907
1908int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
1909				struct snd_kcontrol *kcontrol, int connect)
 
1910{
1911	struct snd_soc_card *card = widget->dapm->card;
1912	int ret;
1913
1914	mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1915	ret = soc_dapm_mixer_update_power(widget, kcontrol, connect);
1916	mutex_unlock(&card->dapm_mutex);
 
 
1917	if (ret > 0)
1918		soc_dpcm_runtime_update(widget);
1919	return ret;
1920}
1921EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
1922
1923/* show dapm widget status in sys fs */
1924static ssize_t dapm_widget_show(struct device *dev,
1925	struct device_attribute *attr, char *buf)
1926{
1927	struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
1928	struct snd_soc_codec *codec =rtd->codec;
1929	struct snd_soc_dapm_widget *w;
1930	int count = 0;
1931	char *state = "not set";
1932
1933	list_for_each_entry(w, &codec->card->widgets, list) {
1934		if (w->dapm != &codec->dapm)
 
 
 
 
 
 
 
1935			continue;
1936
1937		/* only display widgets that burnm power */
1938		switch (w->id) {
1939		case snd_soc_dapm_hp:
1940		case snd_soc_dapm_mic:
1941		case snd_soc_dapm_spk:
1942		case snd_soc_dapm_line:
1943		case snd_soc_dapm_micbias:
1944		case snd_soc_dapm_dac:
1945		case snd_soc_dapm_adc:
1946		case snd_soc_dapm_pga:
 
1947		case snd_soc_dapm_out_drv:
1948		case snd_soc_dapm_mixer:
1949		case snd_soc_dapm_mixer_named_ctl:
1950		case snd_soc_dapm_supply:
1951		case snd_soc_dapm_regulator_supply:
 
 
1952			if (w->name)
1953				count += sprintf(buf + count, "%s: %s\n",
1954					w->name, w->power ? "On":"Off");
1955		break;
1956		default:
1957		break;
1958		}
1959	}
1960
1961	switch (codec->dapm.bias_level) {
1962	case SND_SOC_BIAS_ON:
1963		state = "On";
1964		break;
1965	case SND_SOC_BIAS_PREPARE:
1966		state = "Prepare";
1967		break;
1968	case SND_SOC_BIAS_STANDBY:
1969		state = "Standby";
1970		break;
1971	case SND_SOC_BIAS_OFF:
1972		state = "Off";
1973		break;
1974	}
1975	count += sprintf(buf + count, "PM State: %s\n", state);
1976
1977	return count;
1978}
1979
1980static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1981
1982int snd_soc_dapm_sys_add(struct device *dev)
 
 
 
 
 
 
 
 
 
 
1983{
1984	return device_create_file(dev, &dev_attr_dapm_widget);
 
 
 
 
1985}
1986
1987static void snd_soc_dapm_sys_remove(struct device *dev)
 
 
 
 
 
 
1988{
1989	device_remove_file(dev, &dev_attr_dapm_widget);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1990}
 
1991
1992/* free all dapm widgets and resources */
1993static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
1994{
1995	struct snd_soc_dapm_widget *w, *next_w;
1996	struct snd_soc_dapm_path *p, *next_p;
1997
1998	list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1999		if (w->dapm != dapm)
2000			continue;
2001		list_del(&w->list);
2002		/*
2003		 * remove source and sink paths associated to this widget.
2004		 * While removing the path, remove reference to it from both
2005		 * source and sink widgets so that path is removed only once.
2006		 */
2007		list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
2008			list_del(&p->list_sink);
2009			list_del(&p->list_source);
2010			list_del(&p->list);
2011			kfree(p->long_name);
2012			kfree(p);
2013		}
2014		list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
2015			list_del(&p->list_sink);
2016			list_del(&p->list_source);
2017			list_del(&p->list);
2018			kfree(p->long_name);
2019			kfree(p);
2020		}
2021		kfree(w->kcontrols);
2022		kfree(w->name);
2023		kfree(w);
2024	}
 
 
 
2025}
2026
2027static struct snd_soc_dapm_widget *dapm_find_widget(
2028			struct snd_soc_dapm_context *dapm, const char *pin,
2029			bool search_other_contexts)
2030{
2031	struct snd_soc_dapm_widget *w;
2032	struct snd_soc_dapm_widget *fallback = NULL;
 
 
 
 
 
 
 
 
 
 
 
2033
2034	list_for_each_entry(w, &dapm->card->widgets, list) {
2035		if (!strcmp(w->name, pin)) {
2036			if (w->dapm == dapm)
2037				return w;
2038			else
2039				fallback = w;
2040		}
2041	}
2042
2043	if (search_other_contexts)
2044		return fallback;
2045
2046	return NULL;
2047}
2048
2049static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2050				const char *pin, int status)
 
 
 
 
 
2051{
2052	struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
 
 
 
2053
2054	if (!w) {
2055		dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2056		return -EINVAL;
2057	}
2058
2059	if (w->connected != status)
2060		dapm_mark_dirty(w, "pin configuration");
 
 
 
 
2061
2062	w->connected = status;
2063	if (status == 0)
2064		w->force = 0;
2065
2066	return 0;
 
 
 
 
 
 
 
 
 
 
 
 
2067}
2068
2069/**
2070 * snd_soc_dapm_sync - scan and power dapm paths
2071 * @dapm: DAPM context
2072 *
2073 * Walks all dapm audio paths and powers widgets according to their
2074 * stream or path usage.
2075 *
 
 
2076 * Returns 0 for success.
2077 */
2078int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
2079{
2080	int ret;
2081
2082	/*
2083	 * Suppress early reports (eg, jacks syncing their state) to avoid
2084	 * silly DAPM runs during card startup.
2085	 */
2086	if (!dapm->card || !dapm->card->instantiated)
2087		return 0;
2088
2089	mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2090	ret = dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
2091	mutex_unlock(&dapm->card->dapm_mutex);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2092	return ret;
2093}
2094EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
2095
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2096static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
2097				  const struct snd_soc_dapm_route *route)
2098{
2099	struct snd_soc_dapm_path *path;
2100	struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2101	struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2102	const char *sink;
2103	const char *control = route->control;
2104	const char *source;
2105	char prefixed_sink[80];
2106	char prefixed_source[80];
2107	int ret = 0;
 
 
 
2108
2109	if (dapm->codec && dapm->codec->name_prefix) {
 
2110		snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2111			 dapm->codec->name_prefix, route->sink);
2112		sink = prefixed_sink;
2113		snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2114			 dapm->codec->name_prefix, route->source);
2115		source = prefixed_source;
2116	} else {
2117		sink = route->sink;
2118		source = route->source;
2119	}
2120
 
 
 
 
 
 
2121	/*
2122	 * find src and dest widgets over all widgets but favor a widget from
2123	 * current DAPM context
2124	 */
2125	list_for_each_entry(w, &dapm->card->widgets, list) {
2126		if (!wsink && !(strcmp(w->name, sink))) {
2127			wtsink = w;
2128			if (w->dapm == dapm)
2129				wsink = w;
 
 
 
 
 
 
 
 
2130			continue;
2131		}
2132		if (!wsource && !(strcmp(w->name, source))) {
2133			wtsource = w;
2134			if (w->dapm == dapm)
2135				wsource = w;
 
 
 
 
 
 
 
 
2136		}
2137	}
2138	/* use widget from another DAPM context if not found from this */
2139	if (!wsink)
2140		wsink = wtsink;
2141	if (!wsource)
2142		wsource = wtsource;
2143
2144	if (wsource == NULL || wsink == NULL)
2145		return -ENODEV;
 
 
 
 
 
 
 
 
2146
2147	path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2148	if (!path)
2149		return -ENOMEM;
 
 
 
 
 
 
 
 
 
2150
2151	path->source = wsource;
2152	path->sink = wsink;
2153	path->connected = route->connected;
2154	INIT_LIST_HEAD(&path->list);
2155	INIT_LIST_HEAD(&path->list_source);
2156	INIT_LIST_HEAD(&path->list_sink);
 
 
 
2157
2158	/* check for external widgets */
2159	if (wsink->id == snd_soc_dapm_input) {
2160		if (wsource->id == snd_soc_dapm_micbias ||
2161			wsource->id == snd_soc_dapm_mic ||
2162			wsource->id == snd_soc_dapm_line ||
2163			wsource->id == snd_soc_dapm_output)
2164			wsink->ext = 1;
2165	}
2166	if (wsource->id == snd_soc_dapm_output) {
2167		if (wsink->id == snd_soc_dapm_spk ||
2168			wsink->id == snd_soc_dapm_hp ||
2169			wsink->id == snd_soc_dapm_line ||
2170			wsink->id == snd_soc_dapm_input)
2171			wsource->ext = 1;
2172	}
2173
2174	/* connect static paths */
2175	if (control == NULL) {
2176		list_add(&path->list, &dapm->card->paths);
2177		list_add(&path->list_sink, &wsink->sources);
2178		list_add(&path->list_source, &wsource->sinks);
2179		path->connect = 1;
2180		return 0;
 
 
 
 
2181	}
2182
2183	/* connect dynamic paths */
2184	switch (wsink->id) {
2185	case snd_soc_dapm_adc:
2186	case snd_soc_dapm_dac:
2187	case snd_soc_dapm_pga:
2188	case snd_soc_dapm_out_drv:
2189	case snd_soc_dapm_input:
2190	case snd_soc_dapm_output:
2191	case snd_soc_dapm_siggen:
2192	case snd_soc_dapm_micbias:
2193	case snd_soc_dapm_vmid:
2194	case snd_soc_dapm_pre:
2195	case snd_soc_dapm_post:
2196	case snd_soc_dapm_supply:
2197	case snd_soc_dapm_regulator_supply:
2198	case snd_soc_dapm_aif_in:
2199	case snd_soc_dapm_aif_out:
2200	case snd_soc_dapm_dai:
2201	case snd_soc_dapm_dai_link:
2202		list_add(&path->list, &dapm->card->paths);
2203		list_add(&path->list_sink, &wsink->sources);
2204		list_add(&path->list_source, &wsource->sinks);
2205		path->connect = 1;
2206		return 0;
2207	case snd_soc_dapm_mux:
2208	case snd_soc_dapm_virt_mux:
2209	case snd_soc_dapm_value_mux:
2210		ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
2211			&wsink->kcontrol_news[0]);
2212		if (ret != 0)
2213			goto err;
2214		break;
2215	case snd_soc_dapm_switch:
2216	case snd_soc_dapm_mixer:
2217	case snd_soc_dapm_mixer_named_ctl:
2218		ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
2219		if (ret != 0)
2220			goto err;
2221		break;
2222	case snd_soc_dapm_hp:
2223	case snd_soc_dapm_mic:
2224	case snd_soc_dapm_line:
2225	case snd_soc_dapm_spk:
2226		list_add(&path->list, &dapm->card->paths);
2227		list_add(&path->list_sink, &wsink->sources);
2228		list_add(&path->list_source, &wsource->sinks);
2229		path->connect = 0;
2230		return 0;
2231	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2232	return 0;
2233
2234err:
2235	dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
2236		 source, control, sink);
2237	kfree(path);
2238	return ret;
2239}
2240
2241/**
2242 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
2243 * @dapm: DAPM context
2244 * @route: audio routes
2245 * @num: number of routes
2246 *
2247 * Connects 2 dapm widgets together via a named audio path. The sink is
2248 * the widget receiving the audio signal, whilst the source is the sender
2249 * of the audio signal.
2250 *
2251 * Returns 0 for success else error. On error all resources can be freed
2252 * with a call to snd_soc_card_free().
2253 */
2254int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
2255			    const struct snd_soc_dapm_route *route, int num)
2256{
2257	int i, ret = 0;
2258
2259	mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2260	for (i = 0; i < num; i++) {
2261		ret = snd_soc_dapm_add_route(dapm, route);
2262		if (ret < 0) {
2263			dev_err(dapm->dev, "Failed to add route %s->%s\n",
2264				route->source, route->sink);
2265			break;
2266		}
2267		route++;
2268	}
2269	mutex_unlock(&dapm->card->dapm_mutex);
2270
2271	return ret;
2272}
2273EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2274
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2275static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2276				   const struct snd_soc_dapm_route *route)
2277{
2278	struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2279							      route->source,
2280							      true);
2281	struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2282							    route->sink,
2283							    true);
2284	struct snd_soc_dapm_path *path;
2285	int count = 0;
2286
2287	if (!source) {
2288		dev_err(dapm->dev, "Unable to find source %s for weak route\n",
2289			route->source);
2290		return -ENODEV;
2291	}
2292
2293	if (!sink) {
2294		dev_err(dapm->dev, "Unable to find sink %s for weak route\n",
2295			route->sink);
2296		return -ENODEV;
2297	}
2298
2299	if (route->control || route->connected)
2300		dev_warn(dapm->dev, "Ignoring control for weak route %s->%s\n",
2301			 route->source, route->sink);
2302
2303	list_for_each_entry(path, &source->sinks, list_source) {
2304		if (path->sink == sink) {
2305			path->weak = 1;
2306			count++;
2307		}
2308	}
2309
2310	if (count == 0)
2311		dev_err(dapm->dev, "No path found for weak route %s->%s\n",
2312			route->source, route->sink);
2313	if (count > 1)
2314		dev_warn(dapm->dev, "%d paths found for weak route %s->%s\n",
2315			 count, route->source, route->sink);
2316
2317	return 0;
2318}
2319
2320/**
2321 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2322 * @dapm: DAPM context
2323 * @route: audio routes
2324 * @num: number of routes
2325 *
2326 * Mark existing routes matching those specified in the passed array
2327 * as being weak, meaning that they are ignored for the purpose of
2328 * power decisions.  The main intended use case is for sidetone paths
2329 * which couple audio between other independent paths if they are both
2330 * active in order to make the combination work better at the user
2331 * level but which aren't intended to be "used".
2332 *
2333 * Note that CODEC drivers should not use this as sidetone type paths
2334 * can frequently also be used as bypass paths.
2335 */
2336int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2337			     const struct snd_soc_dapm_route *route, int num)
2338{
2339	int i, err;
2340	int ret = 0;
2341
2342	mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2343	for (i = 0; i < num; i++) {
2344		err = snd_soc_dapm_weak_route(dapm, route);
2345		if (err)
2346			ret = err;
2347		route++;
2348	}
2349	mutex_unlock(&dapm->card->dapm_mutex);
2350
2351	return ret;
2352}
2353EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2354
2355/**
2356 * snd_soc_dapm_new_widgets - add new dapm widgets
2357 * @dapm: DAPM context
2358 *
2359 * Checks the codec for any new dapm widgets and creates them if found.
2360 *
2361 * Returns 0 for success.
2362 */
2363int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
2364{
2365	struct snd_soc_dapm_widget *w;
2366	unsigned int val;
2367
2368	mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2369
2370	list_for_each_entry(w, &dapm->card->widgets, list)
2371	{
2372		if (w->new)
2373			continue;
2374
2375		if (w->num_kcontrols) {
2376			w->kcontrols = kzalloc(w->num_kcontrols *
2377						sizeof(struct snd_kcontrol *),
2378						GFP_KERNEL);
2379			if (!w->kcontrols) {
2380				mutex_unlock(&dapm->card->dapm_mutex);
2381				return -ENOMEM;
2382			}
2383		}
2384
2385		switch(w->id) {
2386		case snd_soc_dapm_switch:
2387		case snd_soc_dapm_mixer:
2388		case snd_soc_dapm_mixer_named_ctl:
2389			dapm_new_mixer(w);
2390			break;
2391		case snd_soc_dapm_mux:
2392		case snd_soc_dapm_virt_mux:
2393		case snd_soc_dapm_value_mux:
2394			dapm_new_mux(w);
2395			break;
2396		case snd_soc_dapm_pga:
 
2397		case snd_soc_dapm_out_drv:
2398			dapm_new_pga(w);
2399			break;
 
 
 
2400		default:
2401			break;
2402		}
2403
2404		/* Read the initial power state from the device */
2405		if (w->reg >= 0) {
2406			val = soc_widget_read(w, w->reg);
2407			val &= 1 << w->shift;
2408			if (w->invert)
2409				val = !val;
2410
2411			if (val)
2412				w->power = 1;
2413		}
2414
2415		w->new = 1;
2416
2417		dapm_mark_dirty(w, "new widget");
2418		dapm_debugfs_add_widget(w);
2419	}
2420
2421	dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
2422	mutex_unlock(&dapm->card->dapm_mutex);
2423	return 0;
2424}
2425EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2426
2427/**
2428 * snd_soc_dapm_get_volsw - dapm mixer get callback
2429 * @kcontrol: mixer control
2430 * @ucontrol: control element information
2431 *
2432 * Callback to get the value of a dapm mixer control.
2433 *
2434 * Returns 0 for success.
2435 */
2436int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2437	struct snd_ctl_elem_value *ucontrol)
2438{
2439	struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2440	struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2441	struct soc_mixer_control *mc =
2442		(struct soc_mixer_control *)kcontrol->private_value;
2443	unsigned int reg = mc->reg;
2444	unsigned int shift = mc->shift;
2445	unsigned int rshift = mc->rshift;
2446	int max = mc->max;
 
 
2447	unsigned int invert = mc->invert;
2448	unsigned int mask = (1 << fls(max)) - 1;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2449
2450	ucontrol->value.integer.value[0] =
2451		(snd_soc_read(widget->codec, reg) >> shift) & mask;
2452	if (shift != rshift)
2453		ucontrol->value.integer.value[1] =
2454			(snd_soc_read(widget->codec, reg) >> rshift) & mask;
2455	if (invert) {
2456		ucontrol->value.integer.value[0] =
2457			max - ucontrol->value.integer.value[0];
2458		if (shift != rshift)
2459			ucontrol->value.integer.value[1] =
2460				max - ucontrol->value.integer.value[1];
2461	}
2462
2463	return 0;
2464}
2465EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2466
2467/**
2468 * snd_soc_dapm_put_volsw - dapm mixer set callback
2469 * @kcontrol: mixer control
2470 * @ucontrol: control element information
2471 *
2472 * Callback to set the value of a dapm mixer control.
2473 *
2474 * Returns 0 for success.
2475 */
2476int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2477	struct snd_ctl_elem_value *ucontrol)
2478{
2479	struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2480	struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2481	struct snd_soc_codec *codec = widget->codec;
2482	struct snd_soc_card *card = codec->card;
2483	struct soc_mixer_control *mc =
2484		(struct soc_mixer_control *)kcontrol->private_value;
2485	unsigned int reg = mc->reg;
2486	unsigned int shift = mc->shift;
2487	int max = mc->max;
2488	unsigned int mask = (1 << fls(max)) - 1;
 
2489	unsigned int invert = mc->invert;
2490	unsigned int val;
2491	int connect, change;
2492	struct snd_soc_dapm_update update;
2493	int wi;
2494
2495	val = (ucontrol->value.integer.value[0] & mask);
 
2496
2497	if (invert)
2498		val = max - val;
2499	mask = mask << shift;
2500	val = val << shift;
2501
2502	if (val)
2503		/* new connection */
2504		connect = invert ? 0 : 1;
2505	else
2506		/* old connection must be powered down */
2507		connect = invert ? 1 : 0;
2508
2509	mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2510
2511	change = snd_soc_test_bits(widget->codec, reg, mask, val);
2512	if (change) {
2513		for (wi = 0; wi < wlist->num_widgets; wi++) {
2514			widget = wlist->widgets[wi];
2515
2516			widget->value = val;
2517
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2518			update.kcontrol = kcontrol;
2519			update.widget = widget;
2520			update.reg = reg;
2521			update.mask = mask;
2522			update.val = val;
2523			widget->dapm->update = &update;
 
2524
2525			soc_dapm_mixer_update_power(widget, kcontrol, connect);
 
2526
2527			widget->dapm->update = NULL;
2528		}
2529	}
2530
2531	mutex_unlock(&card->dapm_mutex);
2532	return 0;
 
 
 
 
2533}
2534EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2535
2536/**
2537 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2538 * @kcontrol: mixer control
2539 * @ucontrol: control element information
2540 *
2541 * Callback to get the value of a dapm enumerated double mixer control.
2542 *
2543 * Returns 0 for success.
2544 */
2545int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2546	struct snd_ctl_elem_value *ucontrol)
2547{
2548	struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2549	struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2550	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2551	unsigned int val, bitmask;
2552
2553	for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2554		;
2555	val = snd_soc_read(widget->codec, e->reg);
2556	ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
2557	if (e->shift_l != e->shift_r)
2558		ucontrol->value.enumerated.item[1] =
2559			(val >> e->shift_r) & (bitmask - 1);
2560
2561	return 0;
2562}
2563EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2564
2565/**
2566 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2567 * @kcontrol: mixer control
2568 * @ucontrol: control element information
2569 *
2570 * Callback to set the value of a dapm enumerated double mixer control.
2571 *
2572 * Returns 0 for success.
2573 */
2574int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2575	struct snd_ctl_elem_value *ucontrol)
2576{
2577	struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2578	struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2579	struct snd_soc_codec *codec = widget->codec;
2580	struct snd_soc_card *card = codec->card;
2581	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2582	unsigned int val, mux, change;
2583	unsigned int mask, bitmask;
2584	struct snd_soc_dapm_update update;
2585	int wi;
2586
2587	for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2588		;
2589	if (ucontrol->value.enumerated.item[0] > e->max - 1)
2590		return -EINVAL;
2591	mux = ucontrol->value.enumerated.item[0];
2592	val = mux << e->shift_l;
2593	mask = (bitmask - 1) << e->shift_l;
2594	if (e->shift_l != e->shift_r) {
2595		if (ucontrol->value.enumerated.item[1] > e->max - 1)
2596			return -EINVAL;
2597		val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2598		mask |= (bitmask - 1) << e->shift_r;
2599	}
2600
2601	mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2602
2603	change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
2604	if (change) {
2605		for (wi = 0; wi < wlist->num_widgets; wi++) {
2606			widget = wlist->widgets[wi];
2607
2608			widget->value = val;
2609
2610			update.kcontrol = kcontrol;
2611			update.widget = widget;
2612			update.reg = e->reg;
2613			update.mask = mask;
2614			update.val = val;
2615			widget->dapm->update = &update;
2616
2617			soc_dapm_mux_update_power(widget, kcontrol, mux, e);
2618
2619			widget->dapm->update = NULL;
2620		}
2621	}
2622
2623	mutex_unlock(&card->dapm_mutex);
2624	return change;
2625}
2626EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2627
2628/**
2629 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2630 * @kcontrol: mixer control
2631 * @ucontrol: control element information
2632 *
2633 * Returns 0 for success.
2634 */
2635int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2636			       struct snd_ctl_elem_value *ucontrol)
2637{
2638	struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2639	struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2640
2641	ucontrol->value.enumerated.item[0] = widget->value;
2642
2643	return 0;
2644}
2645EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2646
2647/**
2648 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2649 * @kcontrol: mixer control
2650 * @ucontrol: control element information
2651 *
2652 * Returns 0 for success.
2653 */
2654int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2655			       struct snd_ctl_elem_value *ucontrol)
2656{
2657	struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2658	struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2659	struct snd_soc_codec *codec = widget->codec;
2660	struct snd_soc_card *card = codec->card;
2661	struct soc_enum *e =
2662		(struct soc_enum *)kcontrol->private_value;
2663	int change;
2664	int ret = 0;
2665	int wi;
2666
2667	if (ucontrol->value.enumerated.item[0] >= e->max)
2668		return -EINVAL;
2669
2670	mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2671
2672	change = widget->value != ucontrol->value.enumerated.item[0];
2673	if (change) {
2674		for (wi = 0; wi < wlist->num_widgets; wi++) {
2675			widget = wlist->widgets[wi];
2676
2677			widget->value = ucontrol->value.enumerated.item[0];
2678
2679			soc_dapm_mux_update_power(widget, kcontrol, widget->value, e);
2680		}
2681	}
 
2682
2683	mutex_unlock(&card->dapm_mutex);
2684	return ret;
2685}
2686EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2687
2688/**
2689 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2690 *					callback
2691 * @kcontrol: mixer control
2692 * @ucontrol: control element information
2693 *
2694 * Callback to get the value of a dapm semi enumerated double mixer control.
2695 *
2696 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2697 * used for handling bitfield coded enumeration for example.
2698 *
2699 * Returns 0 for success.
2700 */
2701int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2702	struct snd_ctl_elem_value *ucontrol)
2703{
2704	struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2705	struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2706	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2707	unsigned int reg_val, val, mux;
2708
2709	reg_val = snd_soc_read(widget->codec, e->reg);
2710	val = (reg_val >> e->shift_l) & e->mask;
2711	for (mux = 0; mux < e->max; mux++) {
2712		if (val == e->values[mux])
2713			break;
2714	}
2715	ucontrol->value.enumerated.item[0] = mux;
2716	if (e->shift_l != e->shift_r) {
2717		val = (reg_val >> e->shift_r) & e->mask;
2718		for (mux = 0; mux < e->max; mux++) {
2719			if (val == e->values[mux])
2720				break;
2721		}
2722		ucontrol->value.enumerated.item[1] = mux;
2723	}
2724
2725	return 0;
2726}
2727EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2728
2729/**
2730 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2731 *					callback
2732 * @kcontrol: mixer control
2733 * @ucontrol: control element information
2734 *
2735 * Callback to set the value of a dapm semi enumerated double mixer control.
2736 *
2737 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2738 * used for handling bitfield coded enumeration for example.
2739 *
2740 * Returns 0 for success.
2741 */
2742int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2743	struct snd_ctl_elem_value *ucontrol)
2744{
2745	struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2746	struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2747	struct snd_soc_codec *codec = widget->codec;
2748	struct snd_soc_card *card = codec->card;
2749	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2750	unsigned int val, mux, change;
 
2751	unsigned int mask;
2752	struct snd_soc_dapm_update update;
2753	int wi;
2754
2755	if (ucontrol->value.enumerated.item[0] > e->max - 1)
2756		return -EINVAL;
2757	mux = ucontrol->value.enumerated.item[0];
2758	val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2759	mask = e->mask << e->shift_l;
2760	if (e->shift_l != e->shift_r) {
2761		if (ucontrol->value.enumerated.item[1] > e->max - 1)
2762			return -EINVAL;
2763		val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2764		mask |= e->mask << e->shift_r;
2765	}
2766
2767	mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2768
2769	change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
2770	if (change) {
2771		for (wi = 0; wi < wlist->num_widgets; wi++) {
2772			widget = wlist->widgets[wi];
2773
2774			widget->value = val;
 
2775
 
 
2776			update.kcontrol = kcontrol;
2777			update.widget = widget;
2778			update.reg = e->reg;
2779			update.mask = mask;
2780			update.val = val;
2781			widget->dapm->update = &update;
 
2782
2783			soc_dapm_mux_update_power(widget, kcontrol, mux, e);
2784
2785			widget->dapm->update = NULL;
2786		}
2787	}
2788
2789	mutex_unlock(&card->dapm_mutex);
 
 
 
 
2790	return change;
2791}
2792EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2793
2794/**
2795 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2796 *
2797 * @kcontrol: mixer control
2798 * @uinfo: control element information
2799 *
2800 * Callback to provide information about a pin switch control.
2801 */
2802int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2803				 struct snd_ctl_elem_info *uinfo)
2804{
2805	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2806	uinfo->count = 1;
2807	uinfo->value.integer.min = 0;
2808	uinfo->value.integer.max = 1;
2809
2810	return 0;
2811}
2812EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2813
2814/**
2815 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2816 *
2817 * @kcontrol: mixer control
2818 * @ucontrol: Value
2819 */
2820int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2821				struct snd_ctl_elem_value *ucontrol)
2822{
2823	struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
2824	const char *pin = (const char *)kcontrol->private_value;
2825
2826	mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2827
2828	ucontrol->value.integer.value[0] =
2829		snd_soc_dapm_get_pin_status(&card->dapm, pin);
2830
2831	mutex_unlock(&card->dapm_mutex);
2832
2833	return 0;
2834}
2835EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2836
2837/**
2838 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2839 *
2840 * @kcontrol: mixer control
2841 * @ucontrol: Value
2842 */
2843int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2844				struct snd_ctl_elem_value *ucontrol)
2845{
2846	struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
2847	const char *pin = (const char *)kcontrol->private_value;
 
2848
2849	mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2850
2851	if (ucontrol->value.integer.value[0])
2852		snd_soc_dapm_enable_pin(&card->dapm, pin);
2853	else
2854		snd_soc_dapm_disable_pin(&card->dapm, pin);
2855
2856	mutex_unlock(&card->dapm_mutex);
2857
2858	snd_soc_dapm_sync(&card->dapm);
2859	return 0;
2860}
2861EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2862
2863static struct snd_soc_dapm_widget *
2864snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
2865			 const struct snd_soc_dapm_widget *widget)
2866{
 
2867	struct snd_soc_dapm_widget *w;
2868	size_t name_len;
2869	int ret;
2870
2871	if ((w = dapm_cnew_widget(widget)) == NULL)
2872		return NULL;
 
2873
2874	switch (w->id) {
2875	case snd_soc_dapm_regulator_supply:
2876		w->regulator = devm_regulator_get(dapm->dev, w->name);
2877		if (IS_ERR(w->regulator)) {
2878			ret = PTR_ERR(w->regulator);
2879			dev_err(dapm->dev, "Failed to request %s: %d\n",
2880				w->name, ret);
2881			return NULL;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2882		}
2883		break;
2884	default:
2885		break;
2886	}
2887
2888	name_len = strlen(widget->name) + 1;
2889	if (dapm->codec && dapm->codec->name_prefix)
2890		name_len += 1 + strlen(dapm->codec->name_prefix);
2891	w->name = kmalloc(name_len, GFP_KERNEL);
2892	if (w->name == NULL) {
2893		kfree(w);
2894		return NULL;
2895	}
2896	if (dapm->codec && dapm->codec->name_prefix)
2897		snprintf((char *)w->name, name_len, "%s %s",
2898			dapm->codec->name_prefix, widget->name);
2899	else
2900		snprintf((char *)w->name, name_len, "%s", widget->name);
2901
2902	switch (w->id) {
2903	case snd_soc_dapm_switch:
2904	case snd_soc_dapm_mixer:
2905	case snd_soc_dapm_mixer_named_ctl:
 
 
 
 
 
 
 
 
 
2906		w->power_check = dapm_generic_check_power;
2907		break;
2908	case snd_soc_dapm_mux:
2909	case snd_soc_dapm_virt_mux:
2910	case snd_soc_dapm_value_mux:
2911		w->power_check = dapm_generic_check_power;
2912		break;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2913	case snd_soc_dapm_adc:
2914	case snd_soc_dapm_aif_out:
2915		w->power_check = dapm_adc_check_power;
2916		break;
2917	case snd_soc_dapm_dac:
2918	case snd_soc_dapm_aif_in:
2919		w->power_check = dapm_dac_check_power;
2920		break;
2921	case snd_soc_dapm_pga:
 
 
 
 
 
 
 
2922	case snd_soc_dapm_out_drv:
2923	case snd_soc_dapm_input:
2924	case snd_soc_dapm_output:
2925	case snd_soc_dapm_micbias:
2926	case snd_soc_dapm_spk:
2927	case snd_soc_dapm_hp:
2928	case snd_soc_dapm_mic:
2929	case snd_soc_dapm_line:
2930	case snd_soc_dapm_dai_link:
 
 
2931		w->power_check = dapm_generic_check_power;
2932		break;
2933	case snd_soc_dapm_supply:
2934	case snd_soc_dapm_regulator_supply:
 
 
 
 
2935		w->power_check = dapm_supply_check_power;
2936		break;
2937	case snd_soc_dapm_dai:
2938		w->power_check = dapm_dai_check_power;
2939		break;
2940	default:
2941		w->power_check = dapm_always_on_check_power;
2942		break;
2943	}
2944
2945	dapm->n_widgets++;
2946	w->dapm = dapm;
2947	w->codec = dapm->codec;
2948	w->platform = dapm->platform;
2949	INIT_LIST_HEAD(&w->sources);
2950	INIT_LIST_HEAD(&w->sinks);
2951	INIT_LIST_HEAD(&w->list);
2952	INIT_LIST_HEAD(&w->dirty);
2953	list_add(&w->list, &dapm->card->widgets);
 
 
 
 
 
 
2954
2955	/* machine layer set ups unconnected pins and insertions */
2956	w->connected = 1;
2957	return w;
 
 
 
 
 
 
 
 
 
2958}
2959
2960/**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2961 * snd_soc_dapm_new_controls - create new dapm controls
2962 * @dapm: DAPM context
2963 * @widget: widget array
2964 * @num: number of widgets
2965 *
2966 * Creates new DAPM controls based upon the templates.
2967 *
2968 * Returns 0 for success else error.
2969 */
2970int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
2971	const struct snd_soc_dapm_widget *widget,
2972	int num)
2973{
2974	struct snd_soc_dapm_widget *w;
2975	int i;
2976	int ret = 0;
2977
2978	mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2979	for (i = 0; i < num; i++) {
2980		w = snd_soc_dapm_new_control(dapm, widget);
2981		if (!w) {
2982			dev_err(dapm->dev,
2983				"ASoC: Failed to create DAPM control %s\n",
2984				widget->name);
2985			ret = -ENOMEM;
2986			break;
2987		}
2988		widget++;
2989	}
2990	mutex_unlock(&dapm->card->dapm_mutex);
2991	return ret;
2992}
2993EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2994
2995static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
2996				  struct snd_kcontrol *kcontrol, int event)
 
2997{
2998	struct snd_soc_dapm_path *source_p, *sink_p;
2999	struct snd_soc_dai *source, *sink;
3000	const struct snd_soc_pcm_stream *config = w->params;
3001	struct snd_pcm_substream substream;
3002	struct snd_pcm_hw_params *params = NULL;
3003	u64 fmt;
3004	int ret;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3005
3006	BUG_ON(!config);
3007	BUG_ON(list_empty(&w->sources) || list_empty(&w->sinks));
 
3008
3009	/* We only support a single source and sink, pick the first */
3010	source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3011				    list_sink);
3012	sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3013				  list_source);
3014
3015	BUG_ON(!source_p || !sink_p);
3016	BUG_ON(!sink_p->source || !source_p->sink);
3017	BUG_ON(!source_p->source || !sink_p->sink);
3018
3019	source = source_p->source->priv;
3020	sink = sink_p->sink->priv;
 
 
 
 
 
 
 
 
 
 
 
3021
3022	/* Be a little careful as we don't want to overflow the mask array */
3023	if (config->formats) {
3024		fmt = ffs(config->formats) - 1;
3025	} else {
3026		dev_warn(w->dapm->dev, "Invalid format %llx specified\n",
3027			 config->formats);
3028		fmt = 0;
3029	}
3030
3031	/* Currently very limited parameter selection */
3032	params = kzalloc(sizeof(*params), GFP_KERNEL);
3033	if (!params) {
3034		ret = -ENOMEM;
3035		goto out;
3036	}
 
 
 
3037	snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
3038
3039	hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
3040		config->rate_min;
3041	hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
3042		config->rate_max;
3043
3044	hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
3045		= config->channels_min;
3046	hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
3047		= config->channels_max;
3048
3049	memset(&substream, 0, sizeof(substream));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3050
3051	switch (event) {
3052	case SND_SOC_DAPM_PRE_PMU:
3053		if (source->driver->ops && source->driver->ops->hw_params) {
3054			substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3055			ret = source->driver->ops->hw_params(&substream,
3056							     params, source);
3057			if (ret != 0) {
3058				dev_err(source->dev,
3059					"hw_params() failed: %d\n", ret);
3060				goto out;
3061			}
3062		}
3063
3064		if (sink->driver->ops && sink->driver->ops->hw_params) {
3065			substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3066			ret = sink->driver->ops->hw_params(&substream, params,
3067							   sink);
3068			if (ret != 0) {
3069				dev_err(sink->dev,
3070					"hw_params() failed: %d\n", ret);
3071				goto out;
3072			}
3073		}
3074		break;
3075
3076	case SND_SOC_DAPM_POST_PMU:
3077		ret = snd_soc_dai_digital_mute(sink, 0);
3078		if (ret != 0 && ret != -ENOTSUPP)
3079			dev_warn(sink->dev, "Failed to unmute: %d\n", ret);
3080		ret = 0;
 
 
3081		break;
3082
3083	case SND_SOC_DAPM_PRE_PMD:
3084		ret = snd_soc_dai_digital_mute(sink, 1);
3085		if (ret != 0 && ret != -ENOTSUPP)
3086			dev_warn(sink->dev, "Failed to mute: %d\n", ret);
3087		ret = 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3088		break;
3089
3090	default:
3091		BUG();
3092		return -EINVAL;
3093	}
3094
3095out:
3096	kfree(params);
 
3097	return ret;
3098}
3099
3100int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3101			 const struct snd_soc_pcm_stream *params,
3102			 struct snd_soc_dapm_widget *source,
3103			 struct snd_soc_dapm_widget *sink)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3104{
3105	struct snd_soc_dapm_route routes[2];
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3106	struct snd_soc_dapm_widget template;
3107	struct snd_soc_dapm_widget *w;
3108	size_t len;
 
 
 
3109	char *link_name;
 
3110
3111	len = strlen(source->name) + strlen(sink->name) + 2;
3112	link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3113	if (!link_name)
3114		return -ENOMEM;
3115	snprintf(link_name, len, "%s-%s", source->name, sink->name);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3116
3117	memset(&template, 0, sizeof(template));
3118	template.reg = SND_SOC_NOPM;
3119	template.id = snd_soc_dapm_dai_link;
3120	template.name = link_name;
3121	template.event = snd_soc_dai_link_event;
3122	template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3123		SND_SOC_DAPM_PRE_PMD;
3124
3125	dev_dbg(card->dev, "adding %s widget\n", link_name);
3126
3127	w = snd_soc_dapm_new_control(&card->dapm, &template);
3128	if (!w) {
3129		dev_err(card->dev, "Failed to create %s widget\n",
3130			link_name);
3131		return -ENOMEM;
 
3132	}
3133
3134	w->params = params;
3135
3136	memset(&routes, 0, sizeof(routes));
3137
3138	routes[0].source = source->name;
3139	routes[0].sink = link_name;
3140	routes[1].source = link_name;
3141	routes[1].sink = sink->name;
3142
3143	return snd_soc_dapm_add_routes(&card->dapm, routes,
3144				       ARRAY_SIZE(routes));
 
 
 
3145}
3146
 
 
 
 
 
 
 
3147int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3148				 struct snd_soc_dai *dai)
3149{
3150	struct snd_soc_dapm_widget template;
3151	struct snd_soc_dapm_widget *w;
3152
3153	WARN_ON(dapm->dev != dai->dev);
3154
3155	memset(&template, 0, sizeof(template));
3156	template.reg = SND_SOC_NOPM;
3157
3158	if (dai->driver->playback.stream_name) {
3159		template.id = snd_soc_dapm_dai;
3160		template.name = dai->driver->playback.stream_name;
3161		template.sname = dai->driver->playback.stream_name;
3162
3163		dev_dbg(dai->dev, "adding %s widget\n",
3164			template.name);
3165
3166		w = snd_soc_dapm_new_control(dapm, &template);
3167		if (!w) {
3168			dev_err(dapm->dev, "Failed to create %s widget\n",
3169				dai->driver->playback.stream_name);
3170		}
3171
3172		w->priv = dai;
3173		dai->playback_widget = w;
3174	}
3175
3176	if (dai->driver->capture.stream_name) {
3177		template.id = snd_soc_dapm_dai;
3178		template.name = dai->driver->capture.stream_name;
3179		template.sname = dai->driver->capture.stream_name;
3180
3181		dev_dbg(dai->dev, "adding %s widget\n",
3182			template.name);
3183
3184		w = snd_soc_dapm_new_control(dapm, &template);
3185		if (!w) {
3186			dev_err(dapm->dev, "Failed to create %s widget\n",
3187				dai->driver->capture.stream_name);
3188		}
3189
3190		w->priv = dai;
3191		dai->capture_widget = w;
3192	}
3193
3194	return 0;
3195}
 
3196
3197int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3198{
3199	struct snd_soc_dapm_widget *dai_w, *w;
 
3200	struct snd_soc_dai *dai;
3201	struct snd_soc_dapm_route r;
3202
3203	memset(&r, 0, sizeof(r));
 
 
 
 
 
 
 
 
3204
3205	/* For each DAI widget... */
3206	list_for_each_entry(dai_w, &card->widgets, list) {
3207		if (dai_w->id != snd_soc_dapm_dai)
 
3208			continue;
 
3209
3210		dai = dai_w->priv;
3211
3212		/* ...find all widgets with the same stream and link them */
3213		list_for_each_entry(w, &card->widgets, list) {
3214			if (w->dapm != dai_w->dapm)
3215				continue;
3216
3217			if (w->id == snd_soc_dapm_dai)
 
 
3218				continue;
 
 
 
3219
3220			if (!w->sname)
3221				continue;
3222
3223			if (dai->driver->playback.stream_name &&
3224			    strstr(w->sname,
3225				   dai->driver->playback.stream_name)) {
3226				r.source = dai->playback_widget->name;
3227				r.sink = w->name;
3228				dev_dbg(dai->dev, "%s -> %s\n",
3229					 r.source, r.sink);
 
 
 
 
 
 
 
3230
3231				snd_soc_dapm_add_route(w->dapm, &r);
3232			}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3233
3234			if (dai->driver->capture.stream_name &&
3235			    strstr(w->sname,
3236				   dai->driver->capture.stream_name)) {
3237				r.source = w->name;
3238				r.sink = dai->capture_widget->name;
3239				dev_dbg(dai->dev, "%s -> %s\n",
3240					r.source, r.sink);
3241
3242				snd_soc_dapm_add_route(w->dapm, &r);
3243			}
3244		}
 
 
 
 
3245	}
3246
3247	return 0;
3248}
3249
3250static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3251	int event)
3252{
 
3253
3254	struct snd_soc_dapm_widget *w_cpu, *w_codec;
3255	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3256	struct snd_soc_dai *codec_dai = rtd->codec_dai;
3257
3258	if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
3259		w_cpu = cpu_dai->playback_widget;
3260		w_codec = codec_dai->playback_widget;
3261	} else {
3262		w_cpu = cpu_dai->capture_widget;
3263		w_codec = codec_dai->capture_widget;
3264	}
3265
3266	if (w_cpu) {
3267
3268		dapm_mark_dirty(w_cpu, "stream event");
 
 
 
 
 
 
3269
3270		switch (event) {
3271		case SND_SOC_DAPM_STREAM_START:
3272			w_cpu->active = 1;
 
3273			break;
3274		case SND_SOC_DAPM_STREAM_STOP:
3275			w_cpu->active = 0;
 
3276			break;
3277		case SND_SOC_DAPM_STREAM_SUSPEND:
3278		case SND_SOC_DAPM_STREAM_RESUME:
3279		case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3280		case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3281			break;
3282		}
3283	}
 
3284
3285	if (w_codec) {
 
 
 
 
 
 
 
 
 
3286
3287		dapm_mark_dirty(w_codec, "stream event");
 
 
 
 
 
 
 
 
 
 
 
 
 
3288
3289		switch (event) {
3290		case SND_SOC_DAPM_STREAM_START:
3291			w_codec->active = 1;
3292			break;
3293		case SND_SOC_DAPM_STREAM_STOP:
3294			w_codec->active = 0;
3295			break;
3296		case SND_SOC_DAPM_STREAM_SUSPEND:
3297		case SND_SOC_DAPM_STREAM_RESUME:
3298		case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3299		case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3300			break;
3301		}
3302	}
 
3303
3304	dapm_power_widgets(&rtd->card->dapm, event);
 
 
 
 
 
 
 
 
 
3305}
3306
3307/**
3308 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3309 * @rtd: PCM runtime data
3310 * @stream: stream name
3311 * @event: stream event
3312 *
3313 * Sends a stream event to the dapm core. The core then makes any
3314 * necessary widget power changes.
3315 *
3316 * Returns 0 for success else error.
3317 */
3318void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3319			      int event)
3320{
3321	struct snd_soc_card *card = rtd->card;
3322
3323	mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3324	soc_dapm_stream_event(rtd, stream, event);
3325	mutex_unlock(&card->dapm_mutex);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3326}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3327
3328/**
3329 * snd_soc_dapm_enable_pin - enable pin.
3330 * @dapm: DAPM context
3331 * @pin: pin name
3332 *
3333 * Enables input/output pin and its parents or children widgets iff there is
3334 * a valid audio route and active audio stream.
 
3335 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3336 * do any widget power switching.
3337 */
3338int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
3339{
3340	return snd_soc_dapm_set_pin(dapm, pin, 1);
 
 
 
 
 
 
 
 
3341}
3342EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
3343
3344/**
3345 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
3346 * @dapm: DAPM context
3347 * @pin: pin name
3348 *
3349 * Enables input/output pin regardless of any other state.  This is
3350 * intended for use with microphone bias supplies used in microphone
3351 * jack detection.
3352 *
 
 
3353 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3354 * do any widget power switching.
3355 */
3356int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3357				  const char *pin)
3358{
3359	struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3360
3361	if (!w) {
3362		dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
3363		return -EINVAL;
3364	}
3365
3366	dev_dbg(w->dapm->dev, "dapm: force enable pin %s\n", pin);
3367	w->connected = 1;
 
 
 
 
 
 
 
 
3368	w->force = 1;
3369	dapm_mark_dirty(w, "force enable");
3370
3371	return 0;
3372}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3373EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3374
3375/**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3376 * snd_soc_dapm_disable_pin - disable pin.
3377 * @dapm: DAPM context
3378 * @pin: pin name
3379 *
3380 * Disables input/output pin and its parents or children widgets.
 
3381 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3382 * do any widget power switching.
3383 */
3384int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3385			     const char *pin)
3386{
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3387	return snd_soc_dapm_set_pin(dapm, pin, 0);
3388}
3389EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3390
3391/**
3392 * snd_soc_dapm_nc_pin - permanently disable pin.
3393 * @dapm: DAPM context
3394 * @pin: pin name
3395 *
3396 * Marks the specified pin as being not connected, disabling it along
3397 * any parent or child widgets.  At present this is identical to
3398 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3399 * additional things such as disabling controls which only affect
3400 * paths through the pin.
3401 *
3402 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3403 * do any widget power switching.
3404 */
3405int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
3406{
3407	return snd_soc_dapm_set_pin(dapm, pin, 0);
 
 
 
 
 
 
 
 
3408}
3409EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3410
3411/**
3412 * snd_soc_dapm_get_pin_status - get audio pin status
3413 * @dapm: DAPM context
3414 * @pin: audio signal pin endpoint (or start point)
3415 *
3416 * Get audio pin status - connected or disconnected.
3417 *
3418 * Returns 1 for connected otherwise 0.
3419 */
3420int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3421				const char *pin)
3422{
3423	struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3424
3425	if (w)
3426		return w->connected;
3427
3428	return 0;
3429}
3430EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
3431
3432/**
3433 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
3434 * @dapm: DAPM context
3435 * @pin: audio signal pin endpoint (or start point)
3436 *
3437 * Mark the given endpoint or pin as ignoring suspend.  When the
3438 * system is disabled a path between two endpoints flagged as ignoring
3439 * suspend will not be disabled.  The path must already be enabled via
3440 * normal means at suspend time, it will not be turned on if it was not
3441 * already enabled.
3442 */
3443int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3444				const char *pin)
3445{
3446	struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
3447
3448	if (!w) {
3449		dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
3450		return -EINVAL;
3451	}
3452
3453	w->ignore_suspend = 1;
3454
3455	return 0;
3456}
3457EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3458
3459static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3460					      struct snd_soc_dapm_widget *w)
3461{
3462	struct snd_soc_dapm_path *p;
3463
3464	list_for_each_entry(p, &card->paths, list) {
3465		if ((p->source == w) || (p->sink == w)) {
3466			dev_dbg(card->dev,
3467			    "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3468			    p->source->name, p->source->id, p->source->dapm,
3469			    p->sink->name, p->sink->id, p->sink->dapm);
3470
3471			/* Connected to something other than the codec */
3472			if (p->source->dapm != p->sink->dapm)
3473				return true;
3474			/*
3475			 * Loopback connection from codec external pin to
3476			 * codec external pin
3477			 */
3478			if (p->sink->id == snd_soc_dapm_input) {
3479				switch (p->source->id) {
3480				case snd_soc_dapm_output:
3481				case snd_soc_dapm_micbias:
3482					return true;
3483				default:
3484					break;
3485				}
3486			}
3487		}
3488	}
3489
3490	return false;
3491}
3492
3493/**
3494 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3495 * @codec: The codec whose pins should be processed
3496 *
3497 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3498 * which are unused. Pins are used if they are connected externally to the
3499 * codec, whether that be to some other device, or a loop-back connection to
3500 * the codec itself.
3501 */
3502void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
3503{
3504	struct snd_soc_card *card = codec->card;
3505	struct snd_soc_dapm_context *dapm = &codec->dapm;
3506	struct snd_soc_dapm_widget *w;
3507
3508	dev_dbg(codec->dev, "Auto NC: DAPMs: card:%p codec:%p\n",
3509		&card->dapm, &codec->dapm);
3510
3511	list_for_each_entry(w, &card->widgets, list) {
3512		if (w->dapm != dapm)
3513			continue;
3514		switch (w->id) {
3515		case snd_soc_dapm_input:
3516		case snd_soc_dapm_output:
3517		case snd_soc_dapm_micbias:
3518			dev_dbg(codec->dev, "Auto NC: Checking widget %s\n",
3519				w->name);
3520			if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
3521				dev_dbg(codec->dev,
3522					"... Not in map; disabling\n");
3523				snd_soc_dapm_nc_pin(dapm, w->name);
3524			}
3525			break;
3526		default:
3527			break;
3528		}
3529	}
3530}
3531
3532/**
3533 * snd_soc_dapm_free - free dapm resources
3534 * @dapm: DAPM context
3535 *
3536 * Free all dapm widgets and resources.
3537 */
3538void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
3539{
3540	snd_soc_dapm_sys_remove(dapm->dev);
3541	dapm_debugfs_cleanup(dapm);
3542	dapm_free_widgets(dapm);
3543	list_del(&dapm->list);
3544}
3545EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3546
3547static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3548{
3549	struct snd_soc_card *card = dapm->card;
3550	struct snd_soc_dapm_widget *w;
3551	LIST_HEAD(down_list);
3552	int powerdown = 0;
3553
3554	mutex_lock(&card->dapm_mutex);
3555
3556	list_for_each_entry(w, &dapm->card->widgets, list) {
3557		if (w->dapm != dapm)
3558			continue;
3559		if (w->power) {
3560			dapm_seq_insert(w, &down_list, false);
3561			w->power = 0;
3562			powerdown = 1;
3563		}
3564	}
3565
3566	/* If there were no widgets to power down we're already in
3567	 * standby.
3568	 */
3569	if (powerdown) {
3570		if (dapm->bias_level == SND_SOC_BIAS_ON)
3571			snd_soc_dapm_set_bias_level(dapm,
3572						    SND_SOC_BIAS_PREPARE);
3573		dapm_seq_run(dapm, &down_list, 0, false);
3574		if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3575			snd_soc_dapm_set_bias_level(dapm,
3576						    SND_SOC_BIAS_STANDBY);
3577	}
3578
3579	mutex_unlock(&card->dapm_mutex);
3580}
3581
3582/*
3583 * snd_soc_dapm_shutdown - callback for system shutdown
3584 */
3585void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3586{
3587	struct snd_soc_codec *codec;
3588
3589	list_for_each_entry(codec, &card->codec_dev_list, list) {
3590		soc_dapm_shutdown_codec(&codec->dapm);
3591		if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3592			snd_soc_dapm_set_bias_level(&codec->dapm,
3593						    SND_SOC_BIAS_OFF);
 
 
3594	}
 
 
 
 
 
3595}
3596
3597/* Module information */
3598MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3599MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3600MODULE_LICENSE("GPL");