Blame view
sound/soc/soc-jack.c
7.74 KB
8a2cd6180 ASoC: Add jack re... |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
/* * 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> #include <sound/soc-dapm.h> |
ec67624d3 ASoC: Add GPIO su... |
17 18 19 20 |
#include <linux/gpio.h> #include <linux/interrupt.h> #include <linux/workqueue.h> #include <linux/delay.h> |
8a2cd6180 ASoC: Add jack re... |
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
/** * snd_soc_jack_new - Create a new jack * @card: ASoC card * @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. */ int snd_soc_jack_new(struct snd_soc_card *card, const char *id, int type, struct snd_soc_jack *jack) { jack->card = card; INIT_LIST_HEAD(&jack->pins); |
d5021ec9f ASoC: Add a notif... |
40 |
BLOCKING_INIT_NOTIFIER_HEAD(&jack->notifier); |
8a2cd6180 ASoC: Add jack re... |
41 |
|
6627a653b ASoC: Push the co... |
42 |
return snd_jack_new(card->codec->card, id, type, &jack->jack); |
8a2cd6180 ASoC: Add jack re... |
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
} 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) { |
4f066173f ASoC: Move derefe... |
62 |
struct snd_soc_codec *codec; |
8a2cd6180 ASoC: Add jack re... |
63 64 65 |
struct snd_soc_jack_pin *pin; int enable; int oldstatus; |
3a278a0c6 ASoC: Allow repor... |
66 |
if (!jack) |
8a2cd6180 ASoC: Add jack re... |
67 |
return; |
3a278a0c6 ASoC: Allow repor... |
68 |
|
4f066173f ASoC: Move derefe... |
69 |
codec = jack->card->codec; |
8a2cd6180 ASoC: Add jack re... |
70 71 72 73 74 75 |
mutex_lock(&codec->mutex); oldstatus = jack->status; jack->status &= ~mask; |
178b699c2 ASoC: Jack handli... |
76 |
jack->status |= status & mask; |
8a2cd6180 ASoC: Add jack re... |
77 |
|
178b699c2 ASoC: Jack handli... |
78 79 80 |
/* The DAPM sync is expensive enough to be worth skipping. * However, empty mask means pin synchronization is desired. */ if (mask && (jack->status == oldstatus)) |
8a2cd6180 ASoC: Add jack re... |
81 82 83 |
goto out; list_for_each_entry(pin, &jack->pins, list) { |
178b699c2 ASoC: Jack handli... |
84 |
enable = pin->mask & jack->status; |
8a2cd6180 ASoC: Add jack re... |
85 86 87 88 89 90 91 92 93 |
if (pin->invert) enable = !enable; if (enable) snd_soc_dapm_enable_pin(codec, pin->pin); else snd_soc_dapm_disable_pin(codec, pin->pin); } |
d5021ec9f ASoC: Add a notif... |
94 95 |
/* Report before the DAPM sync to help users updating micbias status */ blocking_notifier_call_chain(&jack->notifier, status, NULL); |
8a2cd6180 ASoC: Add jack re... |
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
snd_soc_dapm_sync(codec); snd_jack_report(jack->jack, status); out: mutex_unlock(&codec->mutex); } EXPORT_SYMBOL_GPL(snd_soc_jack_report); /** * 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) { printk(KERN_ERR "No name for pin %d ", i); return -EINVAL; } if (!pins[i].mask) { printk(KERN_ERR "No mask for pin %d (%s) ", i, pins[i].pin); 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); |
ec67624d3 ASoC: Add GPIO su... |
147 |
|
d5021ec9f ASoC: Add a notif... |
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
/** * 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); |
ec67624d3 ASoC: Add GPIO su... |
181 182 |
#ifdef CONFIG_GPIOLIB /* gpio detect */ |
26bd7b496 ASoC: Staticise w... |
183 |
static void snd_soc_jack_gpio_detect(struct snd_soc_jack_gpio *gpio) |
ec67624d3 ASoC: Add GPIO su... |
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
{ struct snd_soc_jack *jack = gpio->jack; int enable; int report; if (gpio->debounce_time > 0) mdelay(gpio->debounce_time); enable = gpio_get_value(gpio->gpio); if (gpio->invert) enable = !enable; if (enable) report = gpio->report; else report = 0; |
c871a0531 ASoC: Add jack_st... |
200 201 |
if (gpio->jack_status_check) report = gpio->jack_status_check(); |
ec67624d3 ASoC: Add GPIO su... |
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
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; schedule_work(&gpio->work); return IRQ_HANDLED; } /* gpio work */ static void gpio_work(struct work_struct *work) { struct snd_soc_jack_gpio *gpio; gpio = container_of(work, struct snd_soc_jack_gpio, work); 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)) { printk(KERN_ERR "Invalid gpio %d ", gpios[i].gpio); ret = -EINVAL; goto undo; } if (!gpios[i].name) { printk(KERN_ERR "No name for gpio %d ", 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; |
b8e22c1fe ASoC: jack: Fix r... |
262 263 |
INIT_WORK(&gpios[i].work, gpio_work); gpios[i].jack = jack; |
ec67624d3 ASoC: Add GPIO su... |
264 265 266 267 268 269 270 |
ret = request_irq(gpio_to_irq(gpios[i].gpio), gpio_handler, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, jack->card->dev->driver->name, &gpios[i]); if (ret) goto err; |
178b699c2 ASoC: Jack handli... |
271 272 273 274 |
#ifdef CONFIG_GPIO_SYSFS /* Expose GPIO value over sysfs for diagnostic purposes */ gpio_export(gpios[i].gpio, false); #endif |
178b699c2 ASoC: Jack handli... |
275 276 |
/* Update initial jack status */ snd_soc_jack_gpio_detect(&gpios[i]); |
ec67624d3 ASoC: Add GPIO su... |
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
} 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++) { |
178b699c2 ASoC: Jack handli... |
305 306 307 |
#ifdef CONFIG_GPIO_SYSFS gpio_unexport(gpios[i].gpio); #endif |
ec67624d3 ASoC: Add GPIO su... |
308 309 310 311 312 313 314 |
free_irq(gpio_to_irq(gpios[i].gpio), &gpios[i]); gpio_free(gpios[i].gpio); gpios[i].jack = NULL; } } EXPORT_SYMBOL_GPL(snd_soc_jack_free_gpios); #endif /* CONFIG_GPIOLIB */ |