Mark Brown | 8a2cd61 | 2009-01-07 17:31:10 +0000 | [diff] [blame] | 1 | /* |
| 2 | * soc-jack.c -- ALSA SoC jack handling |
| 3 | * |
| 4 | * Copyright 2008 Wolfson Microelectronics PLC. |
| 5 | * |
| 6 | * Author: Mark Brown <broonie@opensource.wolfsonmicro.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License as published by the |
| 10 | * Free Software Foundation; either version 2 of the License, or (at your |
| 11 | * option) any later version. |
| 12 | */ |
| 13 | |
| 14 | #include <sound/jack.h> |
| 15 | #include <sound/soc.h> |
Lopez Cruz, Misael | ec67624 | 2009-03-03 15:25:04 -0600 | [diff] [blame] | 16 | #include <linux/gpio.h> |
Jarkko Nikula | 50dfb69 | 2014-05-26 14:34:36 +0300 | [diff] [blame] | 17 | #include <linux/gpio/consumer.h> |
Lopez Cruz, Misael | ec67624 | 2009-03-03 15:25:04 -0600 | [diff] [blame] | 18 | #include <linux/interrupt.h> |
| 19 | #include <linux/workqueue.h> |
| 20 | #include <linux/delay.h> |
Paul Gortmaker | d81a6d7 | 2011-09-22 09:34:58 -0400 | [diff] [blame] | 21 | #include <linux/export.h> |
Dmitry Torokhov | 73548dd | 2017-03-21 16:50:43 -0700 | [diff] [blame] | 22 | #include <linux/suspend.h> |
Mark Brown | 3028eb8 | 2010-12-05 12:22:46 +0000 | [diff] [blame] | 23 | #include <trace/events/asoc.h> |
Mark Brown | 8a2cd61 | 2009-01-07 17:31:10 +0000 | [diff] [blame] | 24 | |
| 25 | /** |
Bard Liao | d734401 | 2017-04-07 11:26:41 +0800 | [diff] [blame] | 26 | * snd_soc_codec_set_jack - configure codec jack. |
| 27 | * @codec: CODEC |
| 28 | * @jack: structure to use for the jack |
| 29 | * @data: can be used if codec driver need extra data for configuring jack |
| 30 | * |
| 31 | * Configures and enables jack detection function. |
| 32 | */ |
| 33 | int snd_soc_codec_set_jack(struct snd_soc_codec *codec, |
| 34 | struct snd_soc_jack *jack, void *data) |
| 35 | { |
| 36 | if (codec->driver->set_jack) |
| 37 | return codec->driver->set_jack(codec, jack, data); |
| 38 | else |
| 39 | return -EINVAL; |
| 40 | } |
| 41 | EXPORT_SYMBOL_GPL(snd_soc_codec_set_jack); |
| 42 | |
| 43 | /** |
Lars-Peter Clausen | 9709399 | 2015-03-04 10:33:17 +0100 | [diff] [blame] | 44 | * snd_soc_card_jack_new - Create a new jack |
| 45 | * @card: ASoC card |
Mark Brown | 8a2cd61 | 2009-01-07 17:31:10 +0000 | [diff] [blame] | 46 | * @id: an identifying string for this jack |
| 47 | * @type: a bitmask of enum snd_jack_type values that can be detected by |
| 48 | * this jack |
| 49 | * @jack: structure to use for the jack |
Lars-Peter Clausen | 9709399 | 2015-03-04 10:33:17 +0100 | [diff] [blame] | 50 | * @pins: Array of jack pins to be added to the jack or NULL |
| 51 | * @num_pins: Number of elements in the @pins array |
Mark Brown | 8a2cd61 | 2009-01-07 17:31:10 +0000 | [diff] [blame] | 52 | * |
| 53 | * Creates a new jack object. |
| 54 | * |
| 55 | * Returns zero if successful, or a negative error code on failure. |
| 56 | * On success jack will be initialised. |
| 57 | */ |
Lars-Peter Clausen | 9709399 | 2015-03-04 10:33:17 +0100 | [diff] [blame] | 58 | int snd_soc_card_jack_new(struct snd_soc_card *card, const char *id, int type, |
| 59 | struct snd_soc_jack *jack, struct snd_soc_jack_pin *pins, |
| 60 | unsigned int num_pins) |
Mark Brown | 8a2cd61 | 2009-01-07 17:31:10 +0000 | [diff] [blame] | 61 | { |
Lars-Peter Clausen | 9709399 | 2015-03-04 10:33:17 +0100 | [diff] [blame] | 62 | int ret; |
| 63 | |
Mark Brown | 2667b4b | 2012-03-12 14:07:49 +0000 | [diff] [blame] | 64 | mutex_init(&jack->mutex); |
Lars-Peter Clausen | 9709399 | 2015-03-04 10:33:17 +0100 | [diff] [blame] | 65 | jack->card = card; |
Mark Brown | 8a2cd61 | 2009-01-07 17:31:10 +0000 | [diff] [blame] | 66 | INIT_LIST_HEAD(&jack->pins); |
Vinod Koul | fa9879e | 2011-02-09 14:44:17 +0530 | [diff] [blame] | 67 | INIT_LIST_HEAD(&jack->jack_zones); |
Mark Brown | d5021ec | 2010-03-22 12:06:30 +0000 | [diff] [blame] | 68 | BLOCKING_INIT_NOTIFIER_HEAD(&jack->notifier); |
Mark Brown | 8a2cd61 | 2009-01-07 17:31:10 +0000 | [diff] [blame] | 69 | |
Jie Yang | 4e3f0dc | 2015-04-27 21:20:58 +0800 | [diff] [blame] | 70 | ret = snd_jack_new(card->snd_card, id, type, &jack->jack, false, false); |
Lars-Peter Clausen | 9709399 | 2015-03-04 10:33:17 +0100 | [diff] [blame] | 71 | if (ret) |
| 72 | return ret; |
| 73 | |
| 74 | if (num_pins) |
| 75 | return snd_soc_jack_add_pins(jack, num_pins, pins); |
| 76 | |
| 77 | return 0; |
Mark Brown | 8a2cd61 | 2009-01-07 17:31:10 +0000 | [diff] [blame] | 78 | } |
Lars-Peter Clausen | 9709399 | 2015-03-04 10:33:17 +0100 | [diff] [blame] | 79 | EXPORT_SYMBOL_GPL(snd_soc_card_jack_new); |
Mark Brown | 8a2cd61 | 2009-01-07 17:31:10 +0000 | [diff] [blame] | 80 | |
| 81 | /** |
| 82 | * snd_soc_jack_report - Report the current status for a jack |
| 83 | * |
| 84 | * @jack: the jack |
| 85 | * @status: a bitmask of enum snd_jack_type values that are currently detected. |
| 86 | * @mask: a bitmask of enum snd_jack_type values that being reported. |
| 87 | * |
| 88 | * If configured using snd_soc_jack_add_pins() then the associated |
| 89 | * DAPM pins will be enabled or disabled as appropriate and DAPM |
| 90 | * synchronised. |
| 91 | * |
| 92 | * Note: This function uses mutexes and should be called from a |
| 93 | * context which can sleep (such as a workqueue). |
| 94 | */ |
| 95 | void snd_soc_jack_report(struct snd_soc_jack *jack, int status, int mask) |
| 96 | { |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 97 | struct snd_soc_dapm_context *dapm; |
Mark Brown | 8a2cd61 | 2009-01-07 17:31:10 +0000 | [diff] [blame] | 98 | struct snd_soc_jack_pin *pin; |
Vinod Koul | 30a765d | 2013-10-21 19:07:34 +0530 | [diff] [blame] | 99 | unsigned int sync = 0; |
Mark Brown | 8a2cd61 | 2009-01-07 17:31:10 +0000 | [diff] [blame] | 100 | int enable; |
Mark Brown | 8a2cd61 | 2009-01-07 17:31:10 +0000 | [diff] [blame] | 101 | |
Mark Brown | 3028eb8 | 2010-12-05 12:22:46 +0000 | [diff] [blame] | 102 | trace_snd_soc_jack_report(jack, mask, status); |
| 103 | |
Mark Brown | 3a278a0 | 2010-03-29 20:31:14 +0100 | [diff] [blame] | 104 | if (!jack) |
Mark Brown | 8a2cd61 | 2009-01-07 17:31:10 +0000 | [diff] [blame] | 105 | return; |
Mark Brown | 3a278a0 | 2010-03-29 20:31:14 +0100 | [diff] [blame] | 106 | |
Lars-Peter Clausen | 9709399 | 2015-03-04 10:33:17 +0100 | [diff] [blame] | 107 | dapm = &jack->card->dapm; |
Mark Brown | 8a2cd61 | 2009-01-07 17:31:10 +0000 | [diff] [blame] | 108 | |
Mark Brown | 2667b4b | 2012-03-12 14:07:49 +0000 | [diff] [blame] | 109 | mutex_lock(&jack->mutex); |
Mark Brown | 8a2cd61 | 2009-01-07 17:31:10 +0000 | [diff] [blame] | 110 | |
Mark Brown | 8a2cd61 | 2009-01-07 17:31:10 +0000 | [diff] [blame] | 111 | jack->status &= ~mask; |
Janusz Krzysztofik | 178b699 | 2009-07-24 02:48:57 +0200 | [diff] [blame] | 112 | jack->status |= status & mask; |
Mark Brown | 8a2cd61 | 2009-01-07 17:31:10 +0000 | [diff] [blame] | 113 | |
Mark Brown | 3028eb8 | 2010-12-05 12:22:46 +0000 | [diff] [blame] | 114 | trace_snd_soc_jack_notify(jack, status); |
| 115 | |
Mark Brown | 8a2cd61 | 2009-01-07 17:31:10 +0000 | [diff] [blame] | 116 | list_for_each_entry(pin, &jack->pins, list) { |
Janusz Krzysztofik | 178b699 | 2009-07-24 02:48:57 +0200 | [diff] [blame] | 117 | enable = pin->mask & jack->status; |
Mark Brown | 8a2cd61 | 2009-01-07 17:31:10 +0000 | [diff] [blame] | 118 | |
| 119 | if (pin->invert) |
| 120 | enable = !enable; |
| 121 | |
| 122 | if (enable) |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 123 | snd_soc_dapm_enable_pin(dapm, pin->pin); |
Mark Brown | 8a2cd61 | 2009-01-07 17:31:10 +0000 | [diff] [blame] | 124 | else |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame] | 125 | snd_soc_dapm_disable_pin(dapm, pin->pin); |
Vinod Koul | 30a765d | 2013-10-21 19:07:34 +0530 | [diff] [blame] | 126 | |
| 127 | /* we need to sync for this case only */ |
| 128 | sync = 1; |
Mark Brown | 8a2cd61 | 2009-01-07 17:31:10 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Mark Brown | d5021ec | 2010-03-22 12:06:30 +0000 | [diff] [blame] | 131 | /* Report before the DAPM sync to help users updating micbias status */ |
Mark Brown | 12022a7 | 2012-08-13 16:28:36 +0100 | [diff] [blame] | 132 | blocking_notifier_call_chain(&jack->notifier, jack->status, jack); |
Mark Brown | d5021ec | 2010-03-22 12:06:30 +0000 | [diff] [blame] | 133 | |
Vinod Koul | 30a765d | 2013-10-21 19:07:34 +0530 | [diff] [blame] | 134 | if (sync) |
| 135 | snd_soc_dapm_sync(dapm); |
Mark Brown | 8a2cd61 | 2009-01-07 17:31:10 +0000 | [diff] [blame] | 136 | |
Mark Brown | 747da0f | 2011-09-04 08:18:18 -0700 | [diff] [blame] | 137 | snd_jack_report(jack->jack, jack->status); |
Mark Brown | 8a2cd61 | 2009-01-07 17:31:10 +0000 | [diff] [blame] | 138 | |
Mark Brown | 2667b4b | 2012-03-12 14:07:49 +0000 | [diff] [blame] | 139 | mutex_unlock(&jack->mutex); |
Mark Brown | 8a2cd61 | 2009-01-07 17:31:10 +0000 | [diff] [blame] | 140 | } |
| 141 | EXPORT_SYMBOL_GPL(snd_soc_jack_report); |
| 142 | |
| 143 | /** |
Vinod Koul | fa9879e | 2011-02-09 14:44:17 +0530 | [diff] [blame] | 144 | * snd_soc_jack_add_zones - Associate voltage zones with jack |
| 145 | * |
| 146 | * @jack: ASoC jack |
| 147 | * @count: Number of zones |
Masanari Iida | bb29a93 | 2014-11-12 00:52:23 +0900 | [diff] [blame] | 148 | * @zones: Array of zones |
Vinod Koul | fa9879e | 2011-02-09 14:44:17 +0530 | [diff] [blame] | 149 | * |
| 150 | * After this function has been called the zones specified in the |
| 151 | * array will be associated with the jack. |
| 152 | */ |
| 153 | int snd_soc_jack_add_zones(struct snd_soc_jack *jack, int count, |
| 154 | struct snd_soc_jack_zone *zones) |
| 155 | { |
| 156 | int i; |
| 157 | |
| 158 | for (i = 0; i < count; i++) { |
| 159 | INIT_LIST_HEAD(&zones[i].list); |
| 160 | list_add(&(zones[i].list), &jack->jack_zones); |
| 161 | } |
| 162 | return 0; |
| 163 | } |
| 164 | EXPORT_SYMBOL_GPL(snd_soc_jack_add_zones); |
| 165 | |
| 166 | /** |
| 167 | * snd_soc_jack_get_type - Based on the mic bias value, this function returns |
Peter Meerwald | a92b078 | 2012-10-02 14:08:19 +0200 | [diff] [blame] | 168 | * the type of jack from the zones declared in the jack type |
Vinod Koul | fa9879e | 2011-02-09 14:44:17 +0530 | [diff] [blame] | 169 | * |
Peter Meerwald | a92b078 | 2012-10-02 14:08:19 +0200 | [diff] [blame] | 170 | * @jack: ASoC jack |
Vinod Koul | fa9879e | 2011-02-09 14:44:17 +0530 | [diff] [blame] | 171 | * @micbias_voltage: mic bias voltage at adc channel when jack is plugged in |
| 172 | * |
| 173 | * Based on the mic bias value passed, this function helps identify |
Peter Meerwald | a92b078 | 2012-10-02 14:08:19 +0200 | [diff] [blame] | 174 | * the type of jack from the already declared jack zones |
Vinod Koul | fa9879e | 2011-02-09 14:44:17 +0530 | [diff] [blame] | 175 | */ |
| 176 | int snd_soc_jack_get_type(struct snd_soc_jack *jack, int micbias_voltage) |
| 177 | { |
| 178 | struct snd_soc_jack_zone *zone; |
| 179 | |
| 180 | list_for_each_entry(zone, &jack->jack_zones, list) { |
| 181 | if (micbias_voltage >= zone->min_mv && |
| 182 | micbias_voltage < zone->max_mv) |
| 183 | return zone->jack_type; |
| 184 | } |
| 185 | return 0; |
| 186 | } |
| 187 | EXPORT_SYMBOL_GPL(snd_soc_jack_get_type); |
| 188 | |
| 189 | /** |
Mark Brown | 8a2cd61 | 2009-01-07 17:31:10 +0000 | [diff] [blame] | 190 | * snd_soc_jack_add_pins - Associate DAPM pins with an ASoC jack |
| 191 | * |
| 192 | * @jack: ASoC jack |
| 193 | * @count: Number of pins |
| 194 | * @pins: Array of pins |
| 195 | * |
| 196 | * After this function has been called the DAPM pins specified in the |
| 197 | * pins array will have their status updated to reflect the current |
| 198 | * state of the jack whenever the jack status is updated. |
| 199 | */ |
| 200 | int snd_soc_jack_add_pins(struct snd_soc_jack *jack, int count, |
| 201 | struct snd_soc_jack_pin *pins) |
| 202 | { |
| 203 | int i; |
| 204 | |
| 205 | for (i = 0; i < count; i++) { |
| 206 | if (!pins[i].pin) { |
Lars-Peter Clausen | 9709399 | 2015-03-04 10:33:17 +0100 | [diff] [blame] | 207 | dev_err(jack->card->dev, "ASoC: No name for pin %d\n", |
Liam Girdwood | 008d55e | 2012-11-19 14:39:14 +0000 | [diff] [blame] | 208 | i); |
Mark Brown | 8a2cd61 | 2009-01-07 17:31:10 +0000 | [diff] [blame] | 209 | return -EINVAL; |
| 210 | } |
| 211 | if (!pins[i].mask) { |
Lars-Peter Clausen | 9709399 | 2015-03-04 10:33:17 +0100 | [diff] [blame] | 212 | dev_err(jack->card->dev, "ASoC: No mask for pin %d" |
Liam Girdwood | 008d55e | 2012-11-19 14:39:14 +0000 | [diff] [blame] | 213 | " (%s)\n", i, pins[i].pin); |
Mark Brown | 8a2cd61 | 2009-01-07 17:31:10 +0000 | [diff] [blame] | 214 | return -EINVAL; |
| 215 | } |
| 216 | |
| 217 | INIT_LIST_HEAD(&pins[i].list); |
| 218 | list_add(&(pins[i].list), &jack->pins); |
Jie Yang | f63e858 | 2015-04-27 21:21:00 +0800 | [diff] [blame] | 219 | snd_jack_add_new_kctl(jack->jack, pins[i].pin, pins[i].mask); |
Mark Brown | 8a2cd61 | 2009-01-07 17:31:10 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | /* Update to reflect the last reported status; canned jack |
| 223 | * implementations are likely to set their state before the |
| 224 | * card has an opportunity to associate pins. |
| 225 | */ |
| 226 | snd_soc_jack_report(jack, 0, 0); |
| 227 | |
| 228 | return 0; |
| 229 | } |
| 230 | EXPORT_SYMBOL_GPL(snd_soc_jack_add_pins); |
Lopez Cruz, Misael | ec67624 | 2009-03-03 15:25:04 -0600 | [diff] [blame] | 231 | |
Mark Brown | d5021ec | 2010-03-22 12:06:30 +0000 | [diff] [blame] | 232 | /** |
| 233 | * snd_soc_jack_notifier_register - Register a notifier for jack status |
| 234 | * |
| 235 | * @jack: ASoC jack |
| 236 | * @nb: Notifier block to register |
| 237 | * |
| 238 | * Register for notification of the current status of the jack. Note |
| 239 | * that it is not possible to report additional jack events in the |
| 240 | * callback from the notifier, this is intended to support |
| 241 | * applications such as enabling electrical detection only when a |
| 242 | * mechanical detection event has occurred. |
| 243 | */ |
| 244 | void snd_soc_jack_notifier_register(struct snd_soc_jack *jack, |
| 245 | struct notifier_block *nb) |
| 246 | { |
| 247 | blocking_notifier_chain_register(&jack->notifier, nb); |
| 248 | } |
| 249 | EXPORT_SYMBOL_GPL(snd_soc_jack_notifier_register); |
| 250 | |
| 251 | /** |
| 252 | * snd_soc_jack_notifier_unregister - Unregister a notifier for jack status |
| 253 | * |
| 254 | * @jack: ASoC jack |
| 255 | * @nb: Notifier block to unregister |
| 256 | * |
| 257 | * Stop notifying for status changes. |
| 258 | */ |
| 259 | void snd_soc_jack_notifier_unregister(struct snd_soc_jack *jack, |
| 260 | struct notifier_block *nb) |
| 261 | { |
| 262 | blocking_notifier_chain_unregister(&jack->notifier, nb); |
| 263 | } |
| 264 | EXPORT_SYMBOL_GPL(snd_soc_jack_notifier_unregister); |
| 265 | |
Lopez Cruz, Misael | ec67624 | 2009-03-03 15:25:04 -0600 | [diff] [blame] | 266 | #ifdef CONFIG_GPIOLIB |
| 267 | /* gpio detect */ |
Mark Brown | 26bd7b4 | 2009-03-06 11:32:17 +0000 | [diff] [blame] | 268 | static void snd_soc_jack_gpio_detect(struct snd_soc_jack_gpio *gpio) |
Lopez Cruz, Misael | ec67624 | 2009-03-03 15:25:04 -0600 | [diff] [blame] | 269 | { |
| 270 | struct snd_soc_jack *jack = gpio->jack; |
| 271 | int enable; |
| 272 | int report; |
| 273 | |
Jarkko Nikula | 50dfb69 | 2014-05-26 14:34:36 +0300 | [diff] [blame] | 274 | enable = gpiod_get_value_cansleep(gpio->desc); |
Lopez Cruz, Misael | ec67624 | 2009-03-03 15:25:04 -0600 | [diff] [blame] | 275 | if (gpio->invert) |
| 276 | enable = !enable; |
| 277 | |
| 278 | if (enable) |
| 279 | report = gpio->report; |
| 280 | else |
| 281 | report = 0; |
| 282 | |
Joonyoung Shim | c871a05 | 2009-11-12 17:14:04 +0900 | [diff] [blame] | 283 | if (gpio->jack_status_check) |
xiangxiao | cb29d7b | 2014-02-23 14:40:44 +0800 | [diff] [blame] | 284 | report = gpio->jack_status_check(gpio->data); |
Joonyoung Shim | c871a05 | 2009-11-12 17:14:04 +0900 | [diff] [blame] | 285 | |
Lopez Cruz, Misael | ec67624 | 2009-03-03 15:25:04 -0600 | [diff] [blame] | 286 | snd_soc_jack_report(jack, report, gpio->report); |
| 287 | } |
| 288 | |
| 289 | /* irq handler for gpio pin */ |
| 290 | static irqreturn_t gpio_handler(int irq, void *data) |
| 291 | { |
| 292 | struct snd_soc_jack_gpio *gpio = data; |
Lars-Peter Clausen | 9709399 | 2015-03-04 10:33:17 +0100 | [diff] [blame] | 293 | struct device *dev = gpio->jack->card->dev; |
Mark Brown | f9a6705 | 2010-11-14 19:25:09 +0000 | [diff] [blame] | 294 | |
Mark Brown | 3028eb8 | 2010-12-05 12:22:46 +0000 | [diff] [blame] | 295 | trace_snd_soc_jack_irq(gpio->name); |
| 296 | |
Mark Brown | f9a6705 | 2010-11-14 19:25:09 +0000 | [diff] [blame] | 297 | if (device_may_wakeup(dev)) |
| 298 | pm_wakeup_event(dev, gpio->debounce_time + 50); |
Lopez Cruz, Misael | ec67624 | 2009-03-03 15:25:04 -0600 | [diff] [blame] | 299 | |
Mark Brown | e6058aa | 2013-07-18 22:47:10 +0100 | [diff] [blame] | 300 | queue_delayed_work(system_power_efficient_wq, &gpio->work, |
Mark Brown | 4c14d78 | 2010-10-06 15:54:28 -0700 | [diff] [blame] | 301 | msecs_to_jiffies(gpio->debounce_time)); |
Lopez Cruz, Misael | ec67624 | 2009-03-03 15:25:04 -0600 | [diff] [blame] | 302 | |
| 303 | return IRQ_HANDLED; |
| 304 | } |
| 305 | |
| 306 | /* gpio work */ |
| 307 | static void gpio_work(struct work_struct *work) |
| 308 | { |
| 309 | struct snd_soc_jack_gpio *gpio; |
| 310 | |
Mark Brown | 4c14d78 | 2010-10-06 15:54:28 -0700 | [diff] [blame] | 311 | gpio = container_of(work, struct snd_soc_jack_gpio, work.work); |
Lopez Cruz, Misael | ec67624 | 2009-03-03 15:25:04 -0600 | [diff] [blame] | 312 | snd_soc_jack_gpio_detect(gpio); |
| 313 | } |
| 314 | |
Dmitry Torokhov | 73548dd | 2017-03-21 16:50:43 -0700 | [diff] [blame] | 315 | static int snd_soc_jack_pm_notifier(struct notifier_block *nb, |
| 316 | unsigned long action, void *data) |
| 317 | { |
| 318 | struct snd_soc_jack_gpio *gpio = |
| 319 | container_of(nb, struct snd_soc_jack_gpio, pm_notifier); |
| 320 | |
| 321 | switch (action) { |
| 322 | case PM_POST_SUSPEND: |
| 323 | case PM_POST_HIBERNATION: |
| 324 | case PM_POST_RESTORE: |
| 325 | /* |
| 326 | * Use workqueue so we do not have to care about running |
| 327 | * concurrently with work triggered by the interrupt handler. |
| 328 | */ |
| 329 | queue_delayed_work(system_power_efficient_wq, &gpio->work, 0); |
| 330 | break; |
| 331 | } |
| 332 | |
| 333 | return NOTIFY_DONE; |
| 334 | } |
| 335 | |
Lopez Cruz, Misael | ec67624 | 2009-03-03 15:25:04 -0600 | [diff] [blame] | 336 | /** |
| 337 | * snd_soc_jack_add_gpios - Associate GPIO pins with an ASoC jack |
| 338 | * |
| 339 | * @jack: ASoC jack |
| 340 | * @count: number of pins |
| 341 | * @gpios: array of gpio pins |
| 342 | * |
| 343 | * This function will request gpio, set data direction and request irq |
| 344 | * for each gpio in the array. |
| 345 | */ |
| 346 | int snd_soc_jack_add_gpios(struct snd_soc_jack *jack, int count, |
| 347 | struct snd_soc_jack_gpio *gpios) |
| 348 | { |
| 349 | int i, ret; |
| 350 | |
| 351 | for (i = 0; i < count; i++) { |
Lopez Cruz, Misael | ec67624 | 2009-03-03 15:25:04 -0600 | [diff] [blame] | 352 | if (!gpios[i].name) { |
Lars-Peter Clausen | 9709399 | 2015-03-04 10:33:17 +0100 | [diff] [blame] | 353 | dev_err(jack->card->dev, |
Jarkko Nikula | f025d3b | 2014-05-26 14:34:37 +0300 | [diff] [blame] | 354 | "ASoC: No name for gpio at index %d\n", i); |
Lopez Cruz, Misael | ec67624 | 2009-03-03 15:25:04 -0600 | [diff] [blame] | 355 | ret = -EINVAL; |
| 356 | goto undo; |
| 357 | } |
| 358 | |
Dylan Reid | e616d2e | 2015-05-22 15:09:20 -0700 | [diff] [blame] | 359 | if (gpios[i].desc) { |
| 360 | /* Already have a GPIO descriptor. */ |
| 361 | goto got_gpio; |
| 362 | } else if (gpios[i].gpiod_dev) { |
| 363 | /* Get a GPIO descriptor */ |
Jarkko Nikula | f025d3b | 2014-05-26 14:34:37 +0300 | [diff] [blame] | 364 | gpios[i].desc = gpiod_get_index(gpios[i].gpiod_dev, |
| 365 | gpios[i].name, |
Alexandre Courbot | 00d647b | 2014-10-23 17:15:18 +0900 | [diff] [blame] | 366 | gpios[i].idx, GPIOD_IN); |
Jarkko Nikula | f025d3b | 2014-05-26 14:34:37 +0300 | [diff] [blame] | 367 | if (IS_ERR(gpios[i].desc)) { |
| 368 | ret = PTR_ERR(gpios[i].desc); |
| 369 | dev_err(gpios[i].gpiod_dev, |
| 370 | "ASoC: Cannot get gpio at index %d: %d", |
| 371 | i, ret); |
| 372 | goto undo; |
| 373 | } |
| 374 | } else { |
| 375 | /* legacy GPIO number */ |
| 376 | if (!gpio_is_valid(gpios[i].gpio)) { |
Lars-Peter Clausen | 9709399 | 2015-03-04 10:33:17 +0100 | [diff] [blame] | 377 | dev_err(jack->card->dev, |
Jarkko Nikula | f025d3b | 2014-05-26 14:34:37 +0300 | [diff] [blame] | 378 | "ASoC: Invalid gpio %d\n", |
| 379 | gpios[i].gpio); |
| 380 | ret = -EINVAL; |
| 381 | goto undo; |
| 382 | } |
Lopez Cruz, Misael | ec67624 | 2009-03-03 15:25:04 -0600 | [diff] [blame] | 383 | |
Alexandre Courbot | 00d647b | 2014-10-23 17:15:18 +0900 | [diff] [blame] | 384 | ret = gpio_request_one(gpios[i].gpio, GPIOF_IN, |
| 385 | gpios[i].name); |
Jarkko Nikula | f025d3b | 2014-05-26 14:34:37 +0300 | [diff] [blame] | 386 | if (ret) |
| 387 | goto undo; |
| 388 | |
| 389 | gpios[i].desc = gpio_to_desc(gpios[i].gpio); |
| 390 | } |
Dylan Reid | e616d2e | 2015-05-22 15:09:20 -0700 | [diff] [blame] | 391 | got_gpio: |
Mark Brown | 4c14d78 | 2010-10-06 15:54:28 -0700 | [diff] [blame] | 392 | INIT_DELAYED_WORK(&gpios[i].work, gpio_work); |
Lars-Peter Clausen | b8e22c1 | 2009-07-31 21:15:25 +0200 | [diff] [blame] | 393 | gpios[i].jack = jack; |
| 394 | |
Jarkko Nikula | 50dfb69 | 2014-05-26 14:34:36 +0300 | [diff] [blame] | 395 | ret = request_any_context_irq(gpiod_to_irq(gpios[i].desc), |
Mark Brown | 3f58fd8 | 2010-11-03 09:35:31 -0400 | [diff] [blame] | 396 | gpio_handler, |
| 397 | IRQF_TRIGGER_RISING | |
| 398 | IRQF_TRIGGER_FALLING, |
Stephen Warren | 363618f | 2011-04-01 14:50:43 -0600 | [diff] [blame] | 399 | gpios[i].name, |
Mark Brown | 3f58fd8 | 2010-11-03 09:35:31 -0400 | [diff] [blame] | 400 | &gpios[i]); |
Axel Lin | d2b4c7b | 2011-08-13 19:15:01 +0800 | [diff] [blame] | 401 | if (ret < 0) |
Lopez Cruz, Misael | ec67624 | 2009-03-03 15:25:04 -0600 | [diff] [blame] | 402 | goto err; |
| 403 | |
Mark Brown | 7887ab3 | 2011-02-17 16:35:55 -0800 | [diff] [blame] | 404 | if (gpios[i].wake) { |
Jarkko Nikula | 50dfb69 | 2014-05-26 14:34:36 +0300 | [diff] [blame] | 405 | ret = irq_set_irq_wake(gpiod_to_irq(gpios[i].desc), 1); |
Mark Brown | 7887ab3 | 2011-02-17 16:35:55 -0800 | [diff] [blame] | 406 | if (ret != 0) |
Lars-Peter Clausen | 9709399 | 2015-03-04 10:33:17 +0100 | [diff] [blame] | 407 | dev_err(jack->card->dev, |
Jarkko Nikula | f025d3b | 2014-05-26 14:34:37 +0300 | [diff] [blame] | 408 | "ASoC: Failed to mark GPIO at index %d as wake source: %d\n", |
| 409 | i, ret); |
Mark Brown | 7887ab3 | 2011-02-17 16:35:55 -0800 | [diff] [blame] | 410 | } |
| 411 | |
Dmitry Torokhov | 73548dd | 2017-03-21 16:50:43 -0700 | [diff] [blame] | 412 | /* |
| 413 | * Register PM notifier so we do not miss state transitions |
| 414 | * happening while system is asleep. |
| 415 | */ |
| 416 | gpios[i].pm_notifier.notifier_call = snd_soc_jack_pm_notifier; |
| 417 | register_pm_notifier(&gpios[i].pm_notifier); |
| 418 | |
Janusz Krzysztofik | 178b699 | 2009-07-24 02:48:57 +0200 | [diff] [blame] | 419 | /* Expose GPIO value over sysfs for diagnostic purposes */ |
Jarkko Nikula | 50dfb69 | 2014-05-26 14:34:36 +0300 | [diff] [blame] | 420 | gpiod_export(gpios[i].desc, false); |
Janusz Krzysztofik | 178b699 | 2009-07-24 02:48:57 +0200 | [diff] [blame] | 421 | |
Janusz Krzysztofik | 178b699 | 2009-07-24 02:48:57 +0200 | [diff] [blame] | 422 | /* Update initial jack status */ |
xiangxiao | f1adf5b | 2014-02-23 14:44:52 +0800 | [diff] [blame] | 423 | schedule_delayed_work(&gpios[i].work, |
| 424 | msecs_to_jiffies(gpios[i].debounce_time)); |
Lopez Cruz, Misael | ec67624 | 2009-03-03 15:25:04 -0600 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | return 0; |
| 428 | |
| 429 | err: |
| 430 | gpio_free(gpios[i].gpio); |
| 431 | undo: |
| 432 | snd_soc_jack_free_gpios(jack, i, gpios); |
| 433 | |
| 434 | return ret; |
| 435 | } |
| 436 | EXPORT_SYMBOL_GPL(snd_soc_jack_add_gpios); |
| 437 | |
| 438 | /** |
Jarkko Nikula | f025d3b | 2014-05-26 14:34:37 +0300 | [diff] [blame] | 439 | * snd_soc_jack_add_gpiods - Associate GPIO descriptor pins with an ASoC jack |
| 440 | * |
| 441 | * @gpiod_dev: GPIO consumer device |
| 442 | * @jack: ASoC jack |
| 443 | * @count: number of pins |
| 444 | * @gpios: array of gpio pins |
| 445 | * |
| 446 | * This function will request gpio, set data direction and request irq |
| 447 | * for each gpio in the array. |
| 448 | */ |
| 449 | int snd_soc_jack_add_gpiods(struct device *gpiod_dev, |
| 450 | struct snd_soc_jack *jack, |
| 451 | int count, struct snd_soc_jack_gpio *gpios) |
| 452 | { |
| 453 | int i; |
| 454 | |
| 455 | for (i = 0; i < count; i++) |
| 456 | gpios[i].gpiod_dev = gpiod_dev; |
| 457 | |
| 458 | return snd_soc_jack_add_gpios(jack, count, gpios); |
| 459 | } |
| 460 | EXPORT_SYMBOL_GPL(snd_soc_jack_add_gpiods); |
| 461 | |
| 462 | /** |
Lopez Cruz, Misael | ec67624 | 2009-03-03 15:25:04 -0600 | [diff] [blame] | 463 | * snd_soc_jack_free_gpios - Release GPIO pins' resources of an ASoC jack |
| 464 | * |
| 465 | * @jack: ASoC jack |
| 466 | * @count: number of pins |
| 467 | * @gpios: array of gpio pins |
| 468 | * |
| 469 | * Release gpio and irq resources for gpio pins associated with an ASoC jack. |
| 470 | */ |
| 471 | void snd_soc_jack_free_gpios(struct snd_soc_jack *jack, int count, |
| 472 | struct snd_soc_jack_gpio *gpios) |
| 473 | { |
| 474 | int i; |
| 475 | |
| 476 | for (i = 0; i < count; i++) { |
Jarkko Nikula | 50dfb69 | 2014-05-26 14:34:36 +0300 | [diff] [blame] | 477 | gpiod_unexport(gpios[i].desc); |
Dmitry Torokhov | 73548dd | 2017-03-21 16:50:43 -0700 | [diff] [blame] | 478 | unregister_pm_notifier(&gpios[i].pm_notifier); |
Jarkko Nikula | 50dfb69 | 2014-05-26 14:34:36 +0300 | [diff] [blame] | 479 | free_irq(gpiod_to_irq(gpios[i].desc), &gpios[i]); |
Mark Brown | 4c14d78 | 2010-10-06 15:54:28 -0700 | [diff] [blame] | 480 | cancel_delayed_work_sync(&gpios[i].work); |
Jarkko Nikula | 50dfb69 | 2014-05-26 14:34:36 +0300 | [diff] [blame] | 481 | gpiod_put(gpios[i].desc); |
Lopez Cruz, Misael | ec67624 | 2009-03-03 15:25:04 -0600 | [diff] [blame] | 482 | gpios[i].jack = NULL; |
| 483 | } |
| 484 | } |
| 485 | EXPORT_SYMBOL_GPL(snd_soc_jack_free_gpios); |
| 486 | #endif /* CONFIG_GPIOLIB */ |