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 <asm/pmac_feature.h>
14 #include <linux/interrupt.h>
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.
23 /* these are the GPIO numbers (register addresses as offsets into
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;
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;
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;
48 static int lineout_detect_irq;
49 static int linein_detect_irq;
50 static int headphone_detect_irq;
52 static struct device_node *get_gpio(char *name,
57 struct device_node *np, *gpio;
63 /* check if we can get it the easy way ... */
64 np = of_find_node_by_name(NULL, name);
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");
73 while ((np = of_get_next_child(gpio, np))) {
74 audio_gpio = get_property(np, "audio-gpio", NULL);
77 if (strcmp(audio_gpio, name) == 0)
79 if (altname && (strcmp(audio_gpio, altname) == 0))
82 /* still not found, assume not there */
87 reg = (u32 *)get_property(np, "reg", NULL);
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... */
99 reg = (u32 *)get_property(np, "audio-gpio-active-state", NULL);
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...) */
108 *gpioactiveptr = *reg;
113 static void get_irq(struct device_node * np, int *irqptr)
118 if (np->n_intrs != 1)
120 *irqptr = np->intrs[0].line;
123 /* 0x4 is outenable, 0x1 is out, thus 4 or 5 */
124 #define SWITCH_GPIO(name, v, on) \
126 (name##_gpio_activestate==0?4:5): \
127 (name##_gpio_activestate==0?5:4)))
129 #define FTR_GPIO(name, bit) \
130 static void ftr_gpio_set_##name(struct gpio_runtime *rt, int on)\
134 if (unlikely(!rt)) return; \
136 if (name##_mute_gpio < 0) \
139 v = pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, \
143 /* muted = !on... */ \
144 v = SWITCH_GPIO(name##_mute, v, !on); \
146 pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, \
147 name##_mute_gpio, v); \
149 rt->implementation_private &= ~(1<<bit); \
150 rt->implementation_private |= (!!on << bit); \
152 static int ftr_gpio_get_##name(struct gpio_runtime *rt) \
154 if (unlikely(!rt)) return 0; \
155 return (rt->implementation_private>>bit)&1; \
158 FTR_GPIO(headphone, 0);
160 FTR_GPIO(lineout, 2);
162 static void ftr_gpio_set_hw_reset(struct gpio_runtime *rt, int on)
166 if (unlikely(!rt)) return;
167 if (hw_reset_gpio < 0)
170 v = pmac_call_feature(PMAC_FTR_READ_GPIO, NULL,
172 v = SWITCH_GPIO(hw_reset, v, on);
173 pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL,
177 static void ftr_gpio_all_amps_off(struct gpio_runtime *rt)
181 if (unlikely(!rt)) return;
182 saved = rt->implementation_private;
183 ftr_gpio_set_headphone(rt, 0);
184 ftr_gpio_set_amp(rt, 0);
185 ftr_gpio_set_lineout(rt, 0);
186 rt->implementation_private = saved;
189 static void ftr_gpio_all_amps_restore(struct gpio_runtime *rt)
193 if (unlikely(!rt)) return;
194 s = rt->implementation_private;
195 ftr_gpio_set_headphone(rt, (s>>0)&1);
196 ftr_gpio_set_amp(rt, (s>>1)&1);
197 ftr_gpio_set_lineout(rt, (s>>2)&1);
200 static void ftr_handle_notify(void *data)
202 struct gpio_notification *notif = data;
204 mutex_lock(¬if->mutex);
206 notif->notify(notif->data);
207 mutex_unlock(¬if->mutex);
210 static void ftr_gpio_init(struct gpio_runtime *rt)
212 get_gpio("headphone-mute", NULL,
213 &headphone_mute_gpio,
214 &headphone_mute_gpio_activestate);
215 get_gpio("amp-mute", NULL,
217 &_mute_gpio_activestate);
218 get_gpio("lineout-mute", NULL,
220 &lineout_mute_gpio_activestate);
221 get_gpio("hw-reset", "audio-hw-reset",
223 &hw_reset_gpio_activestate);
225 headphone_detect_node = get_gpio("headphone-detect", NULL,
226 &headphone_detect_gpio,
227 &headphone_detect_gpio_activestate);
228 /* go Apple, and thanks for giving these different names
229 * across the board... */
230 lineout_detect_node = get_gpio("lineout-detect", "line-output-detect",
231 &lineout_detect_gpio,
232 &lineout_detect_gpio_activestate);
233 linein_detect_node = get_gpio("linein-detect", "line-input-detect",
235 &linein_detect_gpio_activestate);
237 get_irq(headphone_detect_node, &headphone_detect_irq);
238 get_irq(lineout_detect_node, &lineout_detect_irq);
239 get_irq(linein_detect_node, &linein_detect_irq);
241 ftr_gpio_all_amps_off(rt);
242 rt->implementation_private = 0;
243 INIT_WORK(&rt->headphone_notify.work, ftr_handle_notify,
244 &rt->headphone_notify);
245 INIT_WORK(&rt->line_in_notify.work, ftr_handle_notify,
246 &rt->line_in_notify);
247 INIT_WORK(&rt->line_out_notify.work, ftr_handle_notify,
248 &rt->line_out_notify);
249 mutex_init(&rt->headphone_notify.mutex);
250 mutex_init(&rt->line_in_notify.mutex);
251 mutex_init(&rt->line_out_notify.mutex);
254 static void ftr_gpio_exit(struct gpio_runtime *rt)
256 ftr_gpio_all_amps_off(rt);
257 rt->implementation_private = 0;
258 if (rt->headphone_notify.notify)
259 free_irq(headphone_detect_irq, &rt->headphone_notify);
260 if (rt->line_in_notify.gpio_private)
261 free_irq(linein_detect_irq, &rt->line_in_notify);
262 if (rt->line_out_notify.gpio_private)
263 free_irq(lineout_detect_irq, &rt->line_out_notify);
264 cancel_delayed_work(&rt->headphone_notify.work);
265 cancel_delayed_work(&rt->line_in_notify.work);
266 cancel_delayed_work(&rt->line_out_notify.work);
267 flush_scheduled_work();
268 mutex_destroy(&rt->headphone_notify.mutex);
269 mutex_destroy(&rt->line_in_notify.mutex);
270 mutex_destroy(&rt->line_out_notify.mutex);
273 static irqreturn_t ftr_handle_notify_irq(int xx,
275 struct pt_regs *regs)
277 struct gpio_notification *notif = data;
279 schedule_work(¬if->work);
284 static int ftr_set_notify(struct gpio_runtime *rt,
285 enum notify_type type,
286 notify_func_t notify,
289 struct gpio_notification *notif;
296 case AOA_NOTIFY_HEADPHONE:
297 notif = &rt->headphone_notify;
298 name = "headphone-detect";
299 irq = headphone_detect_irq;
301 case AOA_NOTIFY_LINE_IN:
302 notif = &rt->line_in_notify;
303 name = "linein-detect";
304 irq = linein_detect_irq;
306 case AOA_NOTIFY_LINE_OUT:
307 notif = &rt->line_out_notify;
308 name = "lineout-detect";
309 irq = lineout_detect_irq;
318 mutex_lock(¬if->mutex);
322 if (!old && !notify) {
328 if (old == notify && notif->data == data)
334 free_irq(irq, notif);
336 if (!old && notify) {
337 err = request_irq(irq, ftr_handle_notify_irq, 0, name, notif);
342 notif->notify = notify;
347 mutex_unlock(¬if->mutex);
351 static int ftr_get_detect(struct gpio_runtime *rt,
352 enum notify_type type)
354 int gpio, ret, active;
357 case AOA_NOTIFY_HEADPHONE:
358 gpio = headphone_detect_gpio;
359 active = headphone_detect_gpio_activestate;
361 case AOA_NOTIFY_LINE_IN:
362 gpio = linein_detect_gpio;
363 active = linein_detect_gpio_activestate;
365 case AOA_NOTIFY_LINE_OUT:
366 gpio = lineout_detect_gpio;
367 active = lineout_detect_gpio_activestate;
376 ret = pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, gpio, 0);
379 return ((ret >> 1) & 1) == active;
382 static struct gpio_methods methods = {
383 .init = ftr_gpio_init,
384 .exit = ftr_gpio_exit,
385 .all_amps_off = ftr_gpio_all_amps_off,
386 .all_amps_restore = ftr_gpio_all_amps_restore,
387 .set_headphone = ftr_gpio_set_headphone,
388 .set_speakers = ftr_gpio_set_amp,
389 .set_lineout = ftr_gpio_set_lineout,
390 .set_hw_reset = ftr_gpio_set_hw_reset,
391 .get_headphone = ftr_gpio_get_headphone,
392 .get_speakers = ftr_gpio_get_amp,
393 .get_lineout = ftr_gpio_get_lineout,
394 .set_notify = ftr_set_notify,
395 .get_detect = ftr_get_detect,
398 struct gpio_methods *ftr_gpio_methods = &methods;
399 EXPORT_SYMBOL_GPL(ftr_gpio_methods);