| 
									
										
										
										
											2009-01-07 17:31:10 +00:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * 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>
 | 
					
						
							| 
									
										
										
										
											2009-03-03 15:25:04 -06:00
										 |  |  | #include <linux/gpio.h>
 | 
					
						
							|  |  |  | #include <linux/interrupt.h>
 | 
					
						
							|  |  |  | #include <linux/workqueue.h>
 | 
					
						
							|  |  |  | #include <linux/delay.h>
 | 
					
						
							| 
									
										
										
										
											2011-09-22 09:34:58 -04:00
										 |  |  | #include <linux/export.h>
 | 
					
						
							| 
									
										
										
										
											2010-12-05 12:22:46 +00:00
										 |  |  | #include <trace/events/asoc.h>
 | 
					
						
							| 
									
										
										
										
											2009-01-07 17:31:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * snd_soc_jack_new - Create a new jack | 
					
						
							| 
									
										
										
										
											2012-10-02 14:08:19 +02:00
										 |  |  |  * @codec: ASoC codec | 
					
						
							| 
									
										
										
										
											2009-01-07 17:31:10 +00:00
										 |  |  |  * @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 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Creates a new jack object. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Returns zero if successful, or a negative error code on failure. | 
					
						
							|  |  |  |  * On success jack will be initialised. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2010-03-17 20:15:21 +00:00
										 |  |  | int snd_soc_jack_new(struct snd_soc_codec *codec, const char *id, int type, | 
					
						
							| 
									
										
										
										
											2009-01-07 17:31:10 +00:00
										 |  |  | 		     struct snd_soc_jack *jack) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2012-03-12 14:07:49 +00:00
										 |  |  | 	mutex_init(&jack->mutex); | 
					
						
							| 
									
										
										
										
											2010-03-17 20:15:21 +00:00
										 |  |  | 	jack->codec = codec; | 
					
						
							| 
									
										
										
										
											2009-01-07 17:31:10 +00:00
										 |  |  | 	INIT_LIST_HEAD(&jack->pins); | 
					
						
							| 
									
										
										
										
											2011-02-09 14:44:17 +05:30
										 |  |  | 	INIT_LIST_HEAD(&jack->jack_zones); | 
					
						
							| 
									
										
										
										
											2010-03-22 12:06:30 +00:00
										 |  |  | 	BLOCKING_INIT_NOTIFIER_HEAD(&jack->notifier); | 
					
						
							| 
									
										
										
										
											2009-01-07 17:31:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-17 20:15:21 +00:00
										 |  |  | 	return snd_jack_new(codec->card->snd_card, id, type, &jack->jack); | 
					
						
							| 
									
										
										
										
											2009-01-07 17:31:10 +00:00
										 |  |  | } | 
					
						
							|  |  |  | EXPORT_SYMBOL_GPL(snd_soc_jack_new); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * 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) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2009-10-17 08:32:56 +02:00
										 |  |  | 	struct snd_soc_codec *codec; | 
					
						
							| 
									
										
										
										
											2010-11-05 15:53:46 +02:00
										 |  |  | 	struct snd_soc_dapm_context *dapm; | 
					
						
							| 
									
										
										
										
											2009-01-07 17:31:10 +00:00
										 |  |  | 	struct snd_soc_jack_pin *pin; | 
					
						
							|  |  |  | 	int enable; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-05 12:22:46 +00:00
										 |  |  | 	trace_snd_soc_jack_report(jack, mask, status); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-29 20:31:14 +01:00
										 |  |  | 	if (!jack) | 
					
						
							| 
									
										
										
										
											2009-01-07 17:31:10 +00:00
										 |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2010-03-29 20:31:14 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-17 20:15:21 +00:00
										 |  |  | 	codec = jack->codec; | 
					
						
							| 
									
										
										
										
											2010-11-05 15:53:46 +02:00
										 |  |  | 	dapm =  &codec->dapm; | 
					
						
							| 
									
										
										
										
											2009-01-07 17:31:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-12 14:07:49 +00:00
										 |  |  | 	mutex_lock(&jack->mutex); | 
					
						
							| 
									
										
										
										
											2009-01-07 17:31:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	jack->status &= ~mask; | 
					
						
							| 
									
										
										
										
											2009-07-24 02:48:57 +02:00
										 |  |  | 	jack->status |= status & mask; | 
					
						
							| 
									
										
										
										
											2009-01-07 17:31:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-05 12:22:46 +00:00
										 |  |  | 	trace_snd_soc_jack_notify(jack, status); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-07 17:31:10 +00:00
										 |  |  | 	list_for_each_entry(pin, &jack->pins, list) { | 
					
						
							| 
									
										
										
										
											2009-07-24 02:48:57 +02:00
										 |  |  | 		enable = pin->mask & jack->status; | 
					
						
							| 
									
										
										
										
											2009-01-07 17:31:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if (pin->invert) | 
					
						
							|  |  |  | 			enable = !enable; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (enable) | 
					
						
							| 
									
										
										
										
											2010-11-05 15:53:46 +02:00
										 |  |  | 			snd_soc_dapm_enable_pin(dapm, pin->pin); | 
					
						
							| 
									
										
										
										
											2009-01-07 17:31:10 +00:00
										 |  |  | 		else | 
					
						
							| 
									
										
										
										
											2010-11-05 15:53:46 +02:00
										 |  |  | 			snd_soc_dapm_disable_pin(dapm, pin->pin); | 
					
						
							| 
									
										
										
										
											2009-01-07 17:31:10 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-22 12:06:30 +00:00
										 |  |  | 	/* Report before the DAPM sync to help users updating micbias status */ | 
					
						
							| 
									
										
										
										
											2012-08-13 16:28:36 +01:00
										 |  |  | 	blocking_notifier_call_chain(&jack->notifier, jack->status, jack); | 
					
						
							| 
									
										
										
										
											2010-03-22 12:06:30 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-11-05 15:53:46 +02:00
										 |  |  | 	snd_soc_dapm_sync(dapm); | 
					
						
							| 
									
										
										
										
											2009-01-07 17:31:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-04 08:18:18 -07:00
										 |  |  | 	snd_jack_report(jack->jack, jack->status); | 
					
						
							| 
									
										
										
										
											2009-01-07 17:31:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-12 14:07:49 +00:00
										 |  |  | 	mutex_unlock(&jack->mutex); | 
					
						
							| 
									
										
										
										
											2009-01-07 17:31:10 +00:00
										 |  |  | } | 
					
						
							|  |  |  | EXPORT_SYMBOL_GPL(snd_soc_jack_report); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-09 14:44:17 +05:30
										 |  |  | /**
 | 
					
						
							|  |  |  |  * snd_soc_jack_add_zones - Associate voltage zones with jack | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @jack:  ASoC jack | 
					
						
							|  |  |  |  * @count: Number of zones | 
					
						
							|  |  |  |  * @zone:  Array of zones | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 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 | 
					
						
							| 
									
										
										
										
											2012-10-02 14:08:19 +02:00
										 |  |  |  * the type of jack from the zones declared in the jack type | 
					
						
							| 
									
										
										
										
											2011-02-09 14:44:17 +05:30
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2012-10-02 14:08:19 +02:00
										 |  |  |  * @jack:  ASoC jack | 
					
						
							| 
									
										
										
										
											2011-02-09 14:44:17 +05:30
										 |  |  |  * @micbias_voltage:  mic bias voltage at adc channel when jack is plugged in | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Based on the mic bias value passed, this function helps identify | 
					
						
							| 
									
										
										
										
											2012-10-02 14:08:19 +02:00
										 |  |  |  * the type of jack from the already declared jack zones | 
					
						
							| 
									
										
										
										
											2011-02-09 14:44:17 +05:30
										 |  |  |  */ | 
					
						
							|  |  |  | 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); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-07 17:31:10 +00:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * 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) { | 
					
						
							| 
									
										
										
										
											2012-11-19 14:39:14 +00:00
										 |  |  | 			dev_err(jack->codec->dev, "ASoC: No name for pin %d\n", | 
					
						
							|  |  |  | 				i); | 
					
						
							| 
									
										
										
										
											2009-01-07 17:31:10 +00:00
										 |  |  | 			return -EINVAL; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if (!pins[i].mask) { | 
					
						
							| 
									
										
										
										
											2012-11-19 14:39:14 +00:00
										 |  |  | 			dev_err(jack->codec->dev, "ASoC: No mask for pin %d" | 
					
						
							|  |  |  | 				" (%s)\n", i, pins[i].pin); | 
					
						
							| 
									
										
										
										
											2009-01-07 17:31:10 +00:00
										 |  |  | 			return -EINVAL; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		INIT_LIST_HEAD(&pins[i].list); | 
					
						
							|  |  |  | 		list_add(&(pins[i].list), &jack->pins); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* 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); | 
					
						
							| 
									
										
										
										
											2009-03-03 15:25:04 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-22 12:06:30 +00:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * 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); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-03 15:25:04 -06:00
										 |  |  | #ifdef CONFIG_GPIOLIB
 | 
					
						
							|  |  |  | /* gpio detect */ | 
					
						
							| 
									
										
										
										
											2009-03-06 11:32:17 +00:00
										 |  |  | static void snd_soc_jack_gpio_detect(struct snd_soc_jack_gpio *gpio) | 
					
						
							| 
									
										
										
										
											2009-03-03 15:25:04 -06:00
										 |  |  | { | 
					
						
							|  |  |  | 	struct snd_soc_jack *jack = gpio->jack; | 
					
						
							|  |  |  | 	int enable; | 
					
						
							|  |  |  | 	int report; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 17:22:23 +02:00
										 |  |  | 	enable = gpio_get_value_cansleep(gpio->gpio); | 
					
						
							| 
									
										
										
										
											2009-03-03 15:25:04 -06:00
										 |  |  | 	if (gpio->invert) | 
					
						
							|  |  |  | 		enable = !enable; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (enable) | 
					
						
							|  |  |  | 		report = gpio->report; | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		report = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-12 17:14:04 +09:00
										 |  |  | 	if (gpio->jack_status_check) | 
					
						
							|  |  |  | 		report = gpio->jack_status_check(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-03 15:25:04 -06:00
										 |  |  | 	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; | 
					
						
							| 
									
										
										
										
											2010-11-14 19:25:09 +00:00
										 |  |  | 	struct device *dev = gpio->jack->codec->card->dev; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-05 12:22:46 +00:00
										 |  |  | 	trace_snd_soc_jack_irq(gpio->name); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-11-14 19:25:09 +00:00
										 |  |  | 	if (device_may_wakeup(dev)) | 
					
						
							|  |  |  | 		pm_wakeup_event(dev, gpio->debounce_time + 50); | 
					
						
							| 
									
										
										
										
											2009-03-03 15:25:04 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-18 22:47:10 +01:00
										 |  |  | 	queue_delayed_work(system_power_efficient_wq, &gpio->work, | 
					
						
							| 
									
										
										
										
											2010-10-06 15:54:28 -07:00
										 |  |  | 			      msecs_to_jiffies(gpio->debounce_time)); | 
					
						
							| 
									
										
										
										
											2009-03-03 15:25:04 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return IRQ_HANDLED; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* gpio work */ | 
					
						
							|  |  |  | static void gpio_work(struct work_struct *work) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct snd_soc_jack_gpio *gpio; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-06 15:54:28 -07:00
										 |  |  | 	gpio = container_of(work, struct snd_soc_jack_gpio, work.work); | 
					
						
							| 
									
										
										
										
											2009-03-03 15:25:04 -06:00
										 |  |  | 	snd_soc_jack_gpio_detect(gpio); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * 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 (!gpio_is_valid(gpios[i].gpio)) { | 
					
						
							| 
									
										
										
										
											2012-11-19 14:39:14 +00:00
										 |  |  | 			dev_err(jack->codec->dev, "ASoC: Invalid gpio %d\n", | 
					
						
							| 
									
										
										
										
											2009-03-03 15:25:04 -06:00
										 |  |  | 				gpios[i].gpio); | 
					
						
							|  |  |  | 			ret = -EINVAL; | 
					
						
							|  |  |  | 			goto undo; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if (!gpios[i].name) { | 
					
						
							| 
									
										
										
										
											2012-11-19 14:39:14 +00:00
										 |  |  | 			dev_err(jack->codec->dev, "ASoC: No name for gpio %d\n", | 
					
						
							| 
									
										
										
										
											2009-03-03 15:25:04 -06:00
										 |  |  | 				gpios[i].gpio); | 
					
						
							|  |  |  | 			ret = -EINVAL; | 
					
						
							|  |  |  | 			goto undo; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		ret = gpio_request(gpios[i].gpio, gpios[i].name); | 
					
						
							|  |  |  | 		if (ret) | 
					
						
							|  |  |  | 			goto undo; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		ret = gpio_direction_input(gpios[i].gpio); | 
					
						
							|  |  |  | 		if (ret) | 
					
						
							|  |  |  | 			goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-06 15:54:28 -07:00
										 |  |  | 		INIT_DELAYED_WORK(&gpios[i].work, gpio_work); | 
					
						
							| 
									
										
										
										
											2009-07-31 21:15:25 +02:00
										 |  |  | 		gpios[i].jack = jack; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-11-03 09:35:31 -04:00
										 |  |  | 		ret = request_any_context_irq(gpio_to_irq(gpios[i].gpio), | 
					
						
							|  |  |  | 					      gpio_handler, | 
					
						
							|  |  |  | 					      IRQF_TRIGGER_RISING | | 
					
						
							|  |  |  | 					      IRQF_TRIGGER_FALLING, | 
					
						
							| 
									
										
										
										
											2011-04-01 14:50:43 -06:00
										 |  |  | 					      gpios[i].name, | 
					
						
							| 
									
										
										
										
											2010-11-03 09:35:31 -04:00
										 |  |  | 					      &gpios[i]); | 
					
						
							| 
									
										
										
										
											2011-08-13 19:15:01 +08:00
										 |  |  | 		if (ret < 0) | 
					
						
							| 
									
										
										
										
											2009-03-03 15:25:04 -06:00
										 |  |  | 			goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-17 16:35:55 -08:00
										 |  |  | 		if (gpios[i].wake) { | 
					
						
							| 
									
										
										
										
											2011-03-30 00:39:44 +02:00
										 |  |  | 			ret = irq_set_irq_wake(gpio_to_irq(gpios[i].gpio), 1); | 
					
						
							| 
									
										
										
										
											2011-02-17 16:35:55 -08:00
										 |  |  | 			if (ret != 0) | 
					
						
							| 
									
										
										
										
											2012-11-19 14:39:14 +00:00
										 |  |  | 				dev_err(jack->codec->dev, "ASoC: " | 
					
						
							| 
									
										
										
										
											2011-02-17 16:35:55 -08:00
										 |  |  | 				  "Failed to mark GPIO %d as wake source: %d\n", | 
					
						
							|  |  |  | 					gpios[i].gpio, ret); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-24 02:48:57 +02:00
										 |  |  | 		/* Expose GPIO value over sysfs for diagnostic purposes */ | 
					
						
							|  |  |  | 		gpio_export(gpios[i].gpio, false); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/* Update initial jack status */ | 
					
						
							|  |  |  | 		snd_soc_jack_gpio_detect(&gpios[i]); | 
					
						
							| 
									
										
										
										
											2009-03-03 15:25:04 -06:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	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); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * 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++) { | 
					
						
							| 
									
										
										
										
											2009-07-24 02:48:57 +02:00
										 |  |  | 		gpio_unexport(gpios[i].gpio); | 
					
						
							| 
									
										
										
										
											2009-03-03 15:25:04 -06:00
										 |  |  | 		free_irq(gpio_to_irq(gpios[i].gpio), &gpios[i]); | 
					
						
							| 
									
										
										
										
											2010-10-06 15:54:28 -07:00
										 |  |  | 		cancel_delayed_work_sync(&gpios[i].work); | 
					
						
							| 
									
										
										
										
											2009-03-03 15:25:04 -06:00
										 |  |  | 		gpio_free(gpios[i].gpio); | 
					
						
							|  |  |  | 		gpios[i].jack = NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | EXPORT_SYMBOL_GPL(snd_soc_jack_free_gpios); | 
					
						
							|  |  |  | #endif	/* CONFIG_GPIOLIB */
 |