]> nv-tegra.nvidia Code Review - linux-2.6.git/blob - sound/aoa/core/snd-aoa-gpio-feature.c
Merge branch 'master' into upstream
[linux-2.6.git] / sound / aoa / core / snd-aoa-gpio-feature.c
1 /*
2  * Apple Onboard Audio feature call GPIO control
3  *
4  * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
5  *
6  * GPL v2, can be found in COPYING.
7  *
8  * This file contains the GPIO control routines for 
9  * direct (through feature calls) access to the GPIO
10  * registers.
11  */
12
13 #include <asm/pmac_feature.h>
14 #include <linux/interrupt.h>
15 #include "../aoa.h"
16
17 /* TODO: these are 20 global variables
18  * that aren't used on most machines...
19  * Move them into a dynamically allocated
20  * structure and use that.
21  */
22
23 /* these are the GPIO numbers (register addresses as offsets into
24  * the GPIO space) */
25 static int headphone_mute_gpio;
26 static int amp_mute_gpio;
27 static int lineout_mute_gpio;
28 static int hw_reset_gpio;
29 static int lineout_detect_gpio;
30 static int headphone_detect_gpio;
31 static int linein_detect_gpio;
32
33 /* see the SWITCH_GPIO macro */
34 static int headphone_mute_gpio_activestate;
35 static int amp_mute_gpio_activestate;
36 static int lineout_mute_gpio_activestate;
37 static int hw_reset_gpio_activestate;
38 static int lineout_detect_gpio_activestate;
39 static int headphone_detect_gpio_activestate;
40 static int linein_detect_gpio_activestate;
41
42 /* node pointers that we save when getting the GPIO number
43  * to get the interrupt later */
44 static struct device_node *lineout_detect_node;
45 static struct device_node *linein_detect_node;
46 static struct device_node *headphone_detect_node;
47
48 static int lineout_detect_irq;
49 static int linein_detect_irq;
50 static int headphone_detect_irq;
51
52 static struct device_node *get_gpio(char *name,
53                                     char *altname,
54                                     int *gpioptr,
55                                     int *gpioactiveptr)
56 {
57         struct device_node *np, *gpio;
58         u32 *reg;
59         char *audio_gpio;
60
61         *gpioptr = -1;
62
63         /* check if we can get it the easy way ... */
64         np = of_find_node_by_name(NULL, name);
65         if (!np) {
66                 /* some machines have only gpioX/extint-gpioX nodes,
67                  * and an audio-gpio property saying what it is ...
68                  * So what we have to do is enumerate all children
69                  * of the gpio node and check them all. */
70                 gpio = of_find_node_by_name(NULL, "gpio");
71                 if (!gpio)
72                         return NULL;
73                 while ((np = of_get_next_child(gpio, np))) {
74                         audio_gpio = get_property(np, "audio-gpio", NULL);
75                         if (!audio_gpio)
76                                 continue;
77                         if (strcmp(audio_gpio, name) == 0)
78                                 break;
79                         if (altname && (strcmp(audio_gpio, altname) == 0))
80                                 break;
81                 }
82                 /* still not found, assume not there */
83                 if (!np)
84                         return NULL;
85         }
86
87         reg = (u32 *)get_property(np, "reg", NULL);
88         if (!reg)
89                 return NULL;
90
91         *gpioptr = *reg;
92
93         /* this is a hack, usually the GPIOs 'reg' property
94          * should have the offset based from the GPIO space
95          * which is at 0x50, but apparently not always... */
96         if (*gpioptr < 0x50)
97                 *gpioptr += 0x50;
98
99         reg = (u32 *)get_property(np, "audio-gpio-active-state", NULL);
100         if (!reg)
101                 /* Apple seems to default to 1, but
102                  * that doesn't seem right at least on most
103                  * machines. So until proven that the opposite
104                  * is necessary, we default to 0
105                  * (which, incidentally, snd-powermac also does...) */
106                 *gpioactiveptr = 0;
107         else
108                 *gpioactiveptr = *reg;
109
110         return np;
111 }
112
113 static void get_irq(struct device_node * np, int *irqptr)
114 {
115         *irqptr = irq_of_parse_and_map(np, 0);
116 }
117
118 /* 0x4 is outenable, 0x1 is out, thus 4 or 5 */
119 #define SWITCH_GPIO(name, v, on)                                \
120         (((v)&~1) | ((on)?                                      \
121                         (name##_gpio_activestate==0?4:5):       \
122                         (name##_gpio_activestate==0?5:4)))
123
124 #define FTR_GPIO(name, bit)                                     \
125 static void ftr_gpio_set_##name(struct gpio_runtime *rt, int on)\
126 {                                                               \
127         int v;                                                  \
128                                                                 \
129         if (unlikely(!rt)) return;                              \
130                                                                 \
131         if (name##_mute_gpio < 0)                               \
132                 return;                                         \
133                                                                 \
134         v = pmac_call_feature(PMAC_FTR_READ_GPIO, NULL,         \
135                               name##_mute_gpio,                 \
136                               0);                               \
137                                                                 \
138         /* muted = !on... */                                    \
139         v = SWITCH_GPIO(name##_mute, v, !on);                   \
140                                                                 \
141         pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL,            \
142                           name##_mute_gpio, v);                 \
143                                                                 \
144         rt->implementation_private &= ~(1<<bit);                \
145         rt->implementation_private |= (!!on << bit);            \
146 }                                                               \
147 static int ftr_gpio_get_##name(struct gpio_runtime *rt)         \
148 {                                                               \
149         if (unlikely(!rt)) return 0;                            \
150         return (rt->implementation_private>>bit)&1;             \
151 }
152
153 FTR_GPIO(headphone, 0);
154 FTR_GPIO(amp, 1);
155 FTR_GPIO(lineout, 2);
156
157 static void ftr_gpio_set_hw_reset(struct gpio_runtime *rt, int on)
158 {
159         int v;
160
161         if (unlikely(!rt)) return;
162         if (hw_reset_gpio < 0)
163                 return;
164
165         v = pmac_call_feature(PMAC_FTR_READ_GPIO, NULL,
166                               hw_reset_gpio, 0);
167         v = SWITCH_GPIO(hw_reset, v, on);
168         pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL,
169                           hw_reset_gpio, v);
170 }
171
172 static void ftr_gpio_all_amps_off(struct gpio_runtime *rt)
173 {
174         int saved;
175
176         if (unlikely(!rt)) return;
177         saved = rt->implementation_private;
178         ftr_gpio_set_headphone(rt, 0);
179         ftr_gpio_set_amp(rt, 0);
180         ftr_gpio_set_lineout(rt, 0);
181         rt->implementation_private = saved;
182 }
183
184 static void ftr_gpio_all_amps_restore(struct gpio_runtime *rt)
185 {
186         int s;
187
188         if (unlikely(!rt)) return;
189         s = rt->implementation_private;
190         ftr_gpio_set_headphone(rt, (s>>0)&1);
191         ftr_gpio_set_amp(rt, (s>>1)&1);
192         ftr_gpio_set_lineout(rt, (s>>2)&1);
193 }
194
195 static void ftr_handle_notify(void *data)
196 {
197         struct gpio_notification *notif = data;
198
199         mutex_lock(&notif->mutex);
200         if (notif->notify)
201                 notif->notify(notif->data);
202         mutex_unlock(&notif->mutex);
203 }
204
205 static void gpio_enable_dual_edge(int gpio)
206 {
207         int v;
208
209         if (gpio == -1)
210                 return;
211         v = pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, gpio, 0);
212         v |= 0x80; /* enable dual edge */
213         pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, gpio, v);
214 }
215
216 static void ftr_gpio_init(struct gpio_runtime *rt)
217 {
218         get_gpio("headphone-mute", NULL,
219                  &headphone_mute_gpio,
220                  &headphone_mute_gpio_activestate);
221         get_gpio("amp-mute", NULL,
222                  &amp_mute_gpio,
223                  &amp_mute_gpio_activestate);
224         get_gpio("lineout-mute", NULL,
225                  &lineout_mute_gpio,
226                  &lineout_mute_gpio_activestate);
227         get_gpio("hw-reset", "audio-hw-reset",
228                  &hw_reset_gpio,
229                  &hw_reset_gpio_activestate);
230
231         headphone_detect_node = get_gpio("headphone-detect", NULL,
232                                          &headphone_detect_gpio,
233                                          &headphone_detect_gpio_activestate);
234         /* go Apple, and thanks for giving these different names
235          * across the board... */
236         lineout_detect_node = get_gpio("lineout-detect", "line-output-detect",
237                                        &lineout_detect_gpio,
238                                        &lineout_detect_gpio_activestate);
239         linein_detect_node = get_gpio("linein-detect", "line-input-detect",
240                                       &linein_detect_gpio,
241                                       &linein_detect_gpio_activestate);
242
243         gpio_enable_dual_edge(headphone_detect_gpio);
244         gpio_enable_dual_edge(lineout_detect_gpio);
245         gpio_enable_dual_edge(linein_detect_gpio);
246
247         get_irq(headphone_detect_node, &headphone_detect_irq);
248         get_irq(lineout_detect_node, &lineout_detect_irq);
249         get_irq(linein_detect_node, &linein_detect_irq);
250
251         ftr_gpio_all_amps_off(rt);
252         rt->implementation_private = 0;
253         INIT_WORK(&rt->headphone_notify.work, ftr_handle_notify,
254                   &rt->headphone_notify);
255         INIT_WORK(&rt->line_in_notify.work, ftr_handle_notify,
256                   &rt->line_in_notify);
257         INIT_WORK(&rt->line_out_notify.work, ftr_handle_notify,
258                   &rt->line_out_notify);
259         mutex_init(&rt->headphone_notify.mutex);
260         mutex_init(&rt->line_in_notify.mutex);
261         mutex_init(&rt->line_out_notify.mutex);
262 }
263
264 static void ftr_gpio_exit(struct gpio_runtime *rt)
265 {
266         ftr_gpio_all_amps_off(rt);
267         rt->implementation_private = 0;
268         if (rt->headphone_notify.notify)
269                 free_irq(headphone_detect_irq, &rt->headphone_notify);
270         if (rt->line_in_notify.gpio_private)
271                 free_irq(linein_detect_irq, &rt->line_in_notify);
272         if (rt->line_out_notify.gpio_private)
273                 free_irq(lineout_detect_irq, &rt->line_out_notify);
274         cancel_delayed_work(&rt->headphone_notify.work);
275         cancel_delayed_work(&rt->line_in_notify.work);
276         cancel_delayed_work(&rt->line_out_notify.work);
277         flush_scheduled_work();
278         mutex_destroy(&rt->headphone_notify.mutex);
279         mutex_destroy(&rt->line_in_notify.mutex);
280         mutex_destroy(&rt->line_out_notify.mutex);
281 }
282
283 static irqreturn_t ftr_handle_notify_irq(int xx,
284                                          void *data,
285                                          struct pt_regs *regs)
286 {
287         struct gpio_notification *notif = data;
288
289         schedule_work(&notif->work);
290
291         return IRQ_HANDLED;
292 }
293
294 static int ftr_set_notify(struct gpio_runtime *rt,
295                           enum notify_type type,
296                           notify_func_t notify,
297                           void *data)
298 {
299         struct gpio_notification *notif;
300         notify_func_t old;
301         int irq;
302         char *name;
303         int err = -EBUSY;
304
305         switch (type) {
306         case AOA_NOTIFY_HEADPHONE:
307                 notif = &rt->headphone_notify;
308                 name = "headphone-detect";
309                 irq = headphone_detect_irq;
310                 break;
311         case AOA_NOTIFY_LINE_IN:
312                 notif = &rt->line_in_notify;
313                 name = "linein-detect";
314                 irq = linein_detect_irq;
315                 break;
316         case AOA_NOTIFY_LINE_OUT:
317                 notif = &rt->line_out_notify;
318                 name = "lineout-detect";
319                 irq = lineout_detect_irq;
320                 break;
321         default:
322                 return -EINVAL;
323         }
324
325         if (irq == -1)
326                 return -ENODEV;
327
328         mutex_lock(&notif->mutex);
329
330         old = notif->notify;
331
332         if (!old && !notify) {
333                 err = 0;
334                 goto out_unlock;
335         }
336
337         if (old && notify) {
338                 if (old == notify && notif->data == data)
339                         err = 0;
340                 goto out_unlock;
341         }
342
343         if (old && !notify)
344                 free_irq(irq, notif);
345
346         if (!old && notify) {
347                 err = request_irq(irq, ftr_handle_notify_irq, 0, name, notif);
348                 if (err)
349                         goto out_unlock;
350         }
351
352         notif->notify = notify;
353         notif->data = data;
354
355         err = 0;
356  out_unlock:
357         mutex_unlock(&notif->mutex);
358         return err;
359 }
360
361 static int ftr_get_detect(struct gpio_runtime *rt,
362                           enum notify_type type)
363 {
364         int gpio, ret, active;
365
366         switch (type) {
367         case AOA_NOTIFY_HEADPHONE:
368                 gpio = headphone_detect_gpio;
369                 active = headphone_detect_gpio_activestate;
370                 break;
371         case AOA_NOTIFY_LINE_IN:
372                 gpio = linein_detect_gpio;
373                 active = linein_detect_gpio_activestate;
374                 break;
375         case AOA_NOTIFY_LINE_OUT:
376                 gpio = lineout_detect_gpio;
377                 active = lineout_detect_gpio_activestate;
378                 break;
379         default:
380                 return -EINVAL;
381         }
382
383         if (gpio == -1)
384                 return -ENODEV;
385
386         ret = pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, gpio, 0);
387         if (ret < 0)
388                 return ret;
389         return ((ret >> 1) & 1) == active;
390 }
391
392 static struct gpio_methods methods = {
393         .init                   = ftr_gpio_init,
394         .exit                   = ftr_gpio_exit,
395         .all_amps_off           = ftr_gpio_all_amps_off,
396         .all_amps_restore       = ftr_gpio_all_amps_restore,
397         .set_headphone          = ftr_gpio_set_headphone,
398         .set_speakers           = ftr_gpio_set_amp,
399         .set_lineout            = ftr_gpio_set_lineout,
400         .set_hw_reset           = ftr_gpio_set_hw_reset,
401         .get_headphone          = ftr_gpio_get_headphone,
402         .get_speakers           = ftr_gpio_get_amp,
403         .get_lineout            = ftr_gpio_get_lineout,
404         .set_notify             = ftr_set_notify,
405         .get_detect             = ftr_get_detect,
406 };
407
408 struct gpio_methods *ftr_gpio_methods = &methods;
409 EXPORT_SYMBOL_GPL(ftr_gpio_methods);