soc-jack.c 13.0 KB
Newer Older
M
Mark Brown 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * soc-jack.c  --  ALSA SoC jack handling
 *
 * Copyright 2008 Wolfson Microelectronics PLC.
 *
 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
 *
 *  This program is free software; you can redistribute  it and/or modify it
 *  under  the terms of  the GNU General  Public License as published by the
 *  Free Software Foundation;  either version 2 of the  License, or (at your
 *  option) any later version.
 */

#include <sound/jack.h>
#include <sound/soc.h>
16
#include <linux/gpio.h>
17
#include <linux/gpio/consumer.h>
18 19 20
#include <linux/interrupt.h>
#include <linux/workqueue.h>
#include <linux/delay.h>
21
#include <linux/export.h>
22
#include <linux/suspend.h>
23
#include <trace/events/asoc.h>
M
Mark Brown 已提交
24

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
/**
 * snd_soc_codec_set_jack - configure codec jack.
 * @codec: CODEC
 * @jack: structure to use for the jack
 * @data: can be used if codec driver need extra data for configuring jack
 *
 * Configures and enables jack detection function.
 */
int snd_soc_codec_set_jack(struct snd_soc_codec *codec,
	struct snd_soc_jack *jack, void *data)
{
	if (codec->driver->set_jack)
		return codec->driver->set_jack(codec, jack, data);
	else
		return -EINVAL;
}
EXPORT_SYMBOL_GPL(snd_soc_codec_set_jack);

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
/**
 * snd_soc_component_set_jack - configure component jack.
 * @component: COMPONENTs
 * @jack: structure to use for the jack
 * @data: can be used if codec driver need extra data for configuring jack
 *
 * Configures and enables jack detection function.
 */
int snd_soc_component_set_jack(struct snd_soc_component *component,
			       struct snd_soc_jack *jack, void *data)
{
	/* will be removed */
	if (component->set_jack)
		return component->set_jack(component, jack, data);

	if (component->driver->set_jack)
		return component->driver->set_jack(component, jack, data);

	return -ENOTSUPP;
}
EXPORT_SYMBOL_GPL(snd_soc_component_set_jack);

M
Mark Brown 已提交
65
/**
66 67
 * snd_soc_card_jack_new - Create a new jack
 * @card:  ASoC card
M
Mark Brown 已提交
68 69 70 71
 * @id:    an identifying string for this jack
 * @type:  a bitmask of enum snd_jack_type values that can be detected by
 *         this jack
 * @jack:  structure to use for the jack
72 73
 * @pins:  Array of jack pins to be added to the jack or NULL
 * @num_pins: Number of elements in the @pins array
M
Mark Brown 已提交
74 75 76 77 78 79
 *
 * Creates a new jack object.
 *
 * Returns zero if successful, or a negative error code on failure.
 * On success jack will be initialised.
 */
80 81 82
int snd_soc_card_jack_new(struct snd_soc_card *card, const char *id, int type,
	struct snd_soc_jack *jack, struct snd_soc_jack_pin *pins,
	unsigned int num_pins)
M
Mark Brown 已提交
83
{
84 85
	int ret;

86
	mutex_init(&jack->mutex);
87
	jack->card = card;
M
Mark Brown 已提交
88
	INIT_LIST_HEAD(&jack->pins);
89
	INIT_LIST_HEAD(&jack->jack_zones);
90
	BLOCKING_INIT_NOTIFIER_HEAD(&jack->notifier);
M
Mark Brown 已提交
91

92
	ret = snd_jack_new(card->snd_card, id, type, &jack->jack, false, false);
93 94 95 96 97 98 99
	if (ret)
		return ret;

	if (num_pins)
		return snd_soc_jack_add_pins(jack, num_pins, pins);

	return 0;
M
Mark Brown 已提交
100
}
101
EXPORT_SYMBOL_GPL(snd_soc_card_jack_new);
M
Mark Brown 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118

/**
 * snd_soc_jack_report - Report the current status for a jack
 *
 * @jack:   the jack
 * @status: a bitmask of enum snd_jack_type values that are currently detected.
 * @mask:   a bitmask of enum snd_jack_type values that being reported.
 *
 * If configured using snd_soc_jack_add_pins() then the associated
 * DAPM pins will be enabled or disabled as appropriate and DAPM
 * synchronised.
 *
 * Note: This function uses mutexes and should be called from a
 * context which can sleep (such as a workqueue).
 */
