Linux Audio

Check our new training course

Embedded Linux training

Mar 10-20, 2025, special US time zones
Register
Loading...
v3.1
   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/meadphone 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 powerdown of audio susbsystem to reduce pops between a quick
  22 *      device reopen.
  23 *
  24 *  Todo:
  25 *    o DAPM power change sequencing - allow for configurable per
  26 *      codec sequences.
  27 *    o Support for analogue bias optimisation.
  28 *    o Support for reduced codec oversampling rates.
  29 *    o Support for reduced codec bias currents.
  30 */
  31
  32#include <linux/module.h>
  33#include <linux/moduleparam.h>
  34#include <linux/init.h>
  35#include <linux/async.h>
  36#include <linux/delay.h>
  37#include <linux/pm.h>
  38#include <linux/bitops.h>
  39#include <linux/platform_device.h>
  40#include <linux/jiffies.h>
  41#include <linux/debugfs.h>
 
 
 
 
  42#include <linux/slab.h>
  43#include <sound/core.h>
  44#include <sound/pcm.h>
  45#include <sound/pcm_params.h>
  46#include <sound/soc.h>
  47#include <sound/initval.h>
  48
  49#include <trace/events/asoc.h>
  50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  51/* dapm power sequences - make this per codec in the future */
  52static int dapm_up_seq[] = {
  53	[snd_soc_dapm_pre] = 0,
  54	[snd_soc_dapm_supply] = 1,
  55	[snd_soc_dapm_micbias] = 2,
  56	[snd_soc_dapm_aif_in] = 3,
  57	[snd_soc_dapm_aif_out] = 3,
  58	[snd_soc_dapm_mic] = 4,
  59	[snd_soc_dapm_mux] = 5,
  60	[snd_soc_dapm_virt_mux] = 5,
  61	[snd_soc_dapm_value_mux] = 5,
  62	[snd_soc_dapm_dac] = 6,
  63	[snd_soc_dapm_mixer] = 7,
  64	[snd_soc_dapm_mixer_named_ctl] = 7,
  65	[snd_soc_dapm_pga] = 8,
  66	[snd_soc_dapm_adc] = 9,
  67	[snd_soc_dapm_out_drv] = 10,
  68	[snd_soc_dapm_hp] = 10,
  69	[snd_soc_dapm_spk] = 10,
  70	[snd_soc_dapm_post] = 11,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  71};
  72
  73static int dapm_down_seq[] = {
  74	[snd_soc_dapm_pre] = 0,
  75	[snd_soc_dapm_adc] = 1,
  76	[snd_soc_dapm_hp] = 2,
  77	[snd_soc_dapm_spk] = 2,
  78	[snd_soc_dapm_out_drv] = 2,
  79	[snd_soc_dapm_pga] = 4,
  80	[snd_soc_dapm_mixer_named_ctl] = 5,
  81	[snd_soc_dapm_mixer] = 5,
  82	[snd_soc_dapm_dac] = 6,
  83	[snd_soc_dapm_mic] = 7,
  84	[snd_soc_dapm_micbias] = 8,
  85	[snd_soc_dapm_mux] = 9,
  86	[snd_soc_dapm_virt_mux] = 9,
  87	[snd_soc_dapm_value_mux] = 9,
  88	[snd_soc_dapm_aif_in] = 10,
  89	[snd_soc_dapm_aif_out] = 10,
  90	[snd_soc_dapm_supply] = 11,
  91	[snd_soc_dapm_post] = 12,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  92};
  93
 
 
 
 
 
 
  94static void pop_wait(u32 pop_time)
  95{
  96	if (pop_time)
  97		schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
  98}
  99
 
 100static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
 101{
 102	va_list args;
 103	char *buf;
 104
 105	if (!pop_time)
 106		return;
 107
 108	buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
 109	if (buf == NULL)
 110		return;
 111
 112	va_start(args, fmt);
 113	vsnprintf(buf, PAGE_SIZE, fmt, args);
 114	dev_info(dev, "%s", buf);
 115	va_end(args);
 116
 117	kfree(buf);
 118}
 119
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 120/* create a new dapm widget */
 121static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
 122	const struct snd_soc_dapm_widget *_widget)
 
 123{
 124	return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 125}
 126
 127/* get snd_card from DAPM context */
 128static inline struct snd_card *dapm_get_snd_card(
 129	struct snd_soc_dapm_context *dapm)
 
 
 
 
 
 
 130{
 131	if (dapm->codec)
 132		return dapm->codec->card->snd_card;
 133	else if (dapm->platform)
 134		return dapm->platform->card->snd_card;
 135	else
 136		BUG();
 137
 138	/* unreachable */
 139	return NULL;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 140}
 141
 142/* get soc_card from DAPM context */
 143static inline struct snd_soc_card *dapm_get_soc_card(
 144		struct snd_soc_dapm_context *dapm)
 145{
 146	if (dapm->codec)
 147		return dapm->codec->card;
 148	else if (dapm->platform)
 149		return dapm->platform->card;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 150	else
 151		BUG();
 152
 153	/* unreachable */
 154	return NULL;
 
 
 
 
 
 
 
 
 
 
 155}
 156
 157static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg)
 
 158{
 159	if (w->codec)
 160		return snd_soc_read(w->codec, reg);
 161	else if (w->platform)
 162		return snd_soc_platform_read(w->platform, reg);
 163
 164	dev_err(w->dapm->dev, "no valid widget read method\n");
 165	return -1;
 166}
 167
 168static int soc_widget_write(struct snd_soc_dapm_widget *w, int reg, int val)
 169{
 170	if (w->codec)
 171		return snd_soc_write(w->codec, reg, val);
 172	else if (w->platform)
 173		return snd_soc_platform_write(w->platform, reg, val);
 174
 175	dev_err(w->dapm->dev, "no valid widget write method\n");
 176	return -1;
 
 
 177}
 178
 179static int soc_widget_update_bits(struct snd_soc_dapm_widget *w,
 180	unsigned short reg, unsigned int mask, unsigned int value)
 181{
 182	int change;
 183	unsigned int old, new;
 184	int ret;
 185
 186	ret = soc_widget_read(w, reg);
 187	if (ret < 0)
 188		return ret;
 189
 190	old = ret;
 191	new = (old & ~mask) | (value & mask);
 192	change = old != new;
 193	if (change) {
 194		ret = soc_widget_write(w, reg, new);
 195		if (ret < 0)
 196			return ret;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 197	}
 198
 199	return change;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 200}
 
 201
 202/**
 203 * snd_soc_dapm_set_bias_level - set the bias level for the system
 204 * @dapm: DAPM context
 205 * @level: level to configure
 206 *
 207 * Configure the bias (power) levels for the SoC audio device.
 208 *
 209 * Returns 0 for success else error.
 210 */
 211static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
 212				       enum snd_soc_bias_level level)
 213{
 214	struct snd_soc_card *card = dapm->card;
 215	int ret = 0;
 216
 217	trace_snd_soc_bias_level_start(card, level);
 218
 219	if (card && card->set_bias_level)
 220		ret = card->set_bias_level(card, dapm, level);
 221	if (ret != 0)
 222		goto out;
 223
 224	if (dapm->codec) {
 225		if (dapm->codec->driver->set_bias_level)
 226			ret = dapm->codec->driver->set_bias_level(dapm->codec,
 227								  level);
 228		else
 229			dapm->bias_level = level;
 230	}
 231	if (ret != 0)
 232		goto out;
 233
 234	if (card && card->set_bias_level_post)
 235		ret = card->set_bias_level_post(card, dapm, level);
 236out:
 237	trace_snd_soc_bias_level_done(card, level);
 238
 239	return ret;
 240}
 241
 242/* set up initial codec paths */
 243static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
 244	struct snd_soc_dapm_path *p, int i)
 
 245{
 246	switch (w->id) {
 247	case snd_soc_dapm_switch:
 248	case snd_soc_dapm_mixer:
 249	case snd_soc_dapm_mixer_named_ctl: {
 250		int val;
 251		struct soc_mixer_control *mc = (struct soc_mixer_control *)
 252			w->kcontrol_news[i].private_value;
 253		unsigned int reg = mc->reg;
 254		unsigned int shift = mc->shift;
 255		int max = mc->max;
 256		unsigned int mask = (1 << fls(max)) - 1;
 257		unsigned int invert = mc->invert;
 258
 259		val = soc_widget_read(w, reg);
 260		val = (val >> shift) & mask;
 261
 262		if ((invert && !val) || (!invert && val))
 263			p->connect = 1;
 264		else
 265			p->connect = 0;
 266	}
 267	break;
 268	case snd_soc_dapm_mux: {
 269		struct soc_enum *e = (struct soc_enum *)
 270			w->kcontrol_news[i].private_value;
 271		int val, item, bitmask;
 272
 273		for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
 274			;
 275		val = soc_widget_read(w, e->reg);
 276		item = (val >> e->shift_l) & (bitmask - 1);
 277
 278		p->connect = 0;
 279		for (i = 0; i < e->max; i++) {
 280			if (!(strcmp(p->name, e->texts[i])) && item == i)
 281				p->connect = 1;
 282		}
 283	}
 284	break;
 285	case snd_soc_dapm_virt_mux: {
 286		struct soc_enum *e = (struct soc_enum *)
 287			w->kcontrol_news[i].private_value;
 288
 289		p->connect = 0;
 
 
 
 
 
 290		/* since a virtual mux has no backing registers to
 291		 * decide which path to connect, it will try to match
 292		 * with the first enumeration.  This is to ensure
 293		 * that the default mux choice (the first) will be
 294		 * correctly powered up during initialization.
 295		 */
 296		if (!strcmp(p->name, e->texts[0]))
 297			p->connect = 1;
 298	}
 299	break;
 300	case snd_soc_dapm_value_mux: {
 301		struct soc_enum *e = (struct soc_enum *)
 302			w->kcontrol_news[i].private_value;
 303		int val, item;
 304
 305		val = soc_widget_read(w, e->reg);
 306		val = (val >> e->shift_l) & e->mask;
 307		for (item = 0; item < e->max; item++) {
 308			if (val == e->values[item])
 309				break;
 310		}
 
 311
 312		p->connect = 0;
 313		for (i = 0; i < e->max; i++) {
 314			if (!(strcmp(p->name, e->texts[i])) && item == i)
 315				p->connect = 1;
 316		}
 317	}
 318	break;
 319	/* does not effect routing - always connected */
 320	case snd_soc_dapm_pga:
 321	case snd_soc_dapm_out_drv:
 322	case snd_soc_dapm_output:
 323	case snd_soc_dapm_adc:
 324	case snd_soc_dapm_input:
 325	case snd_soc_dapm_dac:
 326	case snd_soc_dapm_micbias:
 327	case snd_soc_dapm_vmid:
 328	case snd_soc_dapm_supply:
 329	case snd_soc_dapm_aif_in:
 330	case snd_soc_dapm_aif_out:
 331		p->connect = 1;
 332	break;
 333	/* does effect routing - dynamically connected */
 334	case snd_soc_dapm_hp:
 335	case snd_soc_dapm_mic:
 336	case snd_soc_dapm_spk:
 337	case snd_soc_dapm_line:
 338	case snd_soc_dapm_pre:
 339	case snd_soc_dapm_post:
 340		p->connect = 0;
 341	break;
 342	}
 343}
 344
 345/* connect mux widget to its interconnecting audio paths */
 346static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
 347	struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
 348	struct snd_soc_dapm_path *path, const char *control_name,
 349	const struct snd_kcontrol_new *kcontrol)
 350{
 351	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
 352	int i;
 
 
 353
 354	for (i = 0; i < e->max; i++) {
 355		if (!(strcmp(control_name, e->texts[i]))) {
 356			list_add(&path->list, &dapm->card->paths);
 357			list_add(&path->list_sink, &dest->sources);
 358			list_add(&path->list_source, &src->sinks);
 359			path->name = (char*)e->texts[i];
 360			dapm_set_path_status(dest, path, 0);
 361			return 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 362		}
 
 
 
 
 
 
 
 
 
 
 
 363	}
 364
 365	return -ENODEV;
 366}
 367
 368/* connect mixer widget to its interconnecting audio paths */
 369static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
 370	struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
 371	struct snd_soc_dapm_path *path, const char *control_name)
 372{
 373	int i;
 374
 375	/* search for mixer kcontrol */
 376	for (i = 0; i < dest->num_kcontrols; i++) {
 377		if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
 378			list_add(&path->list, &dapm->card->paths);
 379			list_add(&path->list_sink, &dest->sources);
 380			list_add(&path->list_source, &src->sinks);
 381			path->name = dest->kcontrol_news[i].name;
 382			dapm_set_path_status(dest, path, i);
 383			return 0;
 384		}
 385	}
 386	return -ENODEV;
 387}
 388
 389static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
 390	struct snd_soc_dapm_widget *kcontrolw,
 391	const struct snd_kcontrol_new *kcontrol_new,
 392	struct snd_kcontrol **kcontrol)
 393{
 394	struct snd_soc_dapm_widget *w;
 395	int i;
 396
 397	*kcontrol = NULL;
 398
 399	list_for_each_entry(w, &dapm->card->widgets, list) {
 400		if (w == kcontrolw || w->dapm != kcontrolw->dapm)
 401			continue;
 402		for (i = 0; i < w->num_kcontrols; i++) {
 403			if (&w->kcontrol_news[i] == kcontrol_new) {
 404				if (w->kcontrols)
 405					*kcontrol = w->kcontrols[i];
 406				return 1;
 407			}
 408		}
 409	}
 410
 411	return 0;
 412}
 413
 414/* create new dapm mixer control */
 415static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
 
 
 
 
 416{
 417	struct snd_soc_dapm_context *dapm = w->dapm;
 418	int i, ret = 0;
 419	size_t name_len, prefix_len;
 420	struct snd_soc_dapm_path *path;
 421	struct snd_card *card = dapm->card->snd_card;
 422	const char *prefix;
 423	struct snd_soc_dapm_widget_list *wlist;
 424	size_t wlistsize;
 425
 426	if (dapm->codec)
 427		prefix = dapm->codec->name_prefix;
 428	else
 429		prefix = NULL;
 430
 
 431	if (prefix)
 432		prefix_len = strlen(prefix) + 1;
 433	else
 434		prefix_len = 0;
 435
 436	/* add kcontrol */
 437	for (i = 0; i < w->num_kcontrols; i++) {
 438
 439		/* match name */
 440		list_for_each_entry(path, &w->sources, list_sink) {
 441
 442			/* mixer/mux paths name must match control name */
 443			if (path->name != (char *)w->kcontrol_news[i].name)
 444				continue;
 445
 446			wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
 447				    sizeof(struct snd_soc_dapm_widget *),
 448			wlist = kzalloc(wlistsize, GFP_KERNEL);
 449			if (wlist == NULL) {
 450				dev_err(dapm->dev,
 451					"asoc: can't allocate widget list for %s\n",
 452					w->name);
 453				return -ENOMEM;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 454			}
 455			wlist->num_widgets = 1;
 456			wlist->widgets[0] = w;
 
 457
 458			/* add dapm control with long name.
 459			 * for dapm_mixer this is the concatenation of the
 460			 * mixer and kcontrol name.
 461			 * for dapm_mixer_named_ctl this is simply the
 462			 * kcontrol name.
 
 463			 */
 464			name_len = strlen(w->kcontrol_news[i].name) + 1;
 465			if (w->id != snd_soc_dapm_mixer_named_ctl)
 466				name_len += 1 + strlen(w->name);
 
 
 467
 468			path->long_name = kmalloc(name_len, GFP_KERNEL);
 
 
 
 
 
 
 
 469
 470			if (path->long_name == NULL) {
 471				kfree(wlist);
 472				return -ENOMEM;
 473			}
 
 
 474
 475			switch (w->id) {
 476			default:
 477				/* The control will get a prefix from
 478				 * the control creation process but
 479				 * we're also using the same prefix
 480				 * for widgets so cut the prefix off
 481				 * the front of the widget name.
 482				 */
 483				snprintf(path->long_name, name_len, "%s %s",
 484					 w->name + prefix_len,
 485					 w->kcontrol_news[i].name);
 486				break;
 487			case snd_soc_dapm_mixer_named_ctl:
 488				snprintf(path->long_name, name_len, "%s",
 489					 w->kcontrol_news[i].name);
 490				break;
 491			}
 492
 493			path->long_name[name_len - 1] = '\0';
 
 
 
 
 494
 495			path->kcontrol = snd_soc_cnew(&w->kcontrol_news[i],
 496						      wlist, path->long_name,
 497						      prefix);
 498			ret = snd_ctl_add(card, path->kcontrol);
 499			if (ret < 0) {
 500				dev_err(dapm->dev,
 501					"asoc: failed to add dapm kcontrol %s: %d\n",
 502					path->long_name, ret);
 503				kfree(wlist);
 504				kfree(path->long_name);
 505				path->long_name = NULL;
 506				return ret;
 507			}
 508			w->kcontrols[i] = path->kcontrol;
 509		}
 510	}
 
 
 
 
 
 
 
 
 511	return ret;
 512}
 513
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 514/* create new dapm mux control */
 515static int dapm_new_mux(struct snd_soc_dapm_widget *w)
 516{
 517	struct snd_soc_dapm_context *dapm = w->dapm;
 518	struct snd_soc_dapm_path *path = NULL;
 519	struct snd_kcontrol *kcontrol;
 520	struct snd_card *card = dapm->card->snd_card;
 521	const char *prefix;
 522	size_t prefix_len;
 523	int ret;
 524	struct snd_soc_dapm_widget_list *wlist;
 525	int shared, wlistentries;
 526	size_t wlistsize;
 527	char *name;
 
 
 
 
 
 
 
 
 
 528
 529	if (w->num_kcontrols != 1) {
 530		dev_err(dapm->dev,
 531			"asoc: mux %s has incorrect number of controls\n",
 532			w->name);
 533		return -EINVAL;
 534	}
 535
 536	shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[0],
 537					 &kcontrol);
 538	if (kcontrol) {
 539		wlist = kcontrol->private_data;
 540		wlistentries = wlist->num_widgets + 1;
 541	} else {
 542		wlist = NULL;
 543		wlistentries = 1;
 544	}
 545	wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
 546		wlistentries * sizeof(struct snd_soc_dapm_widget *),
 547	wlist = krealloc(wlist, wlistsize, GFP_KERNEL);
 548	if (wlist == NULL) {
 549		dev_err(dapm->dev,
 550			"asoc: can't allocate widget list for %s\n", w->name);
 551		return -ENOMEM;
 552	}
 553	wlist->num_widgets = wlistentries;
 554	wlist->widgets[wlistentries - 1] = w;
 555
 556	if (!kcontrol) {
 557		if (dapm->codec)
 558			prefix = dapm->codec->name_prefix;
 559		else
 560			prefix = NULL;
 561
 562		if (shared) {
 563			name = w->kcontrol_news[0].name;
 564			prefix_len = 0;
 565		} else {
 566			name = w->name;
 567			if (prefix)
 568				prefix_len = strlen(prefix) + 1;
 569			else
 570				prefix_len = 0;
 571		}
 572
 573		/*
 574		 * The control will get a prefix from the control creation
 575		 * process but we're also using the same prefix for widgets so
 576		 * cut the prefix off the front of the widget name.
 577		 */
 578		kcontrol = snd_soc_cnew(&w->kcontrol_news[0], wlist,
 579					name + prefix_len, prefix);
 580		ret = snd_ctl_add(card, kcontrol);
 581		if (ret < 0) {
 582			dev_err(dapm->dev,
 583				"asoc: failed to add kcontrol %s\n", w->name);
 584			kfree(wlist);
 585			return ret;
 586		}
 587	}
 588
 589	kcontrol->private_data = wlist;
 590
 591	w->kcontrols[0] = kcontrol;
 592
 593	list_for_each_entry(path, &w->sources, list_sink)
 594		path->kcontrol = kcontrol;
 595
 596	return 0;
 597}
 598
 599/* create new dapm volume control */
 600static int dapm_new_pga(struct snd_soc_dapm_widget *w)
 601{
 602	if (w->num_kcontrols)
 603		dev_err(w->dapm->dev,
 604			"asoc: PGA controls not supported: '%s'\n", w->name);
 
 
 
 
 605
 606	return 0;
 607}
 608
 609/* reset 'walked' bit for each dapm path */
 610static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
 611{
 612	struct snd_soc_dapm_path *p;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 613
 614	list_for_each_entry(p, &dapm->card->paths, list)
 615		p->walked = 0;
 616}
 617
 618/* We implement power down on suspend by checking the power state of
 619 * the ALSA card - when we are suspending the ALSA state for the card
 620 * is set to D3.
 621 */
 622static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
 623{
 624	int level = snd_power_get_state(widget->dapm->card->snd_card);
 625
 626	switch (level) {
 627	case SNDRV_CTL_POWER_D3hot:
 628	case SNDRV_CTL_POWER_D3cold:
 629		if (widget->ignore_suspend)
 630			dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
 631				widget->name);
 632		return widget->ignore_suspend;
 633	default:
 634		return 1;
 635	}
 636}
 637
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 638/*
 639 * Recursively check for a completed path to an active or physically connected
 640 * output widget. Returns number of complete paths.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 641 */
 642static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
 
 
 
 
 
 
 643{
 
 644	struct snd_soc_dapm_path *path;
 645	int con = 0;
 646
 647	if (widget->id == snd_soc_dapm_supply)
 648		return 0;
 649
 650	switch (widget->id) {
 651	case snd_soc_dapm_adc:
 652	case snd_soc_dapm_aif_out:
 653		if (widget->active)
 654			return snd_soc_dapm_suspend_check(widget);
 655	default:
 656		break;
 
 
 657	}
 658
 659	if (widget->connected) {
 660		/* connected pin ? */
 661		if (widget->id == snd_soc_dapm_output && !widget->ext)
 662			return snd_soc_dapm_suspend_check(widget);
 663
 664		/* connected jack or spk ? */
 665		if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
 666		    (widget->id == snd_soc_dapm_line && !list_empty(&widget->sources)))
 667			return snd_soc_dapm_suspend_check(widget);
 668	}
 669
 670	list_for_each_entry(path, &widget->sinks, list_source) {
 671		if (path->weak)
 672			continue;
 673
 674		if (path->walked)
 675			continue;
 676
 677		if (path->sink && path->connect) {
 678			path->walked = 1;
 679			con += is_connected_output_ep(path->sink);
 
 
 
 
 
 
 680		}
 681	}
 682
 
 
 683	return con;
 684}
 685
 686/*
 687 * Recursively check for a completed path to an active or physically connected
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 688 * input widget. Returns number of complete paths.
 
 
 
 
 
 689 */
 690static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
 
 
 
 691{
 692	struct snd_soc_dapm_path *path;
 693	int con = 0;
 
 694
 695	if (widget->id == snd_soc_dapm_supply)
 696		return 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 697
 698	/* active stream ? */
 699	switch (widget->id) {
 700	case snd_soc_dapm_dac:
 701	case snd_soc_dapm_aif_in:
 702		if (widget->active)
 703			return snd_soc_dapm_suspend_check(widget);
 704	default:
 705		break;
 706	}
 707
 708	if (widget->connected) {
 709		/* connected pin ? */
 710		if (widget->id == snd_soc_dapm_input && !widget->ext)
 711			return snd_soc_dapm_suspend_check(widget);
 712
 713		/* connected VMID/Bias for lower pops */
 714		if (widget->id == snd_soc_dapm_vmid)
 715			return snd_soc_dapm_suspend_check(widget);
 716
 717		/* connected jack ? */
 718		if (widget->id == snd_soc_dapm_mic ||
 719		    (widget->id == snd_soc_dapm_line && !list_empty(&widget->sinks)))
 720			return snd_soc_dapm_suspend_check(widget);
 721	}
 722
 723	list_for_each_entry(path, &widget->sources, list_sink) {
 724		if (path->weak)
 725			continue;
 726
 727		if (path->walked)
 728			continue;
 
 729
 730		if (path->source && path->connect) {
 731			path->walked = 1;
 732			con += is_connected_input_ep(path->source);
 733		}
 734	}
 735
 736	return con;
 
 
 
 
 
 
 737}
 
 738
 739/*
 740 * Handler for generic register modifier widget.
 741 */
 742int dapm_reg_event(struct snd_soc_dapm_widget *w,
 743		   struct snd_kcontrol *kcontrol, int event)
 744{
 745	unsigned int val;
 746
 747	if (SND_SOC_DAPM_EVENT_ON(event))
 748		val = w->on_val;
 749	else
 750		val = w->off_val;
 751
 752	soc_widget_update_bits(w, -(w->reg + 1),
 753			    w->mask << w->shift, val << w->shift);
 
 
 
 
 
 
 754
 755	return 0;
 
 
 
 
 
 
 
 
 
 
 
 756}
 757EXPORT_SYMBOL_GPL(dapm_reg_event);
 758
 759/* Generic check to see if a widget should be powered.
 
 760 */
 761static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
 
 762{
 763	int in, out;
 
 
 764
 765	in = is_connected_input_ep(w);
 766	dapm_clear_walk(w->dapm);
 767	out = is_connected_output_ep(w);
 768	dapm_clear_walk(w->dapm);
 769	return out != 0 && in != 0;
 
 
 
 
 
 
 
 770}
 
 771
 772/* Check to see if an ADC has power */
 773static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
 
 
 
 774{
 775	int in;
 
 
 
 776
 777	if (w->active) {
 778		in = is_connected_input_ep(w);
 779		dapm_clear_walk(w->dapm);
 780		return in != 0;
 781	} else {
 782		return dapm_generic_check_power(w);
 
 783	}
 
 
 784}
 
 785
 786/* Check to see if a DAC has power */
 787static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
 788{
 789	int out;
 
 790
 791	if (w->active) {
 792		out = is_connected_output_ep(w);
 793		dapm_clear_walk(w->dapm);
 794		return out != 0;
 795	} else {
 796		return dapm_generic_check_power(w);
 797	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 798}
 799
 800/* Check to see if a power supply is needed */
 801static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
 802{
 803	struct snd_soc_dapm_path *path;
 804	int power = 0;
 
 805
 806	/* Check if one of our outputs is connected */
 807	list_for_each_entry(path, &w->sinks, list_source) {
 
 
 808		if (path->weak)
 809			continue;
 810
 811		if (path->connected &&
 812		    !path->connected(path->source, path->sink))
 813			continue;
 814
 815		if (!path->sink)
 816			continue;
 817
 818		if (path->sink->force) {
 819			power = 1;
 820			break;
 821		}
 822
 823		if (path->sink->power_check &&
 824		    path->sink->power_check(path->sink)) {
 825			power = 1;
 826			break;
 827		}
 828	}
 829
 830	dapm_clear_walk(w->dapm);
 
 831
 832	return power;
 
 
 833}
 834
 835static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
 836			    struct snd_soc_dapm_widget *b,
 837			    bool power_up)
 838{
 839	int *sort;
 840
 
 
 
 841	if (power_up)
 842		sort = dapm_up_seq;
 843	else
 844		sort = dapm_down_seq;
 845
 
 
 
 846	if (sort[a->id] != sort[b->id])
 847		return sort[a->id] - sort[b->id];
 848	if (a->subseq != b->subseq) {
 849		if (power_up)
 850			return a->subseq - b->subseq;
 851		else
 852			return b->subseq - a->subseq;
 853	}
 854	if (a->reg != b->reg)
 855		return a->reg - b->reg;
 856	if (a->dapm != b->dapm)
 857		return (unsigned long)a->dapm - (unsigned long)b->dapm;
 858
 859	return 0;
 860}
 861
 862/* Insert a widget in order into a DAPM power sequence. */
 863static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
 864			    struct list_head *list,
 865			    bool power_up)
 866{
 867	struct snd_soc_dapm_widget *w;
 868
 869	list_for_each_entry(w, list, power_list)
 870		if (dapm_seq_compare(new_widget, w, power_up) < 0) {
 871			list_add_tail(&new_widget->power_list, &w->power_list);
 872			return;
 873		}
 874
 875	list_add_tail(&new_widget->power_list, list);
 876}
 877
 878static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
 879				 struct snd_soc_dapm_widget *w, int event)
 880{
 881	struct snd_soc_card *card = dapm->card;
 882	const char *ev_name;
 883	int power, ret;
 884
 885	switch (event) {
 886	case SND_SOC_DAPM_PRE_PMU:
 887		ev_name = "PRE_PMU";
 888		power = 1;
 889		break;
 890	case SND_SOC_DAPM_POST_PMU:
 891		ev_name = "POST_PMU";
 892		power = 1;
 893		break;
 894	case SND_SOC_DAPM_PRE_PMD:
 895		ev_name = "PRE_PMD";
 896		power = 0;
 897		break;
 898	case SND_SOC_DAPM_POST_PMD:
 899		ev_name = "POST_PMD";
 900		power = 0;
 901		break;
 
 
 
 
 
 
 
 
 902	default:
 903		BUG();
 904		return;
 905	}
 906
 907	if (w->power != power)
 908		return;
 909
 910	if (w->event && (w->event_flags & event)) {
 911		pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
 
 
 912			w->name, ev_name);
 
 913		trace_snd_soc_dapm_widget_event_start(w, event);
 914		ret = w->event(w, NULL, event);
 915		trace_snd_soc_dapm_widget_event_done(w, event);
 916		if (ret < 0)
 917			pr_err("%s: %s event failed: %d\n",
 918			       ev_name, w->name, ret);
 919	}
 920}
 921
 922/* Apply the coalesced changes from a DAPM sequence */
 923static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
 924				   struct list_head *pending)
 925{
 926	struct snd_soc_card *card = dapm->card;
 927	struct snd_soc_dapm_widget *w;
 928	int reg, power;
 929	unsigned int value = 0;
 930	unsigned int mask = 0;
 931	unsigned int cur_mask;
 932
 933	reg = list_first_entry(pending, struct snd_soc_dapm_widget,
 934			       power_list)->reg;
 
 935
 936	list_for_each_entry(w, pending, power_list) {
 937		cur_mask = 1 << w->shift;
 938		BUG_ON(reg != w->reg);
 939
 940		if (w->invert)
 941			power = !w->power;
 
 942		else
 943			power = w->power;
 944
 945		mask |= cur_mask;
 946		if (power)
 947			value |= cur_mask;
 948
 949		pop_dbg(dapm->dev, card->pop_time,
 950			"pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
 951			w->name, reg, value, mask);
 952
 953		/* Check for events */
 954		dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
 955		dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
 956	}
 957
 958	if (reg >= 0) {
 959		/* Any widget will do, they should all be updating the
 960		 * same register.
 961		 */
 962		w = list_first_entry(pending, struct snd_soc_dapm_widget,
 963				     power_list);
 964
 965		pop_dbg(dapm->dev, card->pop_time,
 966			"pop test : Applying 0x%x/0x%x to %x in %dms\n",
 967			value, mask, reg, card->pop_time);
 968		pop_wait(card->pop_time);
 969		soc_widget_update_bits(w, reg, mask, value);
 970	}
 971
 972	list_for_each_entry(w, pending, power_list) {
 973		dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
 974		dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
 975	}
 976}
 977
 978/* Apply a DAPM power sequence.
 979 *
 980 * We walk over a pre-sorted list of widgets to apply power to.  In
 981 * order to minimise the number of writes to the device required
 982 * multiple widgets will be updated in a single write where possible.
 983 * Currently anything that requires more than a single write is not
 984 * handled.
 985 */
 986static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
 987			 struct list_head *list, int event, bool power_up)
 988{
 989	struct snd_soc_dapm_widget *w, *n;
 
 990	LIST_HEAD(pending);
 991	int cur_sort = -1;
 992	int cur_subseq = -1;
 993	int cur_reg = SND_SOC_NOPM;
 994	struct snd_soc_dapm_context *cur_dapm = NULL;
 995	int ret, i;
 996	int *sort;
 997
 998	if (power_up)
 999		sort = dapm_up_seq;
1000	else
1001		sort = dapm_down_seq;
1002
1003	list_for_each_entry_safe(w, n, list, power_list) {
1004		ret = 0;
1005
1006		/* Do we need to apply any queued changes? */
1007		if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1008		    w->dapm != cur_dapm || w->subseq != cur_subseq) {
1009			if (!list_empty(&pending))
1010				dapm_seq_run_coalesced(cur_dapm, &pending);
1011
1012			if (cur_dapm && cur_dapm->seq_notifier) {
1013				for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1014					if (sort[i] == cur_sort)
1015						cur_dapm->seq_notifier(cur_dapm,
1016								       i,
1017								       cur_subseq);
1018			}
1019
 
 
 
1020			INIT_LIST_HEAD(&pending);
1021			cur_sort = -1;
1022			cur_subseq = INT_MIN;
1023			cur_reg = SND_SOC_NOPM;
1024			cur_dapm = NULL;
1025		}
1026
1027		switch (w->id) {
1028		case snd_soc_dapm_pre:
1029			if (!w->event)
1030				list_for_each_entry_safe_continue(w, n, list,
1031								  power_list);
1032
1033			if (event == SND_SOC_DAPM_STREAM_START)
1034				ret = w->event(w,
1035					       NULL, SND_SOC_DAPM_PRE_PMU);
1036			else if (event == SND_SOC_DAPM_STREAM_STOP)
1037				ret = w->event(w,
1038					       NULL, SND_SOC_DAPM_PRE_PMD);
1039			break;
1040
1041		case snd_soc_dapm_post:
1042			if (!w->event)
1043				list_for_each_entry_safe_continue(w, n, list,
1044								  power_list);
1045
1046			if (event == SND_SOC_DAPM_STREAM_START)
1047				ret = w->event(w,
1048					       NULL, SND_SOC_DAPM_POST_PMU);
1049			else if (event == SND_SOC_DAPM_STREAM_STOP)
1050				ret = w->event(w,
1051					       NULL, SND_SOC_DAPM_POST_PMD);
1052			break;
1053
1054		default:
1055			/* Queue it up for application */
1056			cur_sort = sort[w->id];
1057			cur_subseq = w->subseq;
1058			cur_reg = w->reg;
1059			cur_dapm = w->dapm;
1060			list_move(&w->power_list, &pending);
1061			break;
1062		}
1063
1064		if (ret < 0)
1065			dev_err(w->dapm->dev,
1066				"Failed to apply widget power: %d\n", ret);
1067	}
1068
1069	if (!list_empty(&pending))
1070		dapm_seq_run_coalesced(cur_dapm, &pending);
1071
1072	if (cur_dapm && cur_dapm->seq_notifier) {
1073		for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1074			if (sort[i] == cur_sort)
1075				cur_dapm->seq_notifier(cur_dapm,
1076						       i, cur_subseq);
 
1077	}
 
 
 
