0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <sound/jack.h>
0010 #include <sound/soc.h>
0011 #include <linux/gpio.h>
0012 #include <linux/gpio/consumer.h>
0013 #include <linux/interrupt.h>
0014 #include <linux/workqueue.h>
0015 #include <linux/delay.h>
0016 #include <linux/export.h>
0017 #include <linux/suspend.h>
0018 #include <trace/events/asoc.h>
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034 void snd_soc_jack_report(struct snd_soc_jack *jack, int status, int mask)
0035 {
0036 struct snd_soc_dapm_context *dapm;
0037 struct snd_soc_jack_pin *pin;
0038 unsigned int sync = 0;
0039
0040 if (!jack)
0041 return;
0042 trace_snd_soc_jack_report(jack, mask, status);
0043
0044 dapm = &jack->card->dapm;
0045
0046 mutex_lock(&jack->mutex);
0047
0048 jack->status &= ~mask;
0049 jack->status |= status & mask;
0050
0051 trace_snd_soc_jack_notify(jack, status);
0052
0053 list_for_each_entry(pin, &jack->pins, list) {
0054 int enable = pin->mask & jack->status;
0055
0056 if (pin->invert)
0057 enable = !enable;
0058
0059 if (enable)
0060 snd_soc_dapm_enable_pin(dapm, pin->pin);
0061 else
0062 snd_soc_dapm_disable_pin(dapm, pin->pin);
0063
0064
0065 sync = 1;
0066 }
0067
0068
0069 blocking_notifier_call_chain(&jack->notifier, jack->status, jack);
0070
0071 if (sync)
0072 snd_soc_dapm_sync(dapm);
0073
0074 snd_jack_report(jack->jack, jack->status);
0075
0076 mutex_unlock(&jack->mutex);
0077 }
0078 EXPORT_SYMBOL_GPL(snd_soc_jack_report);
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090 int snd_soc_jack_add_zones(struct snd_soc_jack *jack, int count,
0091 struct snd_soc_jack_zone *zones)
0092 {
0093 int i;
0094
0095 for (i = 0; i < count; i++) {
0096 INIT_LIST_HEAD(&zones[i].list);
0097 list_add(&(zones[i].list), &jack->jack_zones);
0098 }
0099 return 0;
0100 }
0101 EXPORT_SYMBOL_GPL(snd_soc_jack_add_zones);
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113 int snd_soc_jack_get_type(struct snd_soc_jack *jack, int micbias_voltage)
0114 {
0115 struct snd_soc_jack_zone *zone;
0116
0117 list_for_each_entry(zone, &jack->jack_zones, list) {
0118 if (micbias_voltage >= zone->min_mv &&
0119 micbias_voltage < zone->max_mv)
0120 return zone->jack_type;
0121 }
0122 return 0;
0123 }
0124 EXPORT_SYMBOL_GPL(snd_soc_jack_get_type);
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137 int snd_soc_jack_add_pins(struct snd_soc_jack *jack, int count,
0138 struct snd_soc_jack_pin *pins)
0139 {
0140 int i;
0141
0142 for (i = 0; i < count; i++) {
0143 if (!pins[i].pin) {
0144 dev_err(jack->card->dev, "ASoC: No name for pin %d\n",
0145 i);
0146 return -EINVAL;
0147 }
0148 if (!pins[i].mask) {
0149 dev_err(jack->card->dev, "ASoC: No mask for pin %d"
0150 " (%s)\n", i, pins[i].pin);
0151 return -EINVAL;
0152 }
0153
0154 INIT_LIST_HEAD(&pins[i].list);
0155 list_add(&(pins[i].list), &jack->pins);
0156 snd_jack_add_new_kctl(jack->jack, pins[i].pin, pins[i].mask);
0157 }
0158
0159
0160
0161
0162
0163 snd_soc_jack_report(jack, 0, 0);
0164
0165 return 0;
0166 }
0167 EXPORT_SYMBOL_GPL(snd_soc_jack_add_pins);
0168
0169
0170
0171
0172
0173
0174
0175
0176
0177
0178
0179
0180
0181 void snd_soc_jack_notifier_register(struct snd_soc_jack *jack,
0182 struct notifier_block *nb)
0183 {
0184 blocking_notifier_chain_register(&jack->notifier, nb);
0185 }
0186 EXPORT_SYMBOL_GPL(snd_soc_jack_notifier_register);
0187
0188
0189
0190
0191
0192
0193
0194
0195
0196 void snd_soc_jack_notifier_unregister(struct snd_soc_jack *jack,
0197 struct notifier_block *nb)
0198 {
0199 blocking_notifier_chain_unregister(&jack->notifier, nb);
0200 }
0201 EXPORT_SYMBOL_GPL(snd_soc_jack_notifier_unregister);
0202
0203 #ifdef CONFIG_GPIOLIB
0204 struct jack_gpio_tbl {
0205 int count;
0206 struct snd_soc_jack *jack;
0207 struct snd_soc_jack_gpio *gpios;
0208 };
0209
0210
0211 static void snd_soc_jack_gpio_detect(struct snd_soc_jack_gpio *gpio)
0212 {
0213 struct snd_soc_jack *jack = gpio->jack;
0214 int enable;
0215 int report;
0216
0217 enable = gpiod_get_value_cansleep(gpio->desc);
0218 if (gpio->invert)
0219 enable = !enable;
0220
0221 if (enable)
0222 report = gpio->report;
0223 else
0224 report = 0;
0225
0226 if (gpio->jack_status_check)
0227 report = gpio->jack_status_check(gpio->data);
0228
0229 snd_soc_jack_report(jack, report, gpio->report);
0230 }
0231
0232
0233 static irqreturn_t gpio_handler(int irq, void *data)
0234 {
0235 struct snd_soc_jack_gpio *gpio = data;
0236 struct device *dev = gpio->jack->card->dev;
0237
0238 trace_snd_soc_jack_irq(gpio->name);
0239
0240 if (device_may_wakeup(dev))
0241 pm_wakeup_event(dev, gpio->debounce_time + 50);
0242
0243 queue_delayed_work(system_power_efficient_wq, &gpio->work,
0244 msecs_to_jiffies(gpio->debounce_time));
0245
0246 return IRQ_HANDLED;
0247 }
0248
0249
0250 static void gpio_work(struct work_struct *work)
0251 {
0252 struct snd_soc_jack_gpio *gpio;
0253
0254 gpio = container_of(work, struct snd_soc_jack_gpio, work.work);
0255 snd_soc_jack_gpio_detect(gpio);
0256 }
0257
0258 static int snd_soc_jack_pm_notifier(struct notifier_block *nb,
0259 unsigned long action, void *data)
0260 {
0261 struct snd_soc_jack_gpio *gpio =
0262 container_of(nb, struct snd_soc_jack_gpio, pm_notifier);
0263
0264 switch (action) {
0265 case PM_POST_SUSPEND:
0266 case PM_POST_HIBERNATION:
0267 case PM_POST_RESTORE:
0268
0269
0270
0271
0272 queue_delayed_work(system_power_efficient_wq, &gpio->work, 0);
0273 break;
0274 }
0275
0276 return NOTIFY_DONE;
0277 }
0278
0279 static void jack_free_gpios(struct snd_soc_jack *jack, int count,
0280 struct snd_soc_jack_gpio *gpios)
0281 {
0282 int i;
0283
0284 for (i = 0; i < count; i++) {
0285 gpiod_unexport(gpios[i].desc);
0286 unregister_pm_notifier(&gpios[i].pm_notifier);
0287 free_irq(gpiod_to_irq(gpios[i].desc), &gpios[i]);
0288 cancel_delayed_work_sync(&gpios[i].work);
0289 gpiod_put(gpios[i].desc);
0290 gpios[i].jack = NULL;
0291 }
0292 }
0293
0294 static void jack_devres_free_gpios(struct device *dev, void *res)
0295 {
0296 struct jack_gpio_tbl *tbl = res;
0297
0298 jack_free_gpios(tbl->jack, tbl->count, tbl->gpios);
0299 }
0300
0301
0302
0303
0304
0305
0306
0307
0308
0309
0310
0311 int snd_soc_jack_add_gpios(struct snd_soc_jack *jack, int count,
0312 struct snd_soc_jack_gpio *gpios)
0313 {
0314 int i, ret;
0315 struct jack_gpio_tbl *tbl;
0316
0317 tbl = devres_alloc(jack_devres_free_gpios, sizeof(*tbl), GFP_KERNEL);
0318 if (!tbl)
0319 return -ENOMEM;
0320 tbl->jack = jack;
0321 tbl->count = count;
0322 tbl->gpios = gpios;
0323
0324 for (i = 0; i < count; i++) {
0325 if (!gpios[i].name) {
0326 dev_err(jack->card->dev,
0327 "ASoC: No name for gpio at index %d\n", i);
0328 ret = -EINVAL;
0329 goto undo;
0330 }
0331
0332 if (gpios[i].desc) {
0333
0334 goto got_gpio;
0335 } else if (gpios[i].gpiod_dev) {
0336
0337 gpios[i].desc = gpiod_get_index(gpios[i].gpiod_dev,
0338 gpios[i].name,
0339 gpios[i].idx, GPIOD_IN);
0340 if (IS_ERR(gpios[i].desc)) {
0341 ret = PTR_ERR(gpios[i].desc);
0342 dev_err(gpios[i].gpiod_dev,
0343 "ASoC: Cannot get gpio at index %d: %d",
0344 i, ret);
0345 goto undo;
0346 }
0347 } else {
0348
0349 if (!gpio_is_valid(gpios[i].gpio)) {
0350 dev_err(jack->card->dev,
0351 "ASoC: Invalid gpio %d\n",
0352 gpios[i].gpio);
0353 ret = -EINVAL;
0354 goto undo;
0355 }
0356
0357 ret = gpio_request_one(gpios[i].gpio, GPIOF_IN,
0358 gpios[i].name);
0359 if (ret)
0360 goto undo;
0361
0362 gpios[i].desc = gpio_to_desc(gpios[i].gpio);
0363 }
0364 got_gpio:
0365 INIT_DELAYED_WORK(&gpios[i].work, gpio_work);
0366 gpios[i].jack = jack;
0367
0368 ret = request_any_context_irq(gpiod_to_irq(gpios[i].desc),
0369 gpio_handler,
0370 IRQF_TRIGGER_RISING |
0371 IRQF_TRIGGER_FALLING,
0372 gpios[i].name,
0373 &gpios[i]);
0374 if (ret < 0)
0375 goto err;
0376
0377 if (gpios[i].wake) {
0378 ret = irq_set_irq_wake(gpiod_to_irq(gpios[i].desc), 1);
0379 if (ret != 0)
0380 dev_err(jack->card->dev,
0381 "ASoC: Failed to mark GPIO at index %d as wake source: %d\n",
0382 i, ret);
0383 }
0384
0385
0386
0387
0388
0389 gpios[i].pm_notifier.notifier_call = snd_soc_jack_pm_notifier;
0390 register_pm_notifier(&gpios[i].pm_notifier);
0391
0392
0393 gpiod_export(gpios[i].desc, false);
0394
0395
0396 schedule_delayed_work(&gpios[i].work,
0397 msecs_to_jiffies(gpios[i].debounce_time));
0398 }
0399
0400 devres_add(jack->card->dev, tbl);
0401 return 0;
0402
0403 err:
0404 gpio_free(gpios[i].gpio);
0405 undo:
0406 jack_free_gpios(jack, i, gpios);
0407 devres_free(tbl);
0408
0409 return ret;
0410 }
0411 EXPORT_SYMBOL_GPL(snd_soc_jack_add_gpios);
0412
0413
0414
0415
0416
0417
0418
0419
0420
0421
0422
0423
0424 int snd_soc_jack_add_gpiods(struct device *gpiod_dev,
0425 struct snd_soc_jack *jack,
0426 int count, struct snd_soc_jack_gpio *gpios)
0427 {
0428 int i;
0429
0430 for (i = 0; i < count; i++)
0431 gpios[i].gpiod_dev = gpiod_dev;
0432
0433 return snd_soc_jack_add_gpios(jack, count, gpios);
0434 }
0435 EXPORT_SYMBOL_GPL(snd_soc_jack_add_gpiods);
0436
0437
0438
0439
0440
0441
0442
0443
0444
0445
0446 void snd_soc_jack_free_gpios(struct snd_soc_jack *jack, int count,
0447 struct snd_soc_jack_gpio *gpios)
0448 {
0449 jack_free_gpios(jack, count, gpios);
0450 devres_destroy(jack->card->dev, jack_devres_free_gpios, NULL, NULL);
0451 }
0452 EXPORT_SYMBOL_GPL(snd_soc_jack_free_gpios);
0453 #endif