void snd_soc_jack_report(struct snd_soc_jack *jack, int status, int mask)
{
L
Liam Girdwood 已提交
119
	struct snd_soc_dapm_context *dapm;
M
Mark Brown 已提交
120
	struct snd_soc_jack_pin *pin;
121
	unsigned int sync = 0;
M
Mark Brown 已提交
122 123
	int enable;

124 125
	trace_snd_soc_jack_report(jack, mask, status);

M
Mark Brown 已提交
126
	if (!jack)
M
Mark Brown 已提交
127
		return;
M
Mark Brown 已提交
128

129
	dapm = &jack->card->dapm;
M
Mark Brown 已提交
130

131
	mutex_lock(&jack->mutex);
M
Mark Brown 已提交
132 133

	jack->status &= ~mask;
134
	jack->status |= status & mask;
M
Mark Brown 已提交
135

136 137
	trace_snd_soc_jack_notify(jack, status);

M
Mark Brown 已提交
138
	list_for_each_entry(pin, &jack->pins, list) {
139
		enable = pin->mask & jack->status;
M
Mark Brown 已提交
140 141 142 143 144

		if (pin->invert)
			enable = !enable;

		if (enable)
L
Liam Girdwood 已提交
145
			snd_soc_dapm_enable_pin(dapm, pin->pin);
M
Mark Brown 已提交
146
		else
L
Liam Girdwood 已提交
147
			snd_soc_dapm_disable_pin(dapm, pin->pin);
148 149 150

		/* we need to sync for this case only */
		sync = 1;
M
Mark Brown 已提交
151 152
	}

153
	/* Report before the DAPM sync to help users updating micbias status */
154
	blocking_notifier_call_chain(&jack->notifier, jack->status, jack);
155

156 157
	if (sync)
		snd_soc_dapm_sync(dapm);
M
Mark Brown 已提交
158

159
	snd_jack_report(jack->jack, jack->status);
M
Mark Brown 已提交
160

161
	mutex_unlock(&jack->mutex);
M
Mark Brown 已提交
162 163 164
}
EXPORT_SYMBOL_GPL(snd_soc_jack_report);

165 166 167 168 169
/**
 * snd_soc_jack_add_zones - Associate voltage zones with jack
 *
 * @jack:  ASoC jack
 * @count: Number of zones
170
 * @zones:  Array of zones
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
 *
 * After this function has been called the zones specified in the
 * array will be associated with the jack.
 */
int snd_soc_jack_add_zones(struct snd_soc_jack *jack, int count,
			  struct snd_soc_jack_zone *zones)
{
	int i;

	for (i = 0; i < count; i++) {
		INIT_LIST_HEAD(&zones[i].list);
		list_add(&(zones[i].list), &jack->jack_zones);
	}
	return 0;
}
EXPORT_SYMBOL_GPL(snd_soc_jack_add_zones);

/**
 * snd_soc_jack_get_type - Based on the mic bias value, this function returns
190
 * the type of jack from the zones declared in the jack type
191
 *
192
 * @jack:  ASoC jack
193 194 195
 * @micbias_voltage:  mic bias voltage at adc channel when jack is plugged in
 *
 * Based on the mic bias value passed, this function helps identify
196
 * the type of jack from the already declared jack zones
197 198 199 200 201 202 203 204 205 206 207 208 209 210
 */
int snd_soc_jack_get_type(struct snd_soc_jack *jack, int micbias_voltage)
{
	struct snd_soc_jack_zone *zone;

	list_for_each_entry(zone, &jack->jack_zones, list) {
		if (micbias_voltage >= zone->min_mv &&
			micbias_voltage < zone->max_mv)
				return zone->jack_type;
	}
	return 0;
}
EXPORT_SYMBOL_GPL(snd_soc_jack_get_type);

M
Mark Brown 已提交
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
/**
 * snd_soc_jack_add_pins - Associate DAPM pins with an ASoC jack
 *
 * @jack:  ASoC jack
 * @count: Number of pins
 * @pins:  Array of pins
 *
 * After this function has been called the DAPM pins specified in the
 * pins array will have their status updated to reflect the current
 * state of the jack whenever the jack status is updated.
 */
int snd_soc_jack_add_pins(struct snd_soc_jack *jack, int count,
			  struct snd_soc_jack_pin *pins)
{
	int i;

	for (i = 0; i < count; i++) {
		if (!pins[i].pin) {
229
			dev_err(jack->card->dev, "ASoC: No name for pin %d\n",
230
				i);
M
Mark Brown 已提交
231 232 233
			return -EINVAL;
		}
		if (!pins[i].mask) {
234
			dev_err(jack->card->dev, "ASoC: No mask for pin %d"
235
				" (%s)\n", i, pins[i].pin);
M
Mark Brown 已提交
236 237 238 239 240
			return -EINVAL;
		}

		INIT_LIST_HEAD(&pins[i].list);
		list_add(&(pins[i].list), &jack->pins);
241
		snd_jack_add_new_kctl(jack->jack, pins[i].pin, pins[i].mask);
M
Mark Brown 已提交
242 243 244 245 246 247 248 249 250 251 252
	}