1078}
1079
1080static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
1081{
1082	struct snd_soc_dapm_update *update = dapm->update;
1083	struct snd_soc_dapm_widget *w;
 
 
1084	int ret;
1085
1086	if (!update)
1087		return;
1088
1089	w = update->widget;
1090
1091	if (w->event &&
1092	    (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1093		ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1094		if (ret != 0)
1095			pr_err("%s DAPM pre-event failed: %d\n",
1096			       w->name, ret);
 
1097	}
1098
1099	ret = snd_soc_update_bits(w->codec, update->reg, update->mask,
1100				  update->val);
 
 
 
1101	if (ret < 0)
1102		pr_err("%s DAPM update failed: %d\n", w->name, ret);
 
1103
1104	if (w->event &&
1105	    (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1106		ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1107		if (ret != 0)
1108			pr_err("%s DAPM post-event failed: %d\n",
1109			       w->name, ret);
 
 
 
 
 
 
 
 
 
 
1110	}
1111}
1112
1113/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1114 * they're changing state.
1115 */
1116static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1117{
1118	struct snd_soc_dapm_context *d = data;
1119	int ret;
1120
1121	/* If we're off and we're not supposed to be go into STANDBY */
1122	if (d->bias_level == SND_SOC_BIAS_OFF &&
1123	    d->target_bias_level != SND_SOC_BIAS_OFF) {
 
 
 
1124		ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1125		if (ret != 0)
1126			dev_err(d->dev,
1127				"Failed to turn on bias: %d\n", ret);
1128	}
1129
1130	/* Prepare for a STADDBY->ON or ON->STANDBY transition */
1131	if (d->bias_level != d->target_bias_level) {
 
 
 
1132		ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1133		if (ret != 0)
1134			dev_err(d->dev,
1135				"Failed to prepare bias: %d\n", ret);
1136	}
1137}
1138
1139/* Async callback run prior to DAPM sequences - brings to their final
1140 * state.
1141 */
1142static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1143{
1144	struct snd_soc_dapm_context *d = data;
1145	int ret;
1146
1147	/* If we just powered the last thing off drop to standby bias */
1148	if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1149	    (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1150	     d->target_bias_level == SND_SOC_BIAS_OFF)) {
1151		ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1152		if (ret != 0)
1153			dev_err(d->dev, "Failed to apply standby bias: %d\n",
1154				ret);
1155	}
1156
1157	/* If we're in standby and can support bias off then do that */
1158	if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1159	    d->target_bias_level == SND_SOC_BIAS_OFF) {
1160		ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1161		if (ret != 0)
1162			dev_err(d->dev, "Failed to turn off bias: %d\n", ret);
 
 
 
 
1163	}
1164
1165	/* If we just powered up then move to active bias */
1166	if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1167	    d->target_bias_level == SND_SOC_BIAS_ON) {
1168		ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1169		if (ret != 0)
1170			dev_err(d->dev, "Failed to apply active bias: %d\n",
1171				ret);
1172	}
1173}
1174
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1175/*
1176 * Scan each dapm widget for complete audio path.
1177 * A complete path is a route that has valid endpoints i.e.:-
1178 *
1179 *  o DAC to output pin.
1180 *  o Input Pin to ADC.
1181 *  o Input pin to Output pin (bypass, sidetone)
1182 *  o DAC to ADC (loopback).
1183 */
1184static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
1185{
1186	struct snd_soc_card *card = dapm->card;
1187	struct snd_soc_dapm_widget *w;
1188	struct snd_soc_dapm_context *d;
1189	LIST_HEAD(up_list);
1190	LIST_HEAD(down_list);
1191	LIST_HEAD(async_domain);
1192	enum snd_soc_bias_level bias;
1193	int power;
 
 
1194
1195	trace_snd_soc_dapm_start(card);
1196
1197	list_for_each_entry(d, &card->dapm_list, list) {
1198		if (d->n_widgets || d->codec == NULL) {
1199			if (d->idle_bias_off)
1200				d->target_bias_level = SND_SOC_BIAS_OFF;
1201			else
1202				d->target_bias_level = SND_SOC_BIAS_STANDBY;
1203		}
1204	}
1205
 
 
1206	/* Check which widgets we need to power and store them in
1207	 * lists indicating if they should be powered up or down.
 
 
 
1208	 */
1209	list_for_each_entry(w, &card->widgets, list) {
 
 
 
 
1210		switch (w->id) {
1211		case snd_soc_dapm_pre:
1212			dapm_seq_insert(w, &down_list, false);
1213			break;
1214		case snd_soc_dapm_post:
1215			dapm_seq_insert(w, &up_list, true);
1216			break;
1217
1218		default:
1219			if (!w->power_check)
1220				continue;
1221
1222			if (!w->force)
1223				power = w->power_check(w);
1224			else
1225				power = 1;
1226
1227			if (power) {
1228				d = w->dapm;
1229
1230				/* Supplies and micbiases only bring
1231				 * the context up to STANDBY as unless
1232				 * something else is active and
1233				 * passing audio they generally don't
1234				 * require full power.
1235				 */
1236				switch (w->id) {
1237				case snd_soc_dapm_supply:
1238				case snd_soc_dapm_micbias:
1239					if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1240						d->target_bias_level = SND_SOC_BIAS_STANDBY;
1241					break;
1242				default:
1243					d->target_bias_level = SND_SOC_BIAS_ON;
1244					break;
1245				}
 
 
 
 
 
 
1246			}
1247
1248			if (w->power == power)
1249				continue;
1250
1251			trace_snd_soc_dapm_widget_power(w, power);
1252
1253			if (power)
1254				dapm_seq_insert(w, &up_list, true);
1255			else
1256				dapm_seq_insert(w, &down_list, false);
1257
1258			w->power = power;
1259			break;
1260		}
1261	}
1262
1263	/* If there are no DAPM widgets then try to figure out power from the
1264	 * event type.
1265	 */
1266	if (!dapm->n_widgets) {
1267		switch (event) {
1268		case SND_SOC_DAPM_STREAM_START:
1269		case SND_SOC_DAPM_STREAM_RESUME:
1270			dapm->target_bias_level = SND_SOC_BIAS_ON;
1271			break;
1272		case SND_SOC_DAPM_STREAM_STOP:
1273			if (dapm->codec->active)
1274				dapm->target_bias_level = SND_SOC_BIAS_ON;
1275			else
1276				dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
1277			break;
1278		case SND_SOC_DAPM_STREAM_SUSPEND:
1279			dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
1280			break;
1281		case SND_SOC_DAPM_STREAM_NOP:
1282			dapm->target_bias_level = dapm->bias_level;
1283			break;
1284		default:
1285			break;
1286		}
1287	}
1288
1289	/* Force all contexts in the card to the same bias state */
 
 
1290	bias = SND_SOC_BIAS_OFF;
1291	list_for_each_entry(d, &card->dapm_list, list)
1292		if (d->target_bias_level > bias)
1293			bias = d->target_bias_level;
1294	list_for_each_entry(d, &card->dapm_list, list)
1295		d->target_bias_level = bias;
 
 
 
 
 
 
 
 
 
 
 
 
 
1296
 
 
 
1297
1298	/* Run all the bias changes in parallel */
1299	list_for_each_entry(d, &dapm->card->dapm_list, list)
1300		async_schedule_domain(dapm_pre_sequence_async, d,
1301					&async_domain);
1302	async_synchronize_full_domain(&async_domain);
1303
1304	/* Power down widgets first; try to avoid amplifying pops. */
1305	dapm_seq_run(dapm, &down_list, event, false);
1306
1307	dapm_widget_update(dapm);
1308
1309	/* Now power up. */
1310	dapm_seq_run(dapm, &up_list, event, true);
1311
1312	/* Run all the bias changes in parallel */
1313	list_for_each_entry(d, &dapm->card->dapm_list, list)
1314		async_schedule_domain(dapm_post_sequence_async, d,
1315					&async_domain);
 
 
1316	async_synchronize_full_domain(&async_domain);
 
 
 
 
 
 
 
 
 
 
 
 
1317
1318	pop_dbg(dapm->dev, card->pop_time,
1319		"DAPM sequencing finished, waiting %dms\n", card->pop_time);
1320	pop_wait(card->pop_time);
1321
1322	trace_snd_soc_dapm_done(card);
1323
1324	return 0;
1325}
1326
1327#ifdef CONFIG_DEBUG_FS
1328static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
1329{
1330	file->private_data = inode->i_private;
1331	return 0;
1332}
1333
1334static ssize_t dapm_widget_power_read_file(struct file *file,
1335					   char __user *user_buf,
1336					   size_t count, loff_t *ppos)
1337{
1338	struct snd_soc_dapm_widget *w = file->private_data;
 
1339	char *buf;
1340	int in, out;
1341	ssize_t ret;
1342	struct snd_soc_dapm_path *p = NULL;
1343
1344	buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1345	if (!buf)
1346		return -ENOMEM;
1347
1348	in = is_connected_input_ep(w);
1349	dapm_clear_walk(w->dapm);
1350	out = is_connected_output_ep(w);
1351	dapm_clear_walk(w->dapm);
1352
1353	ret = snprintf(buf, PAGE_SIZE, "%s: %s  in %d out %d",
1354		       w->name, w->power ? "On" : "Off", in, out);
 
 
 
 
 
 
 
 
 
 
1355
1356	if (w->reg >= 0)
1357		ret += snprintf(buf + ret, PAGE_SIZE - ret,
1358				" - R%d(0x%x) bit %d",
1359				w->reg, w->reg, w->shift);
1360
1361	ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1362
1363	if (w->sname)
1364		ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1365				w->sname,
1366				w->active ? "active" : "inactive");
1367
1368	list_for_each_entry(p, &w->sources, list_sink) {
1369		if (p->connected && !p->connected(w, p->sink))
1370			continue;
 
 
1371
1372		if (p->connect)
1373			ret += snprintf(buf + ret, PAGE_SIZE - ret,
1374					" in  \"%s\" \"%s\"\n",
1375					p->name ? p->name : "static",
1376					p->source->name);
1377	}
1378	list_for_each_entry(p, &w->sinks, list_source) {
1379		if (p->connected && !p->connected(w, p->sink))
1380			continue;
1381
1382		if (p->connect)
1383			ret += snprintf(buf + ret, PAGE_SIZE - ret,
1384					" out \"%s\" \"%s\"\n",
1385					p->name ? p->name : "static",
1386					p->sink->name);
 
1387	}
1388
 
 
1389	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1390
1391	kfree(buf);
1392	return ret;
1393}
1394
1395static const struct file_operations dapm_widget_power_fops = {
1396	.open = dapm_widget_power_open_file,
1397	.read = dapm_widget_power_read_file,
1398	.llseek = default_llseek,
1399};
1400
1401static int dapm_bias_open_file(struct inode *inode, struct file *file)
1402{
1403	file->private_data = inode->i_private;
1404	return 0;
1405}
1406
1407static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1408				   size_t count, loff_t *ppos)
1409{
1410	struct snd_soc_dapm_context *dapm = file->private_data;
1411	char *level;
1412
1413	switch (dapm->bias_level) {
1414	case SND_SOC_BIAS_ON:
1415		level = "On\n";
1416		break;
1417	case SND_SOC_BIAS_PREPARE:
1418		level = "Prepare\n";
1419		break;
1420	case SND_SOC_BIAS_STANDBY:
1421		level = "Standby\n";
1422		break;
1423	case SND_SOC_BIAS_OFF:
1424		level = "Off\n";
1425		break;
1426	default:
1427		BUG();
1428		level = "Unknown\n";
1429		break;
1430	}
1431
1432	return simple_read_from_buffer(user_buf, count, ppos, level,
1433				       strlen(level));
1434}
1435
1436static const struct file_operations dapm_bias_fops = {
1437	.open = dapm_bias_open_file,
1438	.read = dapm_bias_read_file,
1439	.llseek = default_llseek,
1440};
1441
1442void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1443	struct dentry *parent)
1444{
1445	struct dentry *d;
 
1446
1447	dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1448
1449	if (!dapm->debugfs_dapm) {
1450		printk(KERN_WARNING
1451		       "Failed to create DAPM debugfs directory\n");
 
 
 
 
 
 
1452		return;
1453	}
1454
1455	d = debugfs_create_file("bias_level", 0444,
1456				dapm->debugfs_dapm, dapm,
1457				&dapm_bias_fops);
1458	if (!d)
1459		dev_warn(dapm->dev,
1460			 "ASoC: Failed to create bias level debugfs file\n");
1461}
1462
1463static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1464{
1465	struct snd_soc_dapm_context *dapm = w->dapm;
1466	struct dentry *d;
1467
1468	if (!dapm->debugfs_dapm || !w->name)
1469		return;
1470
1471	d = debugfs_create_file(w->name, 0444,
1472				dapm->debugfs_dapm, w,
1473				&dapm_widget_power_fops);
1474	if (!d)
1475		dev_warn(w->dapm->dev,
1476			"ASoC: Failed to create %s debugfs file\n",
1477			w->name);
1478}
1479
1480static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1481{
1482	debugfs_remove_recursive(dapm->debugfs_dapm);
 
1483}
1484
1485#else
1486void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1487	struct dentry *parent)
1488{
1489}
1490
1491static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1492{
1493}
1494
 
 
 
 
1495static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1496{
1497}
1498
1499#endif
1500
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1501/* test and update the power status of a mux widget */
1502static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1503				 struct snd_kcontrol *kcontrol, int change,
1504				 int mux, struct soc_enum *e)
1505{
1506	struct snd_soc_dapm_path *path;
1507	int found = 0;
 
1508
1509	if (widget->id != snd_soc_dapm_mux &&
1510	    widget->id != snd_soc_dapm_virt_mux &&
1511	    widget->id != snd_soc_dapm_value_mux)
1512		return -ENODEV;
1513
1514	if (!change)
1515		return 0;
1516
1517	/* find dapm widget path assoc with kcontrol */
1518	list_for_each_entry(path, &widget->dapm->card->paths, list) {
1519		if (path->kcontrol != kcontrol)
1520			continue;
1521
1522		if (!path->name || !e->texts[mux])
1523			continue;
1524
1525		found = 1;
1526		/* we now need to match the string in the enum to the path */
1527		if (!(strcmp(path->name, e->texts[mux])))
1528			path->connect = 1; /* new connection */
1529		else
1530			path->connect = 0; /* old connection must be powered down */
 
 
1531	}
1532
1533	if (found)
1534		dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1535
1536	return 0;
1537}
1538
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1539/* test and update the power status of a mixer or switch widget */
1540static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
1541				   struct snd_kcontrol *kcontrol, int connect)
 
