Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]
Version: [ 2.6.11.8 ] [ 2.6.25 ] [ 2.6.25.8 ] [ 2.6.31.13 ] Architecture: [ i386 ]
  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>
 17 #include <linux/gpio.h>
 18 #include <linux/interrupt.h>
 19 #include <linux/workqueue.h>
 20 #include <linux/delay.h>
 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 
 41         return snd_jack_new(card->codec->card, id, type, &jack->jack);
 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 {
 61         struct snd_soc_codec *codec = jack->card->codec;
 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         }
 70 
 71         mutex_lock(&codec->mutex);
 72 
 73         oldstatus = jack->status;
 74 
 75         jack->status &= ~mask;
 76         jack->status |= status;
 77 
 78         /* The DAPM sync is expensive enough to be worth skipping */
 79         if (jack->status == oldstatus)
 80                 goto out;
 81 
 82         list_for_each_entry(pin, &jack->pins, list) {
 83                 enable = pin->mask & status;
 84 
 85                 if (pin->invert)
 86                         enable = !enable;
 87 
 88                 if (enable)
 89                         snd_soc_dapm_enable_pin(codec, pin->pin);
 90                 else
 91                         snd_soc_dapm_disable_pin(codec, pin->pin);
 92         }
 93 
 94         snd_soc_dapm_sync(codec);
 95 
 96         snd_jack_report(jack->jack, status);
 97 
 98 out:
 99         mutex_unlock(&codec->mutex);
100 }
101 EXPORT_SYMBOL_GPL(snd_soc_jack_report);
102 
103 /**
104  * snd_soc_jack_add_pins - Associate DAPM pins with an ASoC jack
105  *
106  * @jack:  ASoC jack
107  * @count: Number of pins
108  * @pins:  Array of pins
109  *
110  * After this function has been called the DAPM pins specified in the
111  * pins array will have their status updated to reflect the current
112  * state of the jack whenever the jack status is updated.
113  */
114 int snd_soc_jack_add_pins(struct snd_soc_jack *jack, int count,
115                           struct snd_soc_jack_pin *pins)
116 {
117         int i;
118 
119         for (i = 0; i < count; i++) {
120                 if (!pins[i].pin) {
121                         printk(KERN_ERR "No name for pin %d\n", i);
122                         return -EINVAL;
123                 }
124                 if (!pins[i].mask) {
125                         printk(KERN_ERR "No mask for pin %d (%s)\n", i,
126                                pins[i].pin);
127                         return -EINVAL;
128                 }
129 
130                 INIT_LIST_HEAD(&pins[i].list);
131                 list_add(&(pins[i].list), &jack->pins);
132         }
133 
134         /* Update to reflect the last reported status; canned jack
135          * implementations are likely to set their state before the
136          * card has an opportunity to associate pins.
137          */
138         snd_soc_jack_report(jack, 0, 0);
139 
140         return 0;
141 }
142 EXPORT_SYMBOL_GPL(snd_soc_jack_add_pins);
143 
144 #ifdef CONFIG_GPIOLIB
145 /* gpio detect */
146 static 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 */
268 
  This page was automatically generated by the LXR engine.