	/* Update to reflect the last reported status; canned jack
	 * implementations are likely to set their state before the
	 * card has an opportunity to associate pins.
	 */
	snd_soc_jack_report(jack, 0, 0);

	return 0;
}
EXPORT_SYMBOL_GPL(snd_soc_jack_add_pins);
253

254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
/**
 * snd_soc_jack_notifier_register - Register a notifier for jack status
 *
 * @jack:  ASoC jack
 * @nb:    Notifier block to register
 *
 * Register for notification of the current status of the jack.  Note
 * that it is not possible to report additional jack events in the
 * callback from the notifier, this is intended to support
 * applications such as enabling electrical detection only when a
 * mechanical detection event has occurred.
 */
void snd_soc_jack_notifier_register(struct snd_soc_jack *jack,
				    struct notifier_block *nb)
{
	blocking_notifier_chain_register(&jack->notifier, nb);
}
EXPORT_SYMBOL_GPL(snd_soc_jack_notifier_register);

/**
 * snd_soc_jack_notifier_unregister - Unregister a notifier for jack status
 *
 * @jack:  ASoC jack
 * @nb:    Notifier block to unregister
 *
 * Stop notifying for status changes.
 */
void snd_soc_jack_notifier_unregister(struct snd_soc_jack *jack,
				      struct notifier_block *nb)
{
	blocking_notifier_chain_unregister(&jack->notifier, nb);
}
EXPORT_SYMBOL_GPL(snd_soc_jack_notifier_unregister);

288 289
#ifdef CONFIG_GPIOLIB
/* gpio detect */
290
static void snd_soc_jack_gpio_detect(struct snd_soc_jack_gpio *gpio)
291 292 293 294 295
{
	struct snd_soc_jack *jack = gpio->jack;
	int enable;
	int report;

296
	enable = gpiod_get_value_cansleep(gpio->desc);
297 298 299 300 301 302 303 304
	if (gpio->invert)
		enable = !enable;

	if (enable)
		report = gpio->report;
	else
		report = 0;

305
	if (gpio->jack_status_check)
306
		report = gpio->jack_status_check(gpio->data);
307

308 309 310 311 312 313 314
	snd_soc_jack_report(jack, report, gpio->report);
}

/* irq handler for gpio pin */
static irqreturn_t gpio_handler(int irq, void *data)
{
	struct snd_soc_jack_gpio *gpio = data;
315
	struct device *dev = gpio->jack->card->dev;
316

317 318
	trace_snd_soc_jack_irq(gpio->name);

319 320
	if (device_may_wakeup(dev))
		pm_wakeup_event(dev, gpio->debounce_time + 50);
321

322
	queue_delayed_work(system_power_efficient_wq, &gpio->work,
323
			      msecs_to_jiffies(gpio->debounce_time));
324 325 326 327 328 329 330 331 332

	return IRQ_HANDLED;
}

/* gpio work */
static void gpio_work(struct work_struct *work)
{
	struct snd_soc_jack_gpio *gpio;

333
	gpio = container_of(work, struct snd_soc_jack_gpio, work.work);
334 335 336
	snd_soc_jack_gpio_detect(gpio);
}

337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
static int snd_soc_jack_pm_notifier(struct notifier_block *nb,
				    unsigned long action, void *data)
{
	struct snd_soc_jack_gpio *gpio =
			container_of(nb, struct snd_soc_jack_gpio, pm_notifier);

	switch (action) {
	case PM_POST_SUSPEND:
	case PM_POST_HIBERNATION:
	case PM_POST_RESTORE:
		/*
		 * Use workqueue so we do not have to care about running
		 * concurrently with work triggered by the interrupt handler.
		 */
		queue_delayed_work(system_power_efficient_wq, &gpio->work, 0);
		break;
	}

	return NOTIFY_DONE;
}

358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
/**
 * snd_soc_jack_add_gpios - Associate GPIO pins with an ASoC jack
 *
 * @jack:  ASoC jack
 * @count: number of pins
 * @gpios: array of gpio pins
 *
 * This function will request gpio, set data direction and request irq
 * for each gpio in the array.
 */
int snd_soc_jack_add_gpios(struct snd_soc_jack *jack, int count,
			struct snd_soc_jack_gpio *gpios)
{
	int i, ret;