1542{
1543	struct snd_soc_dapm_path *path;
1544	int found = 0;
1545
1546	if (widget->id != snd_soc_dapm_mixer &&
1547	    widget->id != snd_soc_dapm_mixer_named_ctl &&
1548	    widget->id != snd_soc_dapm_switch)
1549		return -ENODEV;
1550
1551	/* find dapm widget path assoc with kcontrol */
1552	list_for_each_entry(path, &widget->dapm->card->paths, list) {
1553		if (path->kcontrol != kcontrol)
1554			continue;
1555
1556		/* found, now check type */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1557		found = 1;
1558		path->connect = connect;
1559		break;
1560	}
1561
1562	if (found)
1563		dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1564
1565	return 0;
1566}
1567
1568/* show dapm widget status in sys fs */
1569static ssize_t dapm_widget_show(struct device *dev,
1570	struct device_attribute *attr, char *buf)
1571{
1572	struct snd_soc_pcm_runtime *rtd =
1573			container_of(dev, struct snd_soc_pcm_runtime, dev);
1574	struct snd_soc_codec *codec =rtd->codec;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1575	struct snd_soc_dapm_widget *w;
1576	int count = 0;
1577	char *state = "not set";
1578
1579	list_for_each_entry(w, &codec->card->widgets, list) {
1580		if (w->dapm != &codec->dapm)
 
 
 
 
 
 
 
1581			continue;
1582
1583		/* only display widgets that burnm power */
1584		switch (w->id) {
1585		case snd_soc_dapm_hp:
1586		case snd_soc_dapm_mic:
1587		case snd_soc_dapm_spk:
1588		case snd_soc_dapm_line:
1589		case snd_soc_dapm_micbias:
1590		case snd_soc_dapm_dac:
1591		case snd_soc_dapm_adc:
1592		case snd_soc_dapm_pga:
 
1593		case snd_soc_dapm_out_drv:
1594		case snd_soc_dapm_mixer:
1595		case snd_soc_dapm_mixer_named_ctl:
1596		case snd_soc_dapm_supply:
 
 
 
1597			if (w->name)
1598				count += sprintf(buf + count, "%s: %s\n",
1599					w->name, w->power ? "On":"Off");
1600		break;
1601		default:
1602		break;
1603		}
1604	}
1605
1606	switch (codec->dapm.bias_level) {
1607	case SND_SOC_BIAS_ON:
1608		state = "On";
1609		break;
1610	case SND_SOC_BIAS_PREPARE:
1611		state = "Prepare";
1612		break;
1613	case SND_SOC_BIAS_STANDBY:
1614		state = "Standby";
1615		break;
1616	case SND_SOC_BIAS_OFF:
1617		state = "Off";
1618		break;
1619	}
1620	count += sprintf(buf + count, "PM State: %s\n", state);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1621
1622	return count;
1623}
1624
1625static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
1626
1627int snd_soc_dapm_sys_add(struct device *dev)
 
 
 
 
 
