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