diff options
| -rw-r--r-- | include/sound/soc.h | 32 | ||||
| -rw-r--r-- | sound/soc/soc-jack.c | 129 |
2 files changed, 161 insertions, 0 deletions
diff --git a/include/sound/soc.h b/include/sound/soc.h index 0e7735264169..a40bc6f316fc 100644 --- a/include/sound/soc.h +++ b/include/sound/soc.h | |||
| @@ -16,6 +16,8 @@ | |||
| 16 | #include <linux/platform_device.h> | 16 | #include <linux/platform_device.h> |
| 17 | #include <linux/types.h> | 17 | #include <linux/types.h> |
| 18 | #include <linux/workqueue.h> | 18 | #include <linux/workqueue.h> |
| 19 | #include <linux/interrupt.h> | ||
| 20 | #include <linux/kernel.h> | ||
| 19 | #include <sound/core.h> | 21 | #include <sound/core.h> |
| 20 | #include <sound/pcm.h> | 22 | #include <sound/pcm.h> |
| 21 | #include <sound/control.h> | 23 | #include <sound/control.h> |
| @@ -168,6 +170,9 @@ struct soc_enum; | |||
| 168 | struct snd_soc_ac97_ops; | 170 | struct snd_soc_ac97_ops; |
| 169 | struct snd_soc_jack; | 171 | struct snd_soc_jack; |
| 170 | struct snd_soc_jack_pin; | 172 | struct snd_soc_jack_pin; |
| 173 | #ifdef CONFIG_GPIOLIB | ||
| 174 | struct snd_soc_jack_gpio; | ||
| 175 | #endif | ||
| 171 | 176 | ||
| 172 | typedef int (*hw_write_t)(void *,const char* ,int); | 177 | typedef int (*hw_write_t)(void *,const char* ,int); |
| 173 | typedef int (*hw_read_t)(void *,char* ,int); | 178 | typedef int (*hw_read_t)(void *,char* ,int); |
| @@ -194,6 +199,12 @@ int snd_soc_jack_new(struct snd_soc_card *card, const char *id, int type, | |||
| 194 | void snd_soc_jack_report(struct snd_soc_jack *jack, int status, int mask); | 199 | void snd_soc_jack_report(struct snd_soc_jack *jack, int status, int mask); |
| 195 | int snd_soc_jack_add_pins(struct snd_soc_jack *jack, int count, | 200 | int snd_soc_jack_add_pins(struct snd_soc_jack *jack, int count, |
| 196 | struct snd_soc_jack_pin *pins); | 201 | struct snd_soc_jack_pin *pins); |
| 202 | #ifdef CONFIG_GPIOLIB | ||
| 203 | int snd_soc_jack_add_gpios(struct snd_soc_jack *jack, int count, | ||
| 204 | struct snd_soc_jack_gpio *gpios); | ||
| 205 | void snd_soc_jack_free_gpios(struct snd_soc_jack *jack, int count, | ||
| 206 | struct snd_soc_jack_gpio *gpios); | ||
| 207 | #endif | ||
| 197 | 208 | ||
| 198 | /* codec IO */ | 209 | /* codec IO */ |
| 199 | #define snd_soc_read(codec, reg) codec->read(codec, reg) | 210 | #define snd_soc_read(codec, reg) codec->read(codec, reg) |
| @@ -264,6 +275,27 @@ struct snd_soc_jack_pin { | |||
| 264 | bool invert; | 275 | bool invert; |
| 265 | }; | 276 | }; |
| 266 | 277 | ||
| 278 | /** | ||
| 279 | * struct snd_soc_jack_gpio - Describes a gpio pin for jack detection | ||
| 280 | * | ||
| 281 | * @gpio: gpio number | ||
| 282 | * @name: gpio name | ||
| 283 | * @report: value to report when jack detected | ||
| 284 | * @invert: report presence in low state | ||
| 285 | * @debouce_time: debouce time in ms | ||
| 286 | */ | ||
| 287 | #ifdef CONFIG_GPIOLIB | ||
| 288 | struct snd_soc_jack_gpio { | ||
| 289 | unsigned int gpio; | ||
| 290 | const char *name; | ||
| 291 | int report; | ||
| 292 | int invert; | ||
| 293 | int debounce_time; | ||
| 294 | struct snd_soc_jack *jack; | ||
| 295 | struct work_struct work; | ||
| 296 | }; | ||
| 297 | #endif | ||
| 298 | |||
| 267 | struct snd_soc_jack { | 299 | struct snd_soc_jack { |
| 268 | struct snd_jack *jack; | 300 | struct snd_jack *jack; |
| 269 | struct snd_soc_card *card; | 301 | struct snd_soc_card *card; |
diff --git a/sound/soc/soc-jack.c b/sound/soc/soc-jack.c index ab64a30bedde..bdf2484c2220 100644 --- a/sound/soc/soc-jack.c +++ b/sound/soc/soc-jack.c | |||
| @@ -14,6 +14,10 @@ | |||
| 14 | #include <sound/jack.h> | 14 | #include <sound/jack.h> |
| 15 | #include <sound/soc.h> | 15 | #include <sound/soc.h> |
| 16 | #include <sound/soc-dapm.h> | 16 | #include <sound/soc-dapm.h> |
| 17 | #include <linux/gpio.h> | ||
| 18 | #include <linux/interrupt.h> | ||
| 19 | #include <linux/workqueue.h> | ||
| 20 | #include <linux/delay.h> | ||
| 17 | 21 | ||
| 18 | /** | 22 | /** |
| 19 | * snd_soc_jack_new - Create a new jack | 23 | * snd_soc_jack_new - Create a new jack |
| @@ -136,3 +140,128 @@ int snd_soc_jack_add_pins(struct snd_soc_jack *jack, int count, | |||
| 136 | return 0; | 140 | return 0; |
| 137 | } | 141 | } |
| 138 | EXPORT_SYMBOL_GPL(snd_soc_jack_add_pins); | 142 | EXPORT_SYMBOL_GPL(snd_soc_jack_add_pins); |
| 143 | |||
| 144 | #ifdef CONFIG_GPIOLIB | ||
| 145 | /* gpio detect */ | ||
| 146 | void snd_soc_jack_gpio_detect(struct snd_soc_jack_gpio *gpio) | ||
| 147 | { | ||
| 148 | struct snd_soc_jack *jack = gpio->jack; | ||
| 149 | int enable; | ||
| 150 | int report; | ||
| 151 | |||
| 152 | if (gpio->debounce_time > 0) | ||
| 153 | mdelay(gpio->debounce_time); | ||
| 154 | |||
| 155 | enable = gpio_get_value(gpio->gpio); | ||
| 156 | if (gpio->invert) | ||
| 157 | enable = !enable; | ||
| 158 | |||
| 159 | if (enable) | ||
| 160 | report = gpio->report; | ||
| 161 | else | ||
| 162 | report = 0; | ||
| 163 | |||
| 164 | snd_soc_jack_report(jack, report, gpio->report); | ||
| 165 | } | ||
| 166 | |||
| 167 | /* irq handler for gpio pin */ | ||
| 168 | static irqreturn_t gpio_handler(int irq, void *data) | ||
| 169 | { | ||
| 170 | struct snd_soc_jack_gpio *gpio = data; | ||
| 171 | |||
| 172 | schedule_work(&gpio->work); | ||
| 173 | |||
| 174 | return IRQ_HANDLED; | ||
| 175 | } | ||
| 176 | |||
| 177 | /* gpio work */ | ||
| 178 | static void gpio_work(struct work_struct *work) | ||
| 179 | { | ||
| 180 | struct snd_soc_jack_gpio *gpio; | ||
| 181 | |||
| 182 | gpio = container_of(work, struct snd_soc_jack_gpio, work); | ||
| 183 | snd_soc_jack_gpio_detect(gpio); | ||
| 184 | } | ||
| 185 | |||
| 186 | /** | ||
| 187 | * snd_soc_jack_add_gpios - Associate GPIO pins with an ASoC jack | ||
| 188 | * | ||
| 189 | * @jack: ASoC jack | ||
| 190 | * @count: number of pins | ||
| 191 | * @gpios: array of gpio pins | ||
| 192 | * | ||
| 193 | * This function will request gpio, set data direction and request irq | ||
| 194 | * for each gpio in the array. | ||
| 195 | */ | ||
| 196 | int snd_soc_jack_add_gpios(struct snd_soc_jack *jack, int count, | ||
| 197 | struct snd_soc_jack_gpio *gpios) | ||
| 198 | { | ||
| 199 | int i, ret; | ||
| 200 | |||
| 201 | for (i = 0; i < count; i++) { | ||
| 202 | if (!gpio_is_valid(gpios[i].gpio)) { | ||
| 203 | printk(KERN_ERR "Invalid gpio %d\n", | ||
| 204 | gpios[i].gpio); | ||
| 205 | ret = -EINVAL; | ||
| 206 | goto undo; | ||
| 207 | } | ||
| 208 | if (!gpios[i].name) { | ||
| 209 | printk(KERN_ERR "No name for gpio %d\n", | ||
| 210 | gpios[i].gpio); | ||
| 211 | ret = -EINVAL; | ||
| 212 | goto undo; | ||
| 213 | } | ||
| 214 | |||
| 215 | ret = gpio_request(gpios[i].gpio, gpios[i].name); | ||
| 216 | if (ret) | ||
| 217 | goto undo; | ||
| 218 | |||
| 219 | ret = gpio_direction_input(gpios[i].gpio); | ||
| 220 | if (ret) | ||
| 221 | goto err; | ||
| 222 | |||
| 223 | ret = request_irq(gpio_to_irq(gpios[i].gpio), | ||
| 224 | gpio_handler, | ||
| 225 | IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, | ||
| 226 | jack->card->dev->driver->name, | ||
| 227 | &gpios[i]); | ||
| 228 | if (ret) | ||
| 229 | goto err; | ||
| 230 | |||
| 231 | INIT_WORK(&gpios[i].work, gpio_work); | ||
| 232 | gpios[i].jack = jack; | ||
| 233 | } | ||
| 234 | |||
| 235 | return 0; | ||
| 236 | |||
| 237 | err: | ||
| 238 | gpio_free(gpios[i].gpio); | ||
| 239 | undo: | ||
| 240 | snd_soc_jack_free_gpios(jack, i, gpios); | ||
| 241 | |||
| 242 | return ret; | ||
| 243 | } | ||
| 244 | EXPORT_SYMBOL_GPL(snd_soc_jack_add_gpios); | ||
| 245 | |||
| 246 | /** | ||
| 247 | * snd_soc_jack_free_gpios - Release GPIO pins' resources of an ASoC jack | ||
| 248 | * | ||
| 249 | * @jack: ASoC jack | ||
| 250 | * @count: number of pins | ||
| 251 | * @gpios: array of gpio pins | ||
| 252 | * | ||
| 253 | * Release gpio and irq resources for gpio pins associated with an ASoC jack. | ||
| 254 | */ | ||
| 255 | void snd_soc_jack_free_gpios(struct snd_soc_jack *jack, int count, | ||
| 256 | struct snd_soc_jack_gpio *gpios) | ||
| 257 | { | ||
| 258 | int i; | ||
| 259 | |||
| 260 | for (i = 0; i < count; i++) { | ||
| 261 | free_irq(gpio_to_irq(gpios[i].gpio), &gpios[i]); | ||
| 262 | gpio_free(gpios[i].gpio); | ||
| 263 | gpios[i].jack = NULL; | ||
| 264 | } | ||
| 265 | } | ||
| 266 | EXPORT_SYMBOL_GPL(snd_soc_jack_free_gpios); | ||
| 267 | #endif /* CONFIG_GPIOLIB */ | ||