1628{
1629	return device_create_file(dev, &dev_attr_dapm_widget);
 
 
 
 
1630}
1631
1632static void snd_soc_dapm_sys_remove(struct device *dev)
 
 
 
 
 
 
1633{
1634	device_remove_file(dev, &dev_attr_dapm_widget);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1635}
 
1636
1637/* free all dapm widgets and resources */
1638static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
1639{
1640	struct snd_soc_dapm_widget *w, *next_w;
1641	struct snd_soc_dapm_path *p, *next_p;
1642
1643	list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1644		if (w->dapm != dapm)
1645			continue;
1646		list_del(&w->list);
1647		/*
1648		 * remove source and sink paths associated to this widget.
1649		 * While removing the path, remove reference to it from both
1650		 * source and sink widgets so that path is removed only once.
1651		 */
1652		list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
1653			list_del(&p->list_sink);
1654			list_del(&p->list_source);
1655			list_del(&p->list);
1656			kfree(p->long_name);
1657			kfree(p);
1658		}
1659		list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
1660			list_del(&p->list_sink);
1661			list_del(&p->list_source);
1662			list_del(&p->list);
1663			kfree(p->long_name);
1664			kfree(p);
1665		}
1666		kfree(w->kcontrols);
1667		kfree(w->name);
1668		kfree(w);
1669	}
 
 
 
