blob: 805dcbff22572c79748783b0ee6ae4022ed3e1f8 [file] [log] [blame]
Johannes Bergf3d94782006-06-21 15:42:43 +02001/*
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) */
25static int headphone_mute_gpio;
26static int amp_mute_gpio;
27static int lineout_mute_gpio;
28static int hw_reset_gpio;
29static int lineout_detect_gpio;
30static int headphone_detect_gpio;
31static int linein_detect_gpio;
32
33/* see the SWITCH_GPIO macro */
34static int headphone_mute_gpio_activestate;
35static int amp_mute_gpio_activestate;
36static int lineout_mute_gpio_activestate;
37static int hw_reset_gpio_activestate;
38static int lineout_detect_gpio_activestate;
39static int headphone_detect_gpio_activestate;
40static int linein_detect_gpio_activestate;
41
42/* node pointers that we save when getting the GPIO number
43 * to get the interrupt later */
44static struct device_node *lineout_detect_node;
45static struct device_node *linein_detect_node;
46static struct device_node *headphone_detect_node;
47
48static int lineout_detect_irq;
49static int linein_detect_irq;
50static int headphone_detect_irq;
51
52static struct device_node *get_gpio(char *name,
53 char *altname,
54 int *gpioptr,
55 int *gpioactiveptr)
56{
57 struct device_node *np, *gpio;
Stephen Rothwella7edd0e2007-04-03 10:52:17 +100058 const u32 *reg;
Jeremy Kerrabddd182006-07-12 15:41:18 +100059 const char *audio_gpio;
Johannes Bergf3d94782006-06-21 15:42:43 +020060
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))) {
Stephen Rothwellc4f55b32007-04-03 22:39:14 +100074 audio_gpio = of_get_property(np, "audio-gpio", NULL);
Johannes Bergf3d94782006-06-21 15:42:43 +020075 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
Stephen Rothwellc4f55b32007-04-03 22:39:14 +100087 reg = of_get_property(np, "reg", NULL);
Johannes Bergf3d94782006-06-21 15:42:43 +020088 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
Stephen Rothwellc4f55b32007-04-03 22:39:14 +100099 reg = of_get_property(np, "audio-gpio-active-state", NULL);
Johannes Bergf3d94782006-06-21 15:42:43 +0200100 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
113static void get_irq(struct device_node * np, int *irqptr)
114{
Johannes Berg2566d362006-07-25 16:12:51 +0200115 if (np)
116 *irqptr = irq_of_parse_and_map(np, 0);
117 else
118 *irqptr = NO_IRQ;
Johannes Bergf3d94782006-06-21 15:42:43 +0200119}
120
121/* 0x4 is outenable, 0x1 is out, thus 4 or 5 */
122#define SWITCH_GPIO(name, v, on) \
123 (((v)&~1) | ((on)? \
124 (name##_gpio_activestate==0?4:5): \
125 (name##_gpio_activestate==0?5:4)))
126
127#define FTR_GPIO(name, bit) \
128static void ftr_gpio_set_##name(struct gpio_runtime *rt, int on)\
129{ \
130 int v; \
131 \
132 if (unlikely(!rt)) return; \
133 \
134 if (name##_mute_gpio < 0) \
135 return; \
136 \
137 v = pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, \
138 name##_mute_gpio, \
139 0); \
140 \
141 /* muted = !on... */ \
142 v = SWITCH_GPIO(name##_mute, v, !on); \
143 \
144 pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, \
145 name##_mute_gpio, v); \
146 \
147 rt->implementation_private &= ~(1<<bit); \
148 rt->implementation_private |= (!!on << bit); \
149} \
150static int ftr_gpio_get_##name(struct gpio_runtime *rt) \
151{ \
152 if (unlikely(!rt)) return 0; \
153 return (rt->implementation_private>>bit)&1; \
154}
155
156FTR_GPIO(headphone, 0);
157FTR_GPIO(amp, 1);
158FTR_GPIO(lineout, 2);
159
160static void ftr_gpio_set_hw_reset(struct gpio_runtime *rt, int on)
161{
162 int v;
163
164 if (unlikely(!rt)) return;
165 if (hw_reset_gpio < 0)
166 return;
167
168 v = pmac_call_feature(PMAC_FTR_READ_GPIO, NULL,
169 hw_reset_gpio, 0);
170 v = SWITCH_GPIO(hw_reset, v, on);
171 pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL,
172 hw_reset_gpio, v);
173}
174
175static void ftr_gpio_all_amps_off(struct gpio_runtime *rt)
176{
177 int saved;
178
179 if (unlikely(!rt)) return;
180 saved = rt->implementation_private;
181 ftr_gpio_set_headphone(rt, 0);
182 ftr_gpio_set_amp(rt, 0);
183 ftr_gpio_set_lineout(rt, 0);
184 rt->implementation_private = saved;
185}
186
187static void ftr_gpio_all_amps_restore(struct gpio_runtime *rt)
188{
189 int s;
190
191 if (unlikely(!rt)) return;
192 s = rt->implementation_private;
193 ftr_gpio_set_headphone(rt, (s>>0)&1);
194 ftr_gpio_set_amp(rt, (s>>1)&1);
195 ftr_gpio_set_lineout(rt, (s>>2)&1);
196}
197
David Howellsc4028952006-11-22 14:57:56 +0000198static void ftr_handle_notify(struct work_struct *work)
Johannes Bergf3d94782006-06-21 15:42:43 +0200199{
David Howellsc4028952006-11-22 14:57:56 +0000200 struct gpio_notification *notif =
201 container_of(work, struct gpio_notification, work.work);
Johannes Bergf3d94782006-06-21 15:42:43 +0200202
203 mutex_lock(&notif->mutex);
204 if (notif->notify)
205 notif->notify(notif->data);
206 mutex_unlock(&notif->mutex);
207}
208
Johannes Bergbd66f3b2006-06-28 14:00:58 +0200209static void gpio_enable_dual_edge(int gpio)
210{
211 int v;
212
213 if (gpio == -1)
214 return;
215 v = pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, gpio, 0);
216 v |= 0x80; /* enable dual edge */
217 pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, gpio, v);
218}
219
Johannes Bergf3d94782006-06-21 15:42:43 +0200220static void ftr_gpio_init(struct gpio_runtime *rt)
221{
222 get_gpio("headphone-mute", NULL,
223 &headphone_mute_gpio,
224 &headphone_mute_gpio_activestate);
225 get_gpio("amp-mute", NULL,
226 &amp_mute_gpio,
227 &amp_mute_gpio_activestate);
228 get_gpio("lineout-mute", NULL,
229 &lineout_mute_gpio,
230 &lineout_mute_gpio_activestate);
231 get_gpio("hw-reset", "audio-hw-reset",
232 &hw_reset_gpio,
233 &hw_reset_gpio_activestate);
234
235 headphone_detect_node = get_gpio("headphone-detect", NULL,
236 &headphone_detect_gpio,
237 &headphone_detect_gpio_activestate);
238 /* go Apple, and thanks for giving these different names
239 * across the board... */
240 lineout_detect_node = get_gpio("lineout-detect", "line-output-detect",
241 &lineout_detect_gpio,
242 &lineout_detect_gpio_activestate);
243 linein_detect_node = get_gpio("linein-detect", "line-input-detect",
244 &linein_detect_gpio,
245 &linein_detect_gpio_activestate);
246
Johannes Bergbd66f3b2006-06-28 14:00:58 +0200247 gpio_enable_dual_edge(headphone_detect_gpio);
248 gpio_enable_dual_edge(lineout_detect_gpio);
249 gpio_enable_dual_edge(linein_detect_gpio);
250
Johannes Bergf3d94782006-06-21 15:42:43 +0200251 get_irq(headphone_detect_node, &headphone_detect_irq);
252 get_irq(lineout_detect_node, &lineout_detect_irq);
253 get_irq(linein_detect_node, &linein_detect_irq);
254
255 ftr_gpio_all_amps_off(rt);
256 rt->implementation_private = 0;
David Howellsc4028952006-11-22 14:57:56 +0000257 INIT_DELAYED_WORK(&rt->headphone_notify.work, ftr_handle_notify);
258 INIT_DELAYED_WORK(&rt->line_in_notify.work, ftr_handle_notify);
259 INIT_DELAYED_WORK(&rt->line_out_notify.work, ftr_handle_notify);
Johannes Bergf3d94782006-06-21 15:42:43 +0200260 mutex_init(&rt->headphone_notify.mutex);
261 mutex_init(&rt->line_in_notify.mutex);
262 mutex_init(&rt->line_out_notify.mutex);
263}
264
265static void ftr_gpio_exit(struct gpio_runtime *rt)
266{
267 ftr_gpio_all_amps_off(rt);
268 rt->implementation_private = 0;
269 if (rt->headphone_notify.notify)
270 free_irq(headphone_detect_irq, &rt->headphone_notify);
271 if (rt->line_in_notify.gpio_private)
272 free_irq(linein_detect_irq, &rt->line_in_notify);
273 if (rt->line_out_notify.gpio_private)
274 free_irq(lineout_detect_irq, &rt->line_out_notify);
275 cancel_delayed_work(&rt->headphone_notify.work);
276 cancel_delayed_work(&rt->line_in_notify.work);
277 cancel_delayed_work(&rt->line_out_notify.work);
278 flush_scheduled_work();
279 mutex_destroy(&rt->headphone_notify.mutex);
280 mutex_destroy(&rt->line_in_notify.mutex);
281 mutex_destroy(&rt->line_out_notify.mutex);
282}
283
David Howells7d12e782006-10-05 14:55:46 +0100284static irqreturn_t ftr_handle_notify_irq(int xx, void *data)
Johannes Bergf3d94782006-06-21 15:42:43 +0200285{
286 struct gpio_notification *notif = data;
287
David Howellsc4028952006-11-22 14:57:56 +0000288 schedule_delayed_work(&notif->work, 0);
Johannes Bergf3d94782006-06-21 15:42:43 +0200289
290 return IRQ_HANDLED;
291}
292
293static int ftr_set_notify(struct gpio_runtime *rt,
294 enum notify_type type,
295 notify_func_t notify,
296 void *data)
297{
298 struct gpio_notification *notif;
299 notify_func_t old;
300 int irq;
301 char *name;
302 int err = -EBUSY;
303
304 switch (type) {
305 case AOA_NOTIFY_HEADPHONE:
306 notif = &rt->headphone_notify;
307 name = "headphone-detect";
308 irq = headphone_detect_irq;
309 break;
310 case AOA_NOTIFY_LINE_IN:
311 notif = &rt->line_in_notify;
312 name = "linein-detect";
313 irq = linein_detect_irq;
314 break;
315 case AOA_NOTIFY_LINE_OUT:
316 notif = &rt->line_out_notify;
317 name = "lineout-detect";
318 irq = lineout_detect_irq;
319 break;
320 default:
321 return -EINVAL;
322 }
323
Johannes Berg2566d362006-07-25 16:12:51 +0200324 if (irq == NO_IRQ)
Johannes Bergf3d94782006-06-21 15:42:43 +0200325 return -ENODEV;
326
327 mutex_lock(&notif->mutex);
328
329 old = notif->notify;
330
331 if (!old && !notify) {
332 err = 0;
333 goto out_unlock;
334 }
335
336 if (old && notify) {
337 if (old == notify && notif->data == data)
338 err = 0;
339 goto out_unlock;
340 }
341
342 if (old && !notify)
343 free_irq(irq, notif);
344
345 if (!old && notify) {
346 err = request_irq(irq, ftr_handle_notify_irq, 0, name, notif);
347 if (err)
348 goto out_unlock;
349 }
350
351 notif->notify = notify;
352 notif->data = data;
353
354 err = 0;
355 out_unlock:
356 mutex_unlock(&notif->mutex);
357 return err;
358}
359
360static int ftr_get_detect(struct gpio_runtime *rt,
361 enum notify_type type)
362{
363 int gpio, ret, active;
364
365 switch (type) {
366 case AOA_NOTIFY_HEADPHONE:
367 gpio = headphone_detect_gpio;
368 active = headphone_detect_gpio_activestate;
369 break;
370 case AOA_NOTIFY_LINE_IN:
371 gpio = linein_detect_gpio;
372 active = linein_detect_gpio_activestate;
373 break;
374 case AOA_NOTIFY_LINE_OUT:
375 gpio = lineout_detect_gpio;
376 active = lineout_detect_gpio_activestate;
377 break;
378 default:
379 return -EINVAL;
380 }
381
382 if (gpio == -1)
383 return -ENODEV;
384
385 ret = pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, gpio, 0);
386 if (ret < 0)
387 return ret;
388 return ((ret >> 1) & 1) == active;
389}
390
391static struct gpio_methods methods = {
392 .init = ftr_gpio_init,
393 .exit = ftr_gpio_exit,
394 .all_amps_off = ftr_gpio_all_amps_off,
395 .all_amps_restore = ftr_gpio_all_amps_restore,
396 .set_headphone = ftr_gpio_set_headphone,
397 .set_speakers = ftr_gpio_set_amp,
398 .set_lineout = ftr_gpio_set_lineout,
399 .set_hw_reset = ftr_gpio_set_hw_reset,
400 .get_headphone = ftr_gpio_get_headphone,
401 .get_speakers = ftr_gpio_get_amp,
402 .get_lineout = ftr_gpio_get_lineout,
403 .set_notify = ftr_set_notify,
404 .get_detect = ftr_get_detect,
405};
406
407struct gpio_methods *ftr_gpio_methods = &methods;
408EXPORT_SYMBOL_GPL(ftr_gpio_methods);