Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 //
0003 // soc-jack.c  --  ALSA SoC jack handling
0004 //
0005 // Copyright 2008 Wolfson Microelectronics PLC.
0006 //
0007 // Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
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  * snd_soc_jack_report - Report the current status for a jack
0022  *
0023  * @jack:   the jack
0024  * @status: a bitmask of enum snd_jack_type values that are currently detected.
0025  * @mask:   a bitmask of enum snd_jack_type values that being reported.
0026  *
0027  * If configured using snd_soc_jack_add_pins() then the associated
0028  * DAPM pins will be enabled or disabled as appropriate and DAPM
0029  * synchronised.
0030  *
0031  * Note: This function uses mutexes and should be called from a
0032  * context which can sleep (such as a workqueue).
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         /* we need to sync for this case only */
0065         sync = 1;
0066     }
0067 
0068     /* Report before the DAPM sync to help users updating micbias status */
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  * snd_soc_jack_add_zones - Associate voltage zones with jack
0082  *
0083  * @jack:  ASoC jack
0084  * @count: Number of zones
0085  * @zones:  Array of zones
0086  *
0087  * After this function has been called the zones specified in the
0088  * array will be associated with the jack.
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  * snd_soc_jack_get_type - Based on the mic bias value, this function returns
0105  * the type of jack from the zones declared in the jack type
0106  *
0107  * @jack:  ASoC jack
0108  * @micbias_voltage:  mic bias voltage at adc channel when jack is plugged in
0109  *
0110  * Based on the mic bias value passed, this function helps identify
0111  * the type of jack from the already declared jack zones
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  * snd_soc_jack_add_pins - Associate DAPM pins with an ASoC jack
0128  *
0129  * @jack:  ASoC jack created with snd_soc_card_jack_new_pins()
0130  * @count: Number of pins
0131  * @pins:  Array of pins
0132  *
0133  * After this function has been called the DAPM pins specified in the
0134  * pins array will have their status updated to reflect the current
0135  * state of the jack whenever the jack status is updated.
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     /* Update to reflect the last reported status; canned jack
0160      * implementations are likely to set their state before the
0161      * card has an opportunity to associate pins.
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  * snd_soc_jack_notifier_register - Register a notifier for jack status
0171  *
0172  * @jack:  ASoC jack
0173  * @nb:    Notifier block to register
0174  *
0175  * Register for notification of the current status of the jack.  Note
0176  * that it is not possible to report additional jack events in the
0177  * callback from the notifier, this is intended to support
0178  * applications such as enabling electrical detection only when a
0179  * mechanical detection event has occurred.
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  * snd_soc_jack_notifier_unregister - Unregister a notifier for jack status
0190  *
0191  * @jack:  ASoC jack
0192  * @nb:    Notifier block to unregister
0193  *
0194  * Stop notifying for status changes.
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 /* gpio detect */
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 /* irq handler for gpio pin */
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 /* gpio work */
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          * Use workqueue so we do not have to care about running
0270          * concurrently with work triggered by the interrupt handler.
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  * snd_soc_jack_add_gpios - Associate GPIO pins with an ASoC jack
0303  *
0304  * @jack:  ASoC jack
0305  * @count: number of pins
0306  * @gpios: array of gpio pins
0307  *
0308  * This function will request gpio, set data direction and request irq
0309  * for each gpio in the array.
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             /* Already have a GPIO descriptor. */
0334             goto got_gpio;
0335         } else if (gpios[i].gpiod_dev) {
0336             /* Get a GPIO descriptor */
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             /* legacy GPIO number */
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          * Register PM notifier so we do not miss state transitions
0387          * happening while system is asleep.
0388          */
0389         gpios[i].pm_notifier.notifier_call = snd_soc_jack_pm_notifier;
0390         register_pm_notifier(&gpios[i].pm_notifier);
0391 
0392         /* Expose GPIO value over sysfs for diagnostic purposes */
0393         gpiod_export(gpios[i].desc, false);
0394 
0395         /* Update initial jack status */
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  * snd_soc_jack_add_gpiods - Associate GPIO descriptor pins with an ASoC jack
0415  *
0416  * @gpiod_dev: GPIO consumer device
0417  * @jack:      ASoC jack
0418  * @count:     number of pins
0419  * @gpios:     array of gpio pins
0420  *
0421  * This function will request gpio, set data direction and request irq
0422  * for each gpio in the array.
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  * snd_soc_jack_free_gpios - Release GPIO pins' resources of an ASoC jack
0439  *
0440  * @jack:  ASoC jack
0441  * @count: number of pins
0442  * @gpios: array of gpio pins
0443  *
0444  * Release gpio and irq resources for gpio pins associated with an ASoC jack.
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  /* CONFIG_GPIOLIB */