1670}
1671
1672static struct snd_soc_dapm_widget *dapm_find_widget(
1673			struct snd_soc_dapm_context *dapm, const char *pin,
1674			bool search_other_contexts)
1675{
1676	struct snd_soc_dapm_widget *w;
1677	struct snd_soc_dapm_widget *fallback = NULL;
 
 
 
 
 
 
 
 
 
 
 
1678
1679	list_for_each_entry(w, &dapm->card->widgets, list) {
1680		if (!strcmp(w->name, pin)) {
1681			if (w->dapm == dapm)
1682				return w;
1683			else
1684				fallback = w;
1685		}
1686	}
1687
1688	if (search_other_contexts)
1689		return fallback;
1690
1691	return NULL;
1692}
1693
1694static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
1695				const char *pin, int status)
 
 
 
 
 
1696{
1697	struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
 
 
 
1698
1699	if (!w) {
1700		dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
1701		return -EINVAL;
1702	}
1703
 
 
 
 
 
 
 
1704	w->connected = status;
1705	if (status == 0)
1706		w->force = 0;
1707
1708	return 0;
 
 
 
 
 
 
 
 
 
 
 
 
1709}
1710
1711/**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1712 * snd_soc_dapm_sync - scan and power dapm paths
1713 * @dapm: DAPM context
1714 *
1715 * Walks all dapm audio paths and powers widgets according to their
1716 * stream or path usage.
1717 *
1718 * Returns 0 for success.
1719 */
1720int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
1721{
1722	return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
 
 
 
 
 
1723}
1724EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
1725
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1726static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
1727				  const struct snd_soc_dapm_route *route)
1728{
1729	struct snd_soc_dapm_path *path;
1730	struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
1731	struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
1732	const char *sink;
1733	const char *control = route->control;
1734	const char *source;
1735	char prefixed_sink[80];
1736	char prefixed_source[80];
1737	int ret = 0;
 
 
 
1738
1739	if (dapm->codec && dapm->codec->name_prefix) {
 
1740		snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
1741			 dapm->codec->name_prefix, route->sink);
1742		sink = prefixed_sink;
1743		snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
1744			 dapm->codec->name_prefix, route->source);
1745		source = prefixed_source;
1746	} else {
1747		sink = route->sink;
1748		source = route->source;
1749	}
1750
 
 
 
 
 
 
1751	/*
1752	 * find src and dest widgets over all widgets but favor a widget from
1753	 * current DAPM context
1754	 */
1755	list_for_each_entry(w, &dapm->card->widgets, list) {
1756		if (!wsink && !(strcmp(w->name, sink))) {
1757			wtsink = w;
1758			if (w->dapm == dapm)
1759				wsink = w;
 
 
 
 
 
 
 
 
1760			continue;
1761		}
1762		if (!wsource && !(strcmp(w->name, source))) {
1763			wtsource = w;
1764			if (w->dapm == dapm)
1765				wsource = w;
 
 
 
 
 
 
 
 
1766		}
1767	}
1768	/* use widget from another DAPM context if not found from this */
1769	if (!wsink)
1770		wsink = wtsink;
1771	if (!wsource)
1772		wsource = wtsource;
1773
1774	if (wsource == NULL || wsink == NULL)
1775		return -ENODEV;
 
 
 
