2 * Apple Onboard Audio feature call GPIO control
4 * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
6 * GPL v2, can be found in COPYING.
8 * This file contains the GPIO control routines for
9 * direct (through feature calls) access to the GPIO
13 #include <linux/of_irq.h>
14 #include <linux/interrupt.h>
15 #include <asm/pmac_feature.h>
18 /* TODO: these are lots of global variables
19 * that aren't used on most machines...
20 * Move them into a dynamically allocated
21 * structure and use that.
24 /* these are the GPIO numbers (register addresses as offsets into
26 static int headphone_mute_gpio;
27 static int master_mute_gpio;
28 static int amp_mute_gpio;
29 static int lineout_mute_gpio;
30 static int hw_reset_gpio;
31 static int lineout_detect_gpio;
32 static int headphone_detect_gpio;
33 static int linein_detect_gpio;
35 /* see the SWITCH_GPIO macro */
36 static int headphone_mute_gpio_activestate;
37 static int master_mute_gpio_activestate;
38 static int amp_mute_gpio_activestate;
39 static int lineout_mute_gpio_activestate;
40 static int hw_reset_gpio_activestate;
41 static int lineout_detect_gpio_activestate;
42 static int headphone_detect_gpio_activestate;
43 static int linein_detect_gpio_activestate;
45 /* node pointers that we save when getting the GPIO number
46 * to get the interrupt later */
47 static struct device_node *lineout_detect_node;
48 static struct device_node *linein_detect_node;
49 static struct device_node *headphone_detect_node;
51 static int lineout_detect_irq;
52 static int linein_detect_irq;
53 static int headphone_detect_irq;
55 static struct device_node *get_gpio(char *name,
60 struct device_node *np, *gpio;
62 const char *audio_gpio;
66 /* check if we can get it the easy way ... */
67 np = of_find_node_by_name(NULL, name);
69 /* some machines have only gpioX/extint-gpioX nodes,
70 * and an audio-gpio property saying what it is ...
71 * So what we have to do is enumerate all children
72 * of the gpio node and check them all. */
73 gpio = of_find_node_by_name(NULL, "gpio");
76 while ((np = of_get_next_child(gpio, np))) {
77 audio_gpio = of_get_property(np, "audio-gpio", NULL);
80 if (strcmp(audio_gpio, name) == 0)
82 if (altname && (strcmp(audio_gpio, altname) == 0))
86 /* still not found, assume not there */
91 reg = of_get_property(np, "reg", NULL);
99 /* this is a hack, usually the GPIOs 'reg' property
100 * should have the offset based from the GPIO space
101 * which is at 0x50, but apparently not always... */
105 reg = of_get_property(np, "audio-gpio-active-state", NULL);
107 /* Apple seems to default to 1, but
108 * that doesn't seem right at least on most
109 * machines. So until proven that the opposite
110 * is necessary, we default to 0
111 * (which, incidentally, snd-powermac also does...) */
114 *gpioactiveptr = *reg;
119 static void get_irq(struct device_node * np, int *irqptr)
122 *irqptr = irq_of_parse_and_map(np, 0);
127 /* 0x4 is outenable, 0x1 is out, thus 4 or 5 */
128 #define SWITCH_GPIO(name, v, on) \
130 (name##_gpio_activestate==0?4:5): \
131 (name##_gpio_activestate==0?5:4)))
133 #define FTR_GPIO(name, bit) \
134 static void ftr_gpio_set_##name(struct gpio_runtime *rt, int on)\
138 if (unlikely(!rt)) return; \
140 if (name##_mute_gpio < 0) \
143 v = pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, \
147 /* muted = !on... */ \
148 v = SWITCH_GPIO(name##_mute, v, !on); \
150 pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, \
151 name##_mute_gpio, v); \
153 rt->implementation_private &= ~(1<<bit); \
154 rt->implementation_private |= (!!on << bit); \
156 static int ftr_gpio_get_##name(struct gpio_runtime *rt) \
158 if (unlikely(!rt)) return 0; \
159 return (rt->implementation_private>>bit)&1; \
162 FTR_GPIO(headphone, 0);
164 FTR_GPIO(lineout, 2);
167 static void ftr_gpio_set_hw_reset(struct gpio_runtime *rt, int on)
171 if (unlikely(!rt)) return;
172 if (hw_reset_gpio < 0)
175 v = pmac_call_feature(PMAC_FTR_READ_GPIO, NULL,
177 v = SWITCH_GPIO(hw_reset, v, on);
178 pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL,
182 static struct gpio_methods methods;
184 static void ftr_gpio_all_amps_off(struct gpio_runtime *rt)
188 if (unlikely(!rt)) return;
189 saved = rt->implementation_private;
190 ftr_gpio_set_headphone(rt, 0);
191 ftr_gpio_set_amp(rt, 0);
192 ftr_gpio_set_lineout(rt, 0);
193 if (methods.set_master)
194 ftr_gpio_set_master(rt, 0);
195 rt->implementation_private = saved;
198 static void ftr_gpio_all_amps_restore(struct gpio_runtime *rt)
202 if (unlikely(!rt)) return;
203 s = rt->implementation_private;
204 ftr_gpio_set_headphone(rt, (s>>0)&1);
205 ftr_gpio_set_amp(rt, (s>>1)&1);
206 ftr_gpio_set_lineout(rt, (s>>2)&1);
207 if (methods.set_master)
208 ftr_gpio_set_master(rt, (s>>3)&1);
211 static void ftr_handle_notify(struct work_struct *work)
213 struct gpio_notification *notif =
214 container_of(work, struct gpio_notification, work.work);
216 mutex_lock(¬if->mutex);
218 notif->notify(notif->data);
219 mutex_unlock(¬if->mutex);
222 static void gpio_enable_dual_edge(int gpio)
228 v = pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, gpio, 0);
229 v |= 0x80; /* enable dual edge */
230 pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, gpio, v);
233 static void ftr_gpio_init(struct gpio_runtime *rt)
235 get_gpio("headphone-mute", NULL,
236 &headphone_mute_gpio,
237 &headphone_mute_gpio_activestate);
238 get_gpio("amp-mute", NULL,
240 &_mute_gpio_activestate);
241 get_gpio("lineout-mute", NULL,
243 &lineout_mute_gpio_activestate);
244 get_gpio("hw-reset", "audio-hw-reset",
246 &hw_reset_gpio_activestate);
247 if (get_gpio("master-mute", NULL,
249 &master_mute_gpio_activestate)) {
250 methods.set_master = ftr_gpio_set_master;
251 methods.get_master = ftr_gpio_get_master;
254 headphone_detect_node = get_gpio("headphone-detect", NULL,
255 &headphone_detect_gpio,
256 &headphone_detect_gpio_activestate);
257 /* go Apple, and thanks for giving these different names
258 * across the board... */
259 lineout_detect_node = get_gpio("lineout-detect", "line-output-detect",
260 &lineout_detect_gpio,
261 &lineout_detect_gpio_activestate);
262 linein_detect_node = get_gpio("linein-detect", "line-input-detect",
264 &linein_detect_gpio_activestate);
266 gpio_enable_dual_edge(headphone_detect_gpio);
267 gpio_enable_dual_edge(lineout_detect_gpio);
268 gpio_enable_dual_edge(linein_detect_gpio);
270 get_irq(headphone_detect_node, &headphone_detect_irq);
271 get_irq(lineout_detect_node, &lineout_detect_irq);
272 get_irq(linein_detect_node, &linein_detect_irq);
274 ftr_gpio_all_amps_off(rt);
275 rt->implementation_private = 0;
276 INIT_DELAYED_WORK(&rt->headphone_notify.work, ftr_handle_notify);
277 INIT_DELAYED_WORK(&rt->line_in_notify.work, ftr_handle_notify);
278 INIT_DELAYED_WORK(&rt->line_out_notify.work, ftr_handle_notify);
279 mutex_init(&rt->headphone_notify.mutex);
280 mutex_init(&rt->line_in_notify.mutex);
281 mutex_init(&rt->line_out_notify.mutex);
284 static void ftr_gpio_exit(struct gpio_runtime *rt)
286 ftr_gpio_all_amps_off(rt);
287 rt->implementation_private = 0;
288 if (rt->headphone_notify.notify)
289 free_irq(headphone_detect_irq, &rt->headphone_notify);
290 if (rt->line_in_notify.gpio_private)
291 free_irq(linein_detect_irq, &rt->line_in_notify);
292 if (rt->line_out_notify.gpio_private)
293 free_irq(lineout_detect_irq, &rt->line_out_notify);
294 cancel_delayed_work_sync(&rt->headphone_notify.work);
295 cancel_delayed_work_sync(&rt->line_in_notify.work);
296 cancel_delayed_work_sync(&rt->line_out_notify.work);
297 mutex_destroy(&rt->headphone_notify.mutex);
298 mutex_destroy(&rt->line_in_notify.mutex);
299 mutex_destroy(&rt->line_out_notify.mutex);
302 static irqreturn_t ftr_handle_notify_irq(int xx, void *data)
304 struct gpio_notification *notif = data;
306 schedule_delayed_work(¬if->work, 0);
311 static int ftr_set_notify(struct gpio_runtime *rt,
312 enum notify_type type,
313 notify_func_t notify,
316 struct gpio_notification *notif;
323 case AOA_NOTIFY_HEADPHONE:
324 notif = &rt->headphone_notify;
325 name = "headphone-detect";
326 irq = headphone_detect_irq;
328 case AOA_NOTIFY_LINE_IN:
329 notif = &rt->line_in_notify;
330 name = "linein-detect";
331 irq = linein_detect_irq;
333 case AOA_NOTIFY_LINE_OUT:
334 notif = &rt->line_out_notify;
335 name = "lineout-detect";
336 irq = lineout_detect_irq;
345 mutex_lock(¬if->mutex);
349 if (!old && !notify) {
355 if (old == notify && notif->data == data)
361 free_irq(irq, notif);
363 if (!old && notify) {
364 err = request_irq(irq, ftr_handle_notify_irq, 0, name, notif);
369 notif->notify = notify;
374 mutex_unlock(¬if->mutex);
378 static int ftr_get_detect(struct gpio_runtime *rt,
379 enum notify_type type)
381 int gpio, ret, active;
384 case AOA_NOTIFY_HEADPHONE:
385 gpio = headphone_detect_gpio;
386 active = headphone_detect_gpio_activestate;
388 case AOA_NOTIFY_LINE_IN:
389 gpio = linein_detect_gpio;
390 active = linein_detect_gpio_activestate;
392 case AOA_NOTIFY_LINE_OUT:
393 gpio = lineout_detect_gpio;
394 active = lineout_detect_gpio_activestate;
403 ret = pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, gpio, 0);
406 return ((ret >> 1) & 1) == active;
409 static struct gpio_methods methods = {
410 .init = ftr_gpio_init,
411 .exit = ftr_gpio_exit,
412 .all_amps_off = ftr_gpio_all_amps_off,
413 .all_amps_restore = ftr_gpio_all_amps_restore,
414 .set_headphone = ftr_gpio_set_headphone,
415 .set_speakers = ftr_gpio_set_amp,
416 .set_lineout = ftr_gpio_set_lineout,
417 .set_hw_reset = ftr_gpio_set_hw_reset,
418 .get_headphone = ftr_gpio_get_headphone,
419 .get_speakers = ftr_gpio_get_amp,
420 .get_lineout = ftr_gpio_get_lineout,
421 .set_notify = ftr_set_notify,
422 .get_detect = ftr_get_detect,
425 struct gpio_methods *ftr_gpio_methods = &methods;
426 EXPORT_SYMBOL_GPL(ftr_gpio_methods);