	for (i = 0; i < count; i++) {
		if (!gpios[i].name) {
375
			dev_err(jack->card->dev,
376
				"ASoC: No name for gpio at index %d\n", i);
377 378 379 380
			ret = -EINVAL;
			goto undo;
		}

381 382 383 384 385
		if (gpios[i].desc) {
			/* Already have a GPIO descriptor. */
			goto got_gpio;
		} else if (gpios[i].gpiod_dev) {
			/* Get a GPIO descriptor */
386 387
			gpios[i].desc = gpiod_get_index(gpios[i].gpiod_dev,
							gpios[i].name,
388
							gpios[i].idx, GPIOD_IN);
389 390 391 392 393 394 395 396 397 398
			if (IS_ERR(gpios[i].desc)) {
				ret = PTR_ERR(gpios[i].desc);
				dev_err(gpios[i].gpiod_dev,
					"ASoC: Cannot get gpio at index %d: %d",
					i, ret);
				goto undo;
			}
		} else {
			/* legacy GPIO number */
			if (!gpio_is_valid(gpios[i].gpio)) {
399
				dev_err(jack->card->dev,
400 401 402 403 404 405
					"ASoC: Invalid gpio %d\n",
					gpios[i].gpio);
				ret = -EINVAL;
				goto undo;
			}

406 407
			ret = gpio_request_one(gpios[i].gpio, GPIOF_IN,
					       gpios[i].name);
408 409 410 411 412
			if (ret)
				goto undo;

			gpios[i].desc = gpio_to_desc(gpios[i].gpio);
		}
413
got_gpio:
414
		INIT_DELAYED_WORK(&gpios[i].work, gpio_work);
415 416
		gpios[i].jack = jack;

417
		ret = request_any_context_irq(gpiod_to_irq(gpios[i].desc),
418 419 420
					      gpio_handler,
					      IRQF_TRIGGER_RISING |
					      IRQF_TRIGGER_FALLING,
421
					      gpios[i].name,
422
					      &gpios[i]);
423
		if (ret < 0)
424 425
			goto err;

426
		if (gpios[i].wake) {
427
			ret = irq_set_irq_wake(gpiod_to_irq(gpios[i].desc), 1);
428
			if (ret != 0)
429
				dev_err(jack->card->dev,
430 431
					"ASoC: Failed to mark GPIO at index %d as wake source: %d\n",
					i, ret);
432 433
		}

434 435 436 437 438 439 440
		/*
		 * Register PM notifier so we do not miss state transitions
		 * happening while system is asleep.
		 */
		gpios[i].pm_notifier.notifier_call = snd_soc_jack_pm_notifier;
		register_pm_notifier(&gpios[i].pm_notifier);

441
		/* Expose GPIO value over sysfs for diagnostic purposes */
442
		gpiod_export(gpios[i].desc, false);
443 444

		/* Update initial jack status */
445 446
		schedule_delayed_work(&gpios[i].work,
				      msecs_to_jiffies(gpios[i].debounce_time));
447 448 449 450 451 452 453 454 455 456 457 458 459
	}

	return 0;

err:
	gpio_free(gpios[i].gpio);
undo:
	snd_soc_jack_free_gpios(jack, i, gpios);

	return ret;
}
EXPORT_SYMBOL_GPL(snd_soc_jack_add_gpios);

460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
/**
 * snd_soc_jack_add_gpiods - Associate GPIO descriptor pins with an ASoC jack
 *
 * @gpiod_dev: GPIO consumer device
 * @jack:      ASoC jack
 * @count:     number of pins
 * @gpios:     array of gpio pins
 *
 * This function will request gpio, set data direction and request irq
 * for each gpio in the array.
 */
int snd_soc_jack_add_gpiods(struct device *gpiod_dev,
			    struct snd_soc_jack *jack,
			    int count, struct snd_soc_jack_gpio *gpios)
{
	int i;

	for (i = 0; i < count; i++)
		gpios[i].gpiod_dev = gpiod_dev;

	return snd_soc_jack_add_gpios(jack, count, gpios);
}
EXPORT_SYMBOL_GPL(snd_soc_jack_add_gpiods);

484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
/**
 * snd_soc_jack_free_gpios - Release GPIO pins' resources of an ASoC jack
 *
 * @jack:  ASoC jack
 * @count: number of pins
 * @gpios: array of gpio pins
 *
 * Release gpio and irq resources for gpio pins associated with an ASoC jack.
 */
void snd_soc_jack_free_gpios(struct snd_soc_jack *jack, int count,
			struct snd_soc_jack_gpio *gpios)
{
	int i;

	for (i = 0; i < count; i++) {
499
		gpiod_unexport(gpios[i].desc);
500
		unregister_pm_notifier(&gpios[i].pm_notifier);
501
		free_irq(gpiod_to_irq(gpios[i].desc), &gpios[i]);
502
		cancel_delayed_work_sync(&gpios[i].work);
503
		gpiod_put(gpios[i].desc);
504 505 506 507 508
		gpios[i].jack = NULL;
	}
}
EXPORT_SYMBOL_GPL(snd_soc_jack_free_gpios);
#endif	/* CONFIG_GPIOLIB */