1776
1777	path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
1778	if (!path)
1779		return -ENOMEM;
 
1780
1781	path->source = wsource;
1782	path->sink = wsink;
1783	path->connected = route->connected;
1784	INIT_LIST_HEAD(&path->list);
1785	INIT_LIST_HEAD(&path->list_source);
1786	INIT_LIST_HEAD(&path->list_sink);
 
 
 
 
 
 
1787
1788	/* check for external widgets */
1789	if (wsink->id == snd_soc_dapm_input) {
1790		if (wsource->id == snd_soc_dapm_micbias ||
1791			wsource->id == snd_soc_dapm_mic ||
1792			wsource->id == snd_soc_dapm_line ||
1793			wsource->id == snd_soc_dapm_output)
1794			wsink->ext = 1;
1795	}
1796	if (wsource->id == snd_soc_dapm_output) {
1797		if (wsink->id == snd_soc_dapm_spk ||
1798			wsink->id == snd_soc_dapm_hp ||
1799			wsink->id == snd_soc_dapm_line ||
1800			wsink->id == snd_soc_dapm_input)
1801			wsource->ext = 1;
1802	}
1803
1804	/* connect static paths */
1805	if (control == NULL) {
1806		list_add(&path->list, &dapm->card->paths);
1807		list_add(&path->list_sink, &wsink->sources);
1808		list_add(&path->list_source, &wsource->sinks);
1809		path->connect = 1;
1810		return 0;
 
 
 
 
1811	}
1812
1813	/* connect dynamic paths */
1814	switch (wsink->id) {
1815	case snd_soc_dapm_adc:
1816	case snd_soc_dapm_dac:
1817	case snd_soc_dapm_pga:
1818	case snd_soc_dapm_out_drv:
1819	case snd_soc_dapm_input:
1820	case snd_soc_dapm_output:
1821	case snd_soc_dapm_micbias:
1822	case snd_soc_dapm_vmid:
1823	case snd_soc_dapm_pre:
1824	case snd_soc_dapm_post:
1825	case snd_soc_dapm_supply:
1826	case snd_soc_dapm_aif_in:
1827	case snd_soc_dapm_aif_out:
1828		list_add(&path->list, &dapm->card->paths);
1829		list_add(&path->list_sink, &wsink->sources);
1830		list_add(&path->list_source, &wsource->sinks);
1831		path->connect = 1;
1832		return 0;
1833	case snd_soc_dapm_mux:
1834	case snd_soc_dapm_virt_mux:
1835	case snd_soc_dapm_value_mux:
1836		ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
1837			&wsink->kcontrol_news[0]);
1838		if (ret != 0)
1839			goto err;
1840		break;
1841	case snd_soc_dapm_switch:
1842	case snd_soc_dapm_mixer:
1843	case snd_soc_dapm_mixer_named_ctl:
1844		ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
1845		if (ret != 0)
1846			goto err;
1847		break;
1848	case snd_soc_dapm_hp:
1849	case snd_soc_dapm_mic:
1850	case snd_soc_dapm_line:
1851	case snd_soc_dapm_spk:
1852		list_add(&path->list, &dapm->card->paths);
1853		list_add(&path->list_sink, &wsink->sources);
1854		list_add(&path->list_source, &wsource->sinks);
1855		path->connect = 0;
1856		return 0;
1857	}
1858	return 0;
1859
1860err:
1861	dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
1862		 source, control, sink);
1863	kfree(path);
1864	return ret;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1865}
1866
1867/**
1868 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
1869 * @dapm: DAPM context
1870 * @route: audio routes
1871 * @num: number of routes
1872 *
1873 * Connects 2 dapm widgets together via a named audio path. The sink is
1874 * the widget receiving the audio signal, whilst the source is the sender
1875 * of the audio signal.
1876 *
1877 * Returns 0 for success else error. On error all resources can be freed
1878 * with a call to snd_soc_card_free().
1879 */
1880int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
1881			    const struct snd_soc_dapm_route *route, int num)
1882{
1883	int i, ret;
1884
 
1885	for (i = 0; i < num; i++) {
1886		ret = snd_soc_dapm_add_route(dapm, route);
1887		if (ret < 0) {
1888			dev_err(dapm->dev, "Failed to add route %s->%s\n",
1889				route->source, route->sink);
1890			return ret;
1891		}
1892		route++;
1893	}
 
1894
1895	return 0;
1896}
1897EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
1898
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1899static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
1900				   const struct snd_soc_dapm_route *route)
1901{
1902	struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
1903							      route->source,
1904							      true);
1905	struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
1906							    route->sink,
1907							    true);
1908	struct snd_soc_dapm_path *path;
1909	int count = 0;
1910
1911	if (!source) {
1912		dev_err(dapm->dev, "Unable to find source %s for weak route\n",
1913			route->source);
1914		return -ENODEV;
1915	}
1916
1917	if (!sink) {
1918		dev_err(dapm->dev, "Unable to find sink %s for weak route\n",
1919			route->sink);
1920		return -ENODEV;
1921	}
1922
1923	if (route->control || route->connected)
1924		dev_warn(dapm->dev, "Ignoring control for weak route %s->%s\n",
1925			 route->source, route->sink);
1926
1927	list_for_each_entry(path, &source->sinks, list_source) {
1928		if (path->sink == sink) {
1929			path->weak = 1;
1930			count++;
1931		}
1932	}
1933
1934	if (count == 0)
1935		dev_err(dapm->dev, "No path found for weak route %s->%s\n",
1936			route->source, route->sink);
1937	if (count > 1)
1938		dev_warn(dapm->dev, "%d paths found for weak route %s->%s\n",
1939			 count, route->source, route->sink);
1940
1941	return 0;
1942}
1943
1944/**
1945 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
1946 * @dapm: DAPM context
1947 * @route: audio routes
1948 * @num: number of routes
1949 *
1950 * Mark existing routes matching those specified in the passed array
1951 * as being weak, meaning that they are ignored for the purpose of
1952 * power decisions.  The main intended use case is for sidetone paths
1953 * which couple audio between other independent paths if they are both
1954 * active in order to make the combination work better at the user
1955 * level but which aren't intended to be "used".
1956 *
1957 * Note that CODEC drivers should not use this as sidetone type paths
1958 * can frequently also be used as bypass paths.
1959 */
1960int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
1961			     const struct snd_soc_dapm_route *route, int num)
1962{
1963	int i, err;
1964	int ret = 0;
1965
 
1966	for (i = 0; i < num; i++) {
1967		err = snd_soc_dapm_weak_route(dapm, route);
1968		if (err)
1969			ret = err;
1970		route++;
1971	}
 
1972
1973	return ret;
1974}
1975EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
1976
1977/**
1978 * snd_soc_dapm_new_widgets - add new dapm widgets
1979 * @dapm: DAPM context
1980 *
1981 * Checks the codec for any new dapm widgets and creates them if found.
1982 *
1983 * Returns 0 for success.
1984 */
1985int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
1986{
1987	struct snd_soc_dapm_widget *w;
1988	unsigned int val;
1989
1990	list_for_each_entry(w, &dapm->card->widgets, list)
 
 
1991	{
1992		if (w->new)
1993			continue;
1994
1995		if (w->num_kcontrols) {
1996			w->kcontrols = kzalloc(w->num_kcontrols *
1997						sizeof(struct snd_kcontrol *),
1998						GFP_KERNEL);
1999			if (!w->kcontrols)
 
2000				return -ENOMEM;
 
2001		}
2002
2003		switch(w->id) {
2004		case snd_soc_dapm_switch:
2005		case snd_soc_dapm_mixer:
2006		case snd_soc_dapm_mixer_named_ctl:
2007			w->power_check = dapm_generic_check_power;
2008			dapm_new_mixer(w);
2009			break;
2010		case snd_soc_dapm_mux:
2011		case snd_soc_dapm_virt_mux:
2012		case snd_soc_dapm_value_mux:
2013			w->power_check = dapm_generic_check_power;
2014			dapm_new_mux(w);
2015			break;
2016		case snd_soc_dapm_adc:
2017		case snd_soc_dapm_aif_out:
2018			w->power_check = dapm_adc_check_power;
2019			break;
2020		case snd_soc_dapm_dac:
2021		case snd_soc_dapm_aif_in:
2022			w->power_check = dapm_dac_check_power;
2023			break;
2024		case snd_soc_dapm_pga:
 
2025		case snd_soc_dapm_out_drv:
2026			w->power_check = dapm_generic_check_power;
2027			dapm_new_pga(w);
2028			break;
2029		case snd_soc_dapm_input:
2030		case snd_soc_dapm_output:
2031		case snd_soc_dapm_micbias:
2032		case snd_soc_dapm_spk:
2033		case snd_soc_dapm_hp:
2034		case snd_soc_dapm_mic:
2035		case snd_soc_dapm_line:
2036			w->power_check = dapm_generic_check_power;
2037			break;
2038		case snd_soc_dapm_supply:
2039			w->power_check = dapm_supply_check_power;
2040		case snd_soc_dapm_vmid:
2041		case snd_soc_dapm_pre:
2042		case snd_soc_dapm_post:
2043			break;
2044		}
2045
2046		/* Read the initial power state from the device */
2047		if (w->reg >= 0) {
2048			val = soc_widget_read(w, w->reg);
2049			val &= 1 << w->shift;
2050			if (w->invert)
2051				val = !val;
2052
2053			if (val)
2054				w->power = 1;
2055		}
2056
2057		w->new = 1;
2058
 
2059		dapm_debugfs_add_widget(w);
2060	}
2061
2062	dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
 
2063	return 0;
2064}
2065EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2066
2067/**
2068 * snd_soc_dapm_get_volsw - dapm mixer get callback
2069 * @kcontrol: mixer control
2070 * @ucontrol: control element information
2071 *
2072 * Callback to get the value of a dapm mixer control.
2073 *
2074 * Returns 0 for success.
2075 */
2076int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2077	struct snd_ctl_elem_value *ucontrol)
2078{
2079	struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2080	struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2081	struct soc_mixer_control *mc =
2082		(struct soc_mixer_control *)kcontrol->private_value;
2083	unsigned int reg = mc->reg;
2084	unsigned int shift = mc->shift;
2085	unsigned int rshift = mc->rshift;
2086	int max = mc->max;
2087	unsigned int invert = mc->invert;
2088	unsigned int mask = (1 << fls(max)) - 1;
 
 
2089
2090	ucontrol->value.integer.value[0] =
2091		(snd_soc_read(widget->codec, reg) >> shift) & mask;
2092	if (shift != rshift)
2093		ucontrol->value.integer.value[1] =
2094			(snd_soc_read(widget->codec, reg) >> rshift) & mask;
2095	if (invert) {
2096		ucontrol->value.integer.value[0] =
2097			max - ucontrol->value.integer.value[0];
2098		if (shift != rshift)
2099			ucontrol->value.integer.value[1] =
2100				max - ucontrol->value.integer.value[1];
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2101	}
2102
2103	return 0;
2104}
2105EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2106
2107/**
2108 * snd_soc_dapm_put_volsw - dapm mixer set callback
2109 * @kcontrol: mixer control
2110 * @ucontrol: control element information
2111 *
2112 * Callback to set the value of a dapm mixer control.
2113 *
2114 * Returns 0 for success.
2115 */
2116int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2117	struct snd_ctl_elem_value *ucontrol)
2118{
2119	struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2120	struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2121	struct snd_soc_codec *codec = widget->codec;
2122	struct soc_mixer_control *mc =
2123		(struct soc_mixer_control *)kcontrol->private_value;
2124	unsigned int reg = mc->reg;
2125	unsigned int shift = mc->shift;
2126	int max = mc->max;
2127	unsigned int mask = (1 << fls(max)) - 1;
 
2128	unsigned int invert = mc->invert;
2129	unsigned int val;
2130	int connect, change;
2131	struct snd_soc_dapm_update update;
2132	int wi;
2133
2134	val = (ucontrol->value.integer.value[0] & mask);
 
2135
2136	if (invert)
2137		val = max - val;
2138	mask = mask << shift;
2139	val = val << shift;
2140
2141	if (val)
2142		/* new connection */
2143		connect = invert ? 0 : 1;
2144	else
2145		/* old connection must be powered down */
2146		connect = invert ? 1 : 0;
2147
2148	mutex_lock(&codec->mutex);
2149
2150	change = snd_soc_test_bits(widget->codec, reg, mask, val);
2151	if (change) {
2152		for (wi = 0; wi < wlist->num_widgets; wi++) {
2153			widget = wlist->widgets[wi];
 
 
2154
2155			widget->value = val;
2156
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2157			update.kcontrol = kcontrol;
2158			update.widget = widget;
2159			update.reg = reg;
2160			update.mask = mask;
2161			update.val = val;
2162			widget->dapm->update = &update;
 
2163
2164			dapm_mixer_update_power(widget, kcontrol, connect);
 
2165
2166			widget->dapm->update = NULL;
2167		}
2168	}
2169
2170	mutex_unlock(&codec->mutex);
2171	return 0;
 
 
 
 
2172}
2173EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2174
2175/**
2176 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2177 * @kcontrol: mixer control
2178 * @ucontrol: control element information
2179 *
2180 * Callback to get the value of a dapm enumerated double mixer control.
2181 *
2182 * Returns 0 for success.
2183 */
2184int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2185	struct snd_ctl_elem_value *ucontrol)
2186{
2187	struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2188	struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2189	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2190	unsigned int val, bitmask;
2191
2192	for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2193		;
2194	val = snd_soc_read(widget->codec, e->reg);
2195	ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
2196	if (e->shift_l != e->shift_r)
2197		ucontrol->value.enumerated.item[1] =
2198			(val >> e->shift_r) & (bitmask - 1);
2199
2200	return 0;
2201}
2202EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2203
2204/**
2205 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2206 * @kcontrol: mixer control
2207 * @ucontrol: control element information
2208 *
2209 * Callback to set the value of a dapm enumerated double mixer control.
2210 *
2211 * Returns 0 for success.
2212 */
2213int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2214	struct snd_ctl_elem_value *ucontrol)
2215{
2216	struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2217	struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2218	struct snd_soc_codec *codec = widget->codec;
2219	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2220	unsigned int val, mux, change;
2221	unsigned int mask, bitmask;
2222	struct snd_soc_dapm_update update;
2223	int wi;
2224
2225	for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2226		;
2227	if (ucontrol->value.enumerated.item[0] > e->max - 1)
2228		return -EINVAL;
2229	mux = ucontrol->value.enumerated.item[0];
2230	val = mux << e->shift_l;
2231	mask = (bitmask - 1) << e->shift_l;
2232	if (e->shift_l != e->shift_r) {
2233		if (ucontrol->value.enumerated.item[1] > e->max - 1)
2234			return -EINVAL;
2235		val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2236		mask |= (bitmask - 1) << e->shift_r;
2237	}
2238
2239	mutex_lock(&codec->mutex);
2240
2241	change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
2242	if (change) {
2243		for (wi = 0; wi < wlist->num_widgets; wi++) {
2244			widget = wlist->widgets[wi];
2245
2246			widget->value = val;
2247
2248			update.kcontrol = kcontrol;
2249			update.widget = widget;
2250			update.reg = e->reg;
2251			update.mask = mask;
2252			update.val = val;
2253			widget->dapm->update = &update;
2254
2255			dapm_mux_update_power(widget, kcontrol, change, mux, e);
2256
2257			widget->dapm->update = NULL;
2258		}
2259	}
2260
2261	mutex_unlock(&codec->mutex);
2262	return change;
2263}
2264EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2265
2266/**
2267 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2268 * @kcontrol: mixer control
2269 * @ucontrol: control element information
2270 *
2271 * Returns 0 for success.
2272 */
2273int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2274			       struct snd_ctl_elem_value *ucontrol)
2275{
2276	struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2277	struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2278
2279	ucontrol->value.enumerated.item[0] = widget->value;
2280
2281	return 0;
2282}
2283EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2284
2285/**
2286 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2287 * @kcontrol: mixer control
2288 * @ucontrol: control element information
2289 *
2290 * Returns 0 for success.
2291 */
2292int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2293			       struct snd_ctl_elem_value *ucontrol)
2294{
2295	struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2296	struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2297	struct snd_soc_codec *codec = widget->codec;
2298	struct soc_enum *e =
2299		(struct soc_enum *)kcontrol->private_value;
2300	int change;
2301	int ret = 0;
2302	int wi;
2303
2304	if (ucontrol->value.enumerated.item[0] >= e->max)
2305		return -EINVAL;
2306
2307	mutex_lock(&codec->mutex);
2308
2309	change = widget->value != ucontrol->value.enumerated.item[0];
2310	if (change) {
2311		for (wi = 0; wi < wlist->num_widgets; wi++) {
2312			widget = wlist->widgets[wi];
2313
2314			widget->value = ucontrol->value.enumerated.item[0];
2315
2316			dapm_mux_update_power(widget, kcontrol, change,
2317					      widget->value, e);
2318		}
2319	}
 
2320
2321	mutex_unlock(&codec->mutex);
2322	return ret;
2323}
2324EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2325
2326/**
2327 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2328 *					callback
2329 * @kcontrol: mixer control
2330 * @ucontrol: control element information
2331 *
2332 * Callback to get the value of a dapm semi enumerated double mixer control.
2333 *
2334 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2335 * used for handling bitfield coded enumeration for example.
2336 *
2337 * Returns 0 for success.
2338 */
2339int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2340	struct snd_ctl_elem_value *ucontrol)
2341{
2342	struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2343	struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2344	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2345	unsigned int reg_val, val, mux;
2346
2347	reg_val = snd_soc_read(widget->codec, e->reg);
2348	val = (reg_val >> e->shift_l) & e->mask;
2349	for (mux = 0; mux < e->max; mux++) {
2350		if (val == e->values[mux])
2351			break;
2352	}
2353	ucontrol->value.enumerated.item[0] = mux;
2354	if (e->shift_l != e->shift_r) {
2355		val = (reg_val >> e->shift_r) & e->mask;
2356		for (mux = 0; mux < e->max; mux++) {
2357			if (val == e->values[mux])
2358				break;
2359		}
2360		ucontrol->value.enumerated.item[1] = mux;
2361	}
2362
2363	return 0;
2364}
2365EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2366
2367/**
2368 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2369 *					callback
2370 * @kcontrol: mixer control
2371 * @ucontrol: control element information
2372 *
2373 * Callback to set the value of a dapm semi enumerated double mixer control.
2374 *
2375 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2376 * used for handling bitfield coded enumeration for example.
2377 *
2378 * Returns 0 for success.
2379 */
2380int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2381	struct snd_ctl_elem_value *ucontrol)
2382{
2383	struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2384	struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2385	struct snd_soc_codec *codec = widget->codec;
2386	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2387	unsigned int val, mux, change;
 
2388	unsigned int mask;
2389	struct snd_soc_dapm_update update;
2390	int wi;
2391
2392	if (ucontrol->value.enumerated.item[0] > e->max - 1)
2393		return -EINVAL;
2394	mux = ucontrol->value.enumerated.item[0];
2395	val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2396	mask = e->mask << e->shift_l;
2397	if (e->shift_l != e->shift_r) {
2398		if (ucontrol->value.enumerated.item[1] > e->max - 1)
2399			return -EINVAL;
2400		val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2401		mask |= e->mask << e->shift_r;
2402	}
2403
2404	mutex_lock(&codec->mutex);
2405
2406	change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
2407	if (change) {
2408		for (wi = 0; wi < wlist->num_widgets; wi++) {
2409			widget = wlist->widgets[wi];
2410
2411			widget->value = val;
 
2412
 
 
2413			update.kcontrol = kcontrol;
2414			update.widget = widget;
2415			update.reg = e->reg;
2416			update.mask = mask;
2417			update.val = val;
2418			widget->dapm->update = &update;
 
2419
2420			dapm_mux_update_power(widget, kcontrol, change, mux, e);
2421
2422			widget->dapm->update = NULL;
2423		}
2424	}
2425
2426	mutex_unlock(&codec->mutex);
 
 
 
 
2427	return change;
2428}
2429EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2430
2431/**
2432 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2433 *
2434 * @kcontrol: mixer control
2435 * @uinfo: control element information
2436 *
2437 * Callback to provide information about a pin switch control.
2438 */
2439int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2440				 struct snd_ctl_elem_info *uinfo)
2441{
2442	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2443	uinfo->count = 1;
2444	uinfo->value.integer.min = 0;
2445	uinfo->value.integer.max = 1;
2446
2447	return 0;
2448}
2449EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2450
2451/**
2452 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2453 *
2454 * @kcontrol: mixer control
2455 * @ucontrol: Value
2456 */
2457int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2458				struct snd_ctl_elem_value *ucontrol)
2459{
2460	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2461	const char *pin = (const char *)kcontrol->private_value;
2462
2463	mutex_lock(&codec->mutex);
2464
2465	ucontrol->value.integer.value[0] =
2466		snd_soc_dapm_get_pin_status(&codec->dapm, pin);
2467
2468	mutex_unlock(&codec->mutex);
2469
2470	return 0;
2471}
2472EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2473
2474/**
2475 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2476 *
2477 * @kcontrol: mixer control
2478 * @ucontrol: Value
2479 */
2480int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2481				struct snd_ctl_elem_value *ucontrol)
2482{
2483	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2484	const char *pin = (const char *)kcontrol->private_value;
 
2485
2486	mutex_lock(&codec->mutex);
 
 
 
2487
2488	if (ucontrol->value.integer.value[0])
2489		snd_soc_dapm_enable_pin(&codec->dapm, pin);
2490	else
2491		snd_soc_dapm_disable_pin(&codec->dapm, pin);
2492
2493	snd_soc_dapm_sync(&codec->dapm);
 
 
 
 
 
 
2494
2495	mutex_unlock(&codec->mutex);
 
 
2496
2497	return 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2498}
2499EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
2500
2501/**
2502 * snd_soc_dapm_new_control - create new dapm control
2503 * @dapm: DAPM context
2504 * @widget: widget template
2505 *
2506 * Creates a new dapm control based upon the template.
2507 *
2508 * Returns 0 for success else error.
2509 */
2510int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
2511	const struct snd_soc_dapm_widget *widget)
 
