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> |
| 16 | #include <sound/soc-dapm.h> |
Lopez Cruz, Misael | ec67624 | 2009-03-03 15:25:04 -0600 | [diff] [blame] | 17 | #include <linux/gpio.h> |
| 18 | #include <linux/interrupt.h> |
| 19 | #include <linux/workqueue.h> |
| 20 | #include <linux/delay.h> |
Mark Brown | 8a2cd61 | 2009-01-07 17:31:10 +0000 | [diff] [blame] | 21 | |
| 22 | /** |
| 23 | * snd_soc_jack_new - Create a new jack |
| 24 | * @card: ASoC card |
| 25 | * @id: an identifying string for this jack |
| 26 | * @type: a bitmask of enum snd_jack_type values that can be detected by |
| 27 | * this jack |
| 28 | * @jack: structure to use for the jack |
| 29 | * |
| 30 | * Creates a new jack object. |
| 31 | * |
| 32 | * Returns zero if successful, or a negative error code on failure. |
| 33 | * On success jack will be initialised. |
| 34 | */ |
| 35 | int snd_soc_jack_new(struct snd_soc_card *card, const char *id, int type, |
| 36 | struct snd_soc_jack *jack) |
| 37 | { |
| 38 | jack->card = card; |
| 39 | INIT_LIST_HEAD(&jack->pins); |
| 40 | |
Mark Brown | 6627a65 | 2009-01-23 22:55:23 +0000 | [diff] [blame] | 41 | return snd_jack_new(card->codec->card, id, type, &jack->jack); |
Mark Brown | 8a2cd61 | 2009-01-07 17:31:10 +0000 | [diff] [blame] | 42 | } |
| 43 | EXPORT_SYMBOL_GPL(snd_soc_jack_new); |
| 44 | |
| 45 | /** |
| 46 | * snd_soc_jack_report - Report the current status for a jack |
| 47 | * |
| 48 | * @jack: the jack |
| 49 | * @status: a bitmask of enum snd_jack_type values that are currently detected. |
| 50 | * @mask: a bitmask of enum snd_jack_type values that being reported. |
| 51 | * |
| 52 | * If configured using snd_soc_jack_add_pins() then the associated |
| 53 | * DAPM pins will be enabled or disabled as appropriate and DAPM |
| 54 | * synchronised. |
| 55 | * |
| 56 | * Note: This function uses mutexes and should be called from a |
| 57 | * context which can sleep (such as a workqueue). |
| 58 | */ |
| 59 | void snd_soc_jack_report(struct snd_soc_jack *jack, int status, int mask) |
| 60 | { |
Julia Lawall | 4f06617 | 2009-10-17 08:32:56 +0200 | [diff] [blame^] | 61 | struct snd_soc_codec *codec; |
Mark Brown | 8a2cd61 | 2009-01-07 17:31:10 +0000 | [diff] [blame] | 62 | struct snd_soc_jack_pin *pin; |
| 63 | int enable; |
| 64 | int oldstatus; |
| 65 | |
| 66 | if (!jack) { |
| 67 | WARN_ON_ONCE(!jack); |
| 68 | return; |
| 69 | } |
Julia Lawall | 4f06617 | 2009-10-17 08:32:56 +0200 | [diff] [blame^] | 70 | codec = jack->card->codec; |
Mark Brown | 8a2cd61 | 2009-01-07 17:31:10 +0000 | [diff] [blame] | 71 | |
| 72 | mutex_lock(&codec->mutex); |
| 73 | |
| 74 | oldstatus = jack->status; |
| 75 | |
| 76 | jack->status &= ~mask; |
Janusz Krzysztofik | 178b699 | 2009-07-24 02:48:57 +0200 | [diff] [blame] | 77 | jack->status |= status & mask; |
Mark Brown | 8a2cd61 | 2009-01-07 17:31:10 +0000 | [diff] [blame] | 78 | |
Janusz Krzysztofik | 178b699 | 2009-07-24 02:48:57 +0200 | [diff] [blame] | 79 | /* The DAPM sync is expensive enough to be worth skipping. |
| 80 | * However, empty mask means pin synchronization is desired. */ |
| 81 | if (mask && (jack->status == oldstatus)) |
Mark Brown | 8a2cd61 | 2009-01-07 17:31:10 +0000 | [diff] [blame] | 82 | goto out; |
| 83 | |
| 84 | list_for_each_entry(pin, &jack->pins, list) { |
Janusz Krzysztofik | 178b699 | 2009-07-24 02:48:57 +0200 | [diff] [blame] | 85 | enable = pin->mask & jack->status; |
Mark Brown | 8a2cd61 | 2009-01-07 17:31:10 +0000 | [diff] [blame] | 86 | |
| 87 | if (pin->invert) |
| 88 | enable = !enable; |
| 89 | |
| 90 | if (enable) |
| 91 | snd_soc_dapm_enable_pin(codec, pin->pin); |
| 92 | else |
| 93 | snd_soc_dapm_disable_pin(codec, pin->pin); |
| 94 | } |
| 95 | |
| 96 | snd_soc_dapm_sync(codec); |
| 97 | |
| 98 | snd_jack_report(jack->jack, status); |
| 99 | |
| 100 | out: |
| 101 | mutex_unlock(&codec->mutex); |
| 102 | } |
| 103 | EXPORT_SYMBOL_GPL(snd_soc_jack_report); |
| 104 | |
| 105 | /** |
| 106 | * snd_soc_jack_add_pins - Associate DAPM pins with an ASoC jack |
| 107 | * |
| 108 | * @jack: ASoC jack |
| 109 | * @count: Number of pins |
| 110 | * @pins: Array of pins |
| 111 | * |
| 112 | * After this function has been called the DAPM pins specified in the |
| 113 | * pins array will have their status updated to reflect the current |
| 114 | * state of the jack whenever the jack status is updated. |
| 115 | */ |
| 116 | int snd_soc_jack_add_pins(struct snd_soc_jack *jack, int count, |
| 117 | struct snd_soc_jack_pin *pins) |
| 118 | { |
| 119 | int i; |
| 120 | |
| 121 | for (i = 0; i < count; i++) { |
| 122 | if (!pins[i].pin) { |
| 123 | printk(KERN_ERR "No name for pin %d\n", i); |
| 124 | return -EINVAL; |
| 125 | } |
| 126 | if (!pins[i].mask) { |
| 127 | printk(KERN_ERR "No mask for pin %d (%s)\n", i, |
| 128 | pins[i].pin); |
| 129 | return -EINVAL; |
| 130 | } |
| 131 | |
| 132 | INIT_LIST_HEAD(&pins[i].list); |
| 133 | list_add(&(pins[i].list), &jack->pins); |
| 134 | } |
| 135 | |
| 136 | /* Update to reflect the last reported status; canned jack |
| 137 | * implementations are likely to set their state before the |
| 138 | * card has an opportunity to associate pins. |
| 139 | */ |
| 140 | snd_soc_jack_report(jack, 0, 0); |
| 141 | |
| 142 | return 0; |
| 143 | } |
| 144 | EXPORT_SYMBOL_GPL(snd_soc_jack_add_pins); |
Lopez Cruz, Misael | ec67624 | 2009-03-03 15:25:04 -0600 | [diff] [blame] | 145 | |
| 146 | #ifdef CONFIG_GPIOLIB |
| 147 | /* gpio detect */ |
Mark Brown | 26bd7b4 | 2009-03-06 11:32:17 +0000 | [diff] [blame] | 148 | 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] | 149 | { |
| 150 | struct snd_soc_jack *jack = gpio->jack; |
| 151 | int enable; |
| 152 | int report; |
| 153 | |
| 154 | if (gpio->debounce_time > 0) |
| 155 | mdelay(gpio->debounce_time); |
| 156 | |
| 157 | enable = gpio_get_value(gpio->gpio); |
| 158 | if (gpio->invert) |
| 159 | enable = !enable; |
| 160 | |
| 161 | if (enable) |
| 162 | report = gpio->report; |
| 163 | else |
| 164 | report = 0; |
| 165 | |
| 166 | snd_soc_jack_report(jack, report, gpio->report); |
| 167 | } |
| 168 | |
| 169 | /* irq handler for gpio pin */ |
| 170 | static irqreturn_t gpio_handler(int irq, void *data) |
| 171 | { |
| 172 | struct snd_soc_jack_gpio *gpio = data; |
| 173 | |
| 174 | schedule_work(&gpio->work); |
| 175 | |
| 176 | return IRQ_HANDLED; |
| 177 | } |
| 178 | |
| 179 | /* gpio work */ |
| 180 | static void gpio_work(struct work_struct *work) |
| 181 | { |
| 182 | struct snd_soc_jack_gpio *gpio; |
| 183 | |
| 184 | gpio = container_of(work, struct snd_soc_jack_gpio, work); |
| 185 | snd_soc_jack_gpio_detect(gpio); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * snd_soc_jack_add_gpios - Associate GPIO pins with an ASoC jack |
| 190 | * |
| 191 | * @jack: ASoC jack |
| 192 | * @count: number of pins |
| 193 | * @gpios: array of gpio pins |
| 194 | * |
| 195 | * This function will request gpio, set data direction and request irq |
| 196 | * for each gpio in the array. |
| 197 | */ |
| 198 | int snd_soc_jack_add_gpios(struct snd_soc_jack *jack, int count, |
| 199 | struct snd_soc_jack_gpio *gpios) |
| 200 | { |
| 201 | int i, ret; |
| 202 | |
| 203 | for (i = 0; i < count; i++) { |
| 204 | if (!gpio_is_valid(gpios[i].gpio)) { |
| 205 | printk(KERN_ERR "Invalid gpio %d\n", |
| 206 | gpios[i].gpio); |
| 207 | ret = -EINVAL; |
| 208 | goto undo; |
| 209 | } |
| 210 | if (!gpios[i].name) { |
| 211 | printk(KERN_ERR "No name for gpio %d\n", |
| 212 | gpios[i].gpio); |
| 213 | ret = -EINVAL; |
| 214 | goto undo; |
| 215 | } |
| 216 | |
| 217 | ret = gpio_request(gpios[i].gpio, gpios[i].name); |
| 218 | if (ret) |
| 219 | goto undo; |
| 220 | |
| 221 | ret = gpio_direction_input(gpios[i].gpio); |
| 222 | if (ret) |
| 223 | goto err; |
| 224 | |
Lars-Peter Clausen | b8e22c1 | 2009-07-31 21:15:25 +0200 | [diff] [blame] | 225 | INIT_WORK(&gpios[i].work, gpio_work); |
| 226 | gpios[i].jack = jack; |
| 227 | |
Lopez Cruz, Misael | ec67624 | 2009-03-03 15:25:04 -0600 | [diff] [blame] | 228 | ret = request_irq(gpio_to_irq(gpios[i].gpio), |
| 229 | gpio_handler, |
| 230 | IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, |
| 231 | jack->card->dev->driver->name, |
| 232 | &gpios[i]); |
| 233 | if (ret) |
| 234 | goto err; |
| 235 | |
Janusz Krzysztofik | 178b699 | 2009-07-24 02:48:57 +0200 | [diff] [blame] | 236 | #ifdef CONFIG_GPIO_SYSFS |
| 237 | /* Expose GPIO value over sysfs for diagnostic purposes */ |
| 238 | gpio_export(gpios[i].gpio, false); |
| 239 | #endif |
| 240 | |
Janusz Krzysztofik | 178b699 | 2009-07-24 02:48:57 +0200 | [diff] [blame] | 241 | /* Update initial jack status */ |
| 242 | snd_soc_jack_gpio_detect(&gpios[i]); |
Lopez Cruz, Misael | ec67624 | 2009-03-03 15:25:04 -0600 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | return 0; |
| 246 | |
| 247 | err: |
| 248 | gpio_free(gpios[i].gpio); |
| 249 | undo: |
| 250 | snd_soc_jack_free_gpios(jack, i, gpios); |
| 251 | |
| 252 | return ret; |
| 253 | } |
| 254 | EXPORT_SYMBOL_GPL(snd_soc_jack_add_gpios); |
| 255 | |
| 256 | /** |
| 257 | * snd_soc_jack_free_gpios - Release GPIO pins' resources of an ASoC jack |
| 258 | * |
| 259 | * @jack: ASoC jack |
| 260 | * @count: number of pins |
| 261 | * @gpios: array of gpio pins |
| 262 | * |
| 263 | * Release gpio and irq resources for gpio pins associated with an ASoC jack. |
| 264 | */ |
| 265 | void snd_soc_jack_free_gpios(struct snd_soc_jack *jack, int count, |
| 266 | struct snd_soc_jack_gpio *gpios) |
| 267 | { |
| 268 | int i; |
| 269 | |
| 270 | for (i = 0; i < count; i++) { |
Janusz Krzysztofik | 178b699 | 2009-07-24 02:48:57 +0200 | [diff] [blame] | 271 | #ifdef CONFIG_GPIO_SYSFS |
| 272 | gpio_unexport(gpios[i].gpio); |
| 273 | #endif |
Lopez Cruz, Misael | ec67624 | 2009-03-03 15:25:04 -0600 | [diff] [blame] | 274 | free_irq(gpio_to_irq(gpios[i].gpio), &gpios[i]); |
| 275 | gpio_free(gpios[i].gpio); |
| 276 | gpios[i].jack = NULL; |
| 277 | } |
| 278 | } |
| 279 | EXPORT_SYMBOL_GPL(snd_soc_jack_free_gpios); |
| 280 | #endif /* CONFIG_GPIOLIB */ |