2512{
2513	struct snd_soc_dapm_widget *w;
2514	size_t name_len;
2515
2516	if ((w = dapm_cnew_widget(widget)) == NULL)
2517		return -ENOMEM;
2518
2519	name_len = strlen(widget->name) + 1;
2520	if (dapm->codec && dapm->codec->name_prefix)
2521		name_len += 1 + strlen(dapm->codec->name_prefix);
2522	w->name = kmalloc(name_len, GFP_KERNEL);
2523	if (w->name == NULL) {
2524		kfree(w);
2525		return -ENOMEM;
2526	}
2527	if (dapm->codec && dapm->codec->name_prefix)
2528		snprintf(w->name, name_len, "%s %s",
2529			dapm->codec->name_prefix, widget->name);
2530	else
2531		snprintf(w->name, name_len, "%s", widget->name);
2532
2533	dapm->n_widgets++;
2534	w->dapm = dapm;
2535	w->codec = dapm->codec;
2536	w->platform = dapm->platform;
2537	INIT_LIST_HEAD(&w->sources);
2538	INIT_LIST_HEAD(&w->sinks);
2539	INIT_LIST_HEAD(&w->list);
2540	list_add(&w->list, &dapm->card->widgets);
2541
2542	/* machine layer set ups unconnected pins and insertions */
2543	w->connected = 1;
2544	return 0;
2545}
2546EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
2547
2548/**
2549 * snd_soc_dapm_new_controls - create new dapm controls
2550 * @dapm: DAPM context
2551 * @widget: widget array
2552 * @num: number of widgets
2553 *
2554 * Creates new DAPM controls based upon the templates.
2555 *
2556 * Returns 0 for success else error.
2557 */
2558int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
2559	const struct snd_soc_dapm_widget *widget,
2560	int num)
2561{
2562	int i, ret;
 
2563
 
2564	for (i = 0; i < num; i++) {
2565		ret = snd_soc_dapm_new_control(dapm, widget);
2566		if (ret < 0) {
2567			dev_err(dapm->dev,
2568				"ASoC: Failed to create DAPM control %s: %d\n",
2569				widget->name, ret);
2570			return ret;
2571		}
2572		widget++;
2573	}
2574	return 0;
 
2575}
2576EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
2577
2578static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
2579	const char *stream, int event)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2580{
 
 
2581	struct snd_soc_dapm_widget *w;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2582
2583	list_for_each_entry(w, &dapm->card->widgets, list)
2584	{
2585		if (!w->sname || w->dapm != dapm)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2586			continue;
2587		dev_dbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n",
2588			w->name, w->sname, stream, event);
2589		if (strstr(w->sname, stream)) {
2590			switch(event) {
2591			case SND_SOC_DAPM_STREAM_START:
2592				w->active = 1;
2593				break;
2594			case SND_SOC_DAPM_STREAM_STOP:
2595				w->active = 0;
2596				break;
2597			case SND_SOC_DAPM_STREAM_SUSPEND:
2598			case SND_SOC_DAPM_STREAM_RESUME:
2599			case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
2600			case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
 
 
 
 
 
 
 
2601				break;
2602			}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2603		}
2604	}
 
2605
2606	dapm_power_widgets(dapm, event);
 
 
 
 
 
 
 
 
 
2607}
2608
2609/**
2610 * snd_soc_dapm_stream_event - send a stream event to the dapm core
2611 * @rtd: PCM runtime data
2612 * @stream: stream name
2613 * @event: stream event
2614 *
2615 * Sends a stream event to the dapm core. The core then makes any
2616 * necessary widget power changes.
2617 *
2618 * Returns 0 for success else error.
2619 */
2620int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
2621	const char *stream, int event)
2622{
2623	struct snd_soc_codec *codec = rtd->codec;
2624
2625	if (stream == NULL)
2626		return 0;
 
 
2627
2628	mutex_lock(&codec->mutex);
2629	soc_dapm_stream_event(&codec->dapm, stream, event);
2630	mutex_unlock(&codec->mutex);
2631	return 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2632}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2633
2634/**
2635 * snd_soc_dapm_enable_pin - enable pin.
2636 * @dapm: DAPM context
2637 * @pin: pin name
2638 *
2639 * Enables input/output pin and its parents or children widgets iff there is
2640 * a valid audio route and active audio stream.
 
2641 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2642 * do any widget power switching.
2643 */
2644int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
2645{
2646	return snd_soc_dapm_set_pin(dapm, pin, 1);
 
 
 
 
 
 
 
 
2647}
2648EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
2649
2650/**
2651 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
2652 * @dapm: DAPM context
2653 * @pin: pin name
2654 *
2655 * Enables input/output pin regardless of any other state.  This is
2656 * intended for use with microphone bias supplies used in microphone
2657 * jack detection.
2658 *
 
 
2659 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2660 * do any widget power switching.
2661 */
2662int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
2663				  const char *pin)
2664{
2665	struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2666
2667	if (!w) {
2668		dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2669		return -EINVAL;
2670	}
2671
2672	dev_dbg(w->dapm->dev, "dapm: force enable pin %s\n", pin);
2673	w->connected = 1;
 
 
 
 
 
 
 
 
2674	w->force = 1;
 
2675
2676	return 0;
2677}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2678EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
2679
2680/**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2681 * snd_soc_dapm_disable_pin - disable pin.
2682 * @dapm: DAPM context
2683 * @pin: pin name
2684 *
2685 * Disables input/output pin and its parents or children widgets.
 
2686 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2687 * do any widget power switching.
2688 */
2689int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
2690			     const char *pin)
2691{
2692	return snd_soc_dapm_set_pin(dapm, pin, 0);
 
 
 
 
 
 
 
 
2693}
2694EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
2695
2696/**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2697 * snd_soc_dapm_nc_pin - permanently disable pin.
2698 * @dapm: DAPM context
2699 * @pin: pin name
2700 *
2701 * Marks the specified pin as being not connected, disabling it along
2702 * any parent or child widgets.  At present this is identical to
2703 * snd_soc_dapm_disable_pin() but in future it will be extended to do
2704 * additional things such as disabling controls which only affect
2705 * paths through the pin.
2706 *
2707 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
2708 * do any widget power switching.
2709 */
2710int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
2711{
2712	return snd_soc_dapm_set_pin(dapm, pin, 0);
 
 
 
 
 
 
 
 
2713}
2714EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
2715
2716/**
2717 * snd_soc_dapm_get_pin_status - get audio pin status
2718 * @dapm: DAPM context
2719 * @pin: audio signal pin endpoint (or start point)
2720 *
2721 * Get audio pin status - connected or disconnected.
2722 *
2723 * Returns 1 for connected otherwise 0.
2724 */
2725int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
2726				const char *pin)
2727{
2728	struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2729
2730	if (w)
2731		return w->connected;
2732
2733	return 0;
2734}
2735EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
2736
2737/**
2738 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
2739 * @dapm: DAPM context
2740 * @pin: audio signal pin endpoint (or start point)
2741 *
2742 * Mark the given endpoint or pin as ignoring suspend.  When the
2743 * system is disabled a path between two endpoints flagged as ignoring
2744 * suspend will not be disabled.  The path must already be enabled via
2745 * normal means at suspend time, it will not be turned on if it was not
2746 * already enabled.
2747 */
2748int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
2749				const char *pin)
2750{
2751	struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
2752
2753	if (!w) {
2754		dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
2755		return -EINVAL;
2756	}
2757
2758	w->ignore_suspend = 1;
2759
2760	return 0;
2761}
2762EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
2763
2764/**
2765 * snd_soc_dapm_free - free dapm resources
2766 * @dapm: DAPM context
2767 *
2768 * Free all dapm widgets and resources.
2769 */
2770void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
2771{
2772	snd_soc_dapm_sys_remove(dapm->dev);
2773	dapm_debugfs_cleanup(dapm);
2774	dapm_free_widgets(dapm);
2775	list_del(&dapm->list);
2776}
2777EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
2778
2779static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2780{
 
2781	struct snd_soc_dapm_widget *w;
2782	LIST_HEAD(down_list);
2783	int powerdown = 0;
2784
2785	list_for_each_entry(w, &dapm->card->widgets, list) {
 
 
2786		if (w->dapm != dapm)
2787			continue;
2788		if (w->power) {
2789			dapm_seq_insert(w, &down_list, false);
2790			w->power = 0;
2791			powerdown = 1;
2792		}
2793	}
2794
2795	/* If there were no widgets to power down we're already in
2796	 * standby.
2797	 */
2798	if (powerdown) {
2799		snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_PREPARE);
2800		dapm_seq_run(dapm, &down_list, 0, false);
2801		snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_STANDBY);
 
 
 
 
2802	}
 
 
2803}
2804
2805/*
2806 * snd_soc_dapm_shutdown - callback for system shutdown
2807 */
2808void snd_soc_dapm_shutdown(struct snd_soc_card *card)
2809{
2810	struct snd_soc_codec *codec;
2811
2812	list_for_each_entry(codec, &card->codec_dev_list, list) {
2813		soc_dapm_shutdown_codec(&codec->dapm);
2814		snd_soc_dapm_set_bias_level(&codec->dapm, SND_SOC_BIAS_OFF);
 
 
 
 
2815	}
 
 
 
 
 
2816}
2817
2818/* Module information */
2819MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
2820MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
2821MODULE_LICENSE("GPL");
v6.8
   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(card, 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(card, 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);
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);
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");