blob: 71960089e207b1032f462bca61512bf93eafbc6a [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 *
Johannes Berg888dcb72008-10-23 15:47:56 +02008 * This file contains the GPIO control routines for
Johannes Bergf3d94782006-06-21 15:42:43 +02009 * direct (through feature calls) access to the GPIO
10 * registers.
11 */
12
Rob Herring5af50732013-09-17 14:28:33 -050013#include <linux/of_irq.h>
Johannes Bergf3d94782006-06-21 15:42:43 +020014#include <linux/interrupt.h>
Rob Herring5af50732013-09-17 14:28:33 -050015#include <asm/pmac_feature.h>
Johannes Bergf3d94782006-06-21 15:42:43 +020016#include "../aoa.h"
17
Johannes Berg5f17e792009-01-15 18:22:31 +010018/* TODO: these are lots of global variables
Johannes Bergf3d94782006-06-21 15:42:43 +020019 * that aren't used on most machines...
20 * Move them into a dynamically allocated
21 * structure and use that.
22 */
23
24/* these are the GPIO numbers (register addresses as offsets into
25 * the GPIO space) */
26static int headphone_mute_gpio;
Johannes Berg5f17e792009-01-15 18:22:31 +010027static int master_mute_gpio;
Johannes Bergf3d94782006-06-21 15:42:43 +020028static int amp_mute_gpio;
29static int lineout_mute_gpio;
30static int hw_reset_gpio;
31static int lineout_detect_gpio;
32static int headphone_detect_gpio;
33static int linein_detect_gpio;
34
35/* see the SWITCH_GPIO macro */
36static int headphone_mute_gpio_activestate;
Johannes Berg5f17e792009-01-15 18:22:31 +010037static int master_mute_gpio_activestate;
Johannes Bergf3d94782006-06-21 15:42:43 +020038static int amp_mute_gpio_activestate;
39static int lineout_mute_gpio_activestate;
40static int hw_reset_gpio_activestate;
41static int lineout_detect_gpio_activestate;
42static int headphone_detect_gpio_activestate;
43static int linein_detect_gpio_activestate;
44
45/* node pointers that we save when getting the GPIO number
46 * to get the interrupt later */
47static struct device_node *lineout_detect_node;
48static struct device_node *linein_detect_node;
49static struct device_node *headphone_detect_node;
50
51static int lineout_detect_irq;
52static int linein_detect_irq;
53static int headphone_detect_irq;
54
55static struct device_node *get_gpio(char *name,
56 char *altname,
57 int *gpioptr,
58 int *gpioactiveptr)
59{
60 struct device_node *np, *gpio;
Stephen Rothwella7edd0e2007-04-03 10:52:17 +100061 const u32 *reg;
Jeremy Kerrabddd182006-07-12 15:41:18 +100062 const char *audio_gpio;
Johannes Bergf3d94782006-06-21 15:42:43 +020063
64 *gpioptr = -1;
65
66 /* check if we can get it the easy way ... */
67 np = of_find_node_by_name(NULL, name);
68 if (!np) {
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");
74 if (!gpio)
75 return NULL;
76 while ((np = of_get_next_child(gpio, np))) {
Stephen Rothwellc4f55b32007-04-03 22:39:14 +100077 audio_gpio = of_get_property(np, "audio-gpio", NULL);
Johannes Bergf3d94782006-06-21 15:42:43 +020078 if (!audio_gpio)
79 continue;
80 if (strcmp(audio_gpio, name) == 0)
81 break;
82 if (altname && (strcmp(audio_gpio, altname) == 0))
83 break;
84 }
85 /* still not found, assume not there */
86 if (!np)
87 return NULL;
88 }
89
Stephen Rothwellc4f55b32007-04-03 22:39:14 +100090 reg = of_get_property(np, "reg", NULL);
Johannes Bergf3d94782006-06-21 15:42:43 +020091 if (!reg)
92 return NULL;
93
94 *gpioptr = *reg;
95
96 /* this is a hack, usually the GPIOs 'reg' property
97 * should have the offset based from the GPIO space
98 * which is at 0x50, but apparently not always... */
99 if (*gpioptr < 0x50)
100 *gpioptr += 0x50;
101
Stephen Rothwellc4f55b32007-04-03 22:39:14 +1000102 reg = of_get_property(np, "audio-gpio-active-state", NULL);
Johannes Bergf3d94782006-06-21 15:42:43 +0200103 if (!reg)
104 /* Apple seems to default to 1, but
105 * that doesn't seem right at least on most
106 * machines. So until proven that the opposite
107 * is necessary, we default to 0
108 * (which, incidentally, snd-powermac also does...) */
109 *gpioactiveptr = 0;
110 else
111 *gpioactiveptr = *reg;
112
113 return np;
114}
115
116static void get_irq(struct device_node * np, int *irqptr)
117{
Johannes Berg2566d362006-07-25 16:12:51 +0200118 if (np)
119 *irqptr = irq_of_parse_and_map(np, 0);
120 else
Michael Ellermanef24ba72016-09-06 21:53:24 +1000121 *irqptr = 0;
Johannes Bergf3d94782006-06-21 15:42:43 +0200122}
123
124/* 0x4 is outenable, 0x1 is out, thus 4 or 5 */
125#define SWITCH_GPIO(name, v, on) \
126 (((v)&~1) | ((on)? \
127 (name##_gpio_activestate==0?4:5): \
128 (name##_gpio_activestate==0?5:4)))
129
130#define FTR_GPIO(name, bit) \
131static void ftr_gpio_set_##name(struct gpio_runtime *rt, int on)\
132{ \
133 int v; \
134 \
135 if (unlikely(!rt)) return; \
136 \
137 if (name##_mute_gpio < 0) \
138 return; \
139 \
140 v = pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, \
141 name##_mute_gpio, \
142 0); \
143 \
144 /* muted = !on... */ \
145 v = SWITCH_GPIO(name##_mute, v, !on); \
146 \
147 pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, \
148 name##_mute_gpio, v); \
149 \
150 rt->implementation_private &= ~(1<<bit); \
151 rt->implementation_private |= (!!on << bit); \
152} \
153static int ftr_gpio_get_##name(struct gpio_runtime *rt) \
154{ \
155 if (unlikely(!rt)) return 0; \
156 return (rt->implementation_private>>bit)&1; \
157}
158
159FTR_GPIO(headphone, 0);
160FTR_GPIO(amp, 1);
161FTR_GPIO(lineout, 2);
Johannes Berg5f17e792009-01-15 18:22:31 +0100162FTR_GPIO(master, 3);
Johannes Bergf3d94782006-06-21 15:42:43 +0200163
164static void ftr_gpio_set_hw_reset(struct gpio_runtime *rt, int on)
165{
166 int v;
167
168 if (unlikely(!rt)) return;
169 if (hw_reset_gpio < 0)
170 return;
171
172 v = pmac_call_feature(PMAC_FTR_READ_GPIO, NULL,
173 hw_reset_gpio, 0);
174 v = SWITCH_GPIO(hw_reset, v, on);
175 pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL,
176 hw_reset_gpio, v);
177}
178
Johannes Berg5f17e792009-01-15 18:22:31 +0100179static struct gpio_methods methods;
180
Johannes Bergf3d94782006-06-21 15:42:43 +0200181static void ftr_gpio_all_amps_off(struct gpio_runtime *rt)
182{
183 int saved;
184
185 if (unlikely(!rt)) return;
186 saved = rt->implementation_private;
187 ftr_gpio_set_headphone(rt, 0);
188 ftr_gpio_set_amp(rt, 0);
189 ftr_gpio_set_lineout(rt, 0);
Johannes Berg5f17e792009-01-15 18:22:31 +0100190 if (methods.set_master)
191 ftr_gpio_set_master(rt, 0);
Johannes Bergf3d94782006-06-21 15:42:43 +0200192 rt->implementation_private = saved;
193}
194
195static void ftr_gpio_all_amps_restore(struct gpio_runtime *rt)
196{
197 int s;
198
199 if (unlikely(!rt)) return;
200 s = rt->implementation_private;
201 ftr_gpio_set_headphone(rt, (s>>0)&1);
202 ftr_gpio_set_amp(rt, (s>>1)&1);
203 ftr_gpio_set_lineout(rt, (s>>2)&1);
Johannes Berg5f17e792009-01-15 18:22:31 +0100204 if (methods.set_master)
205 ftr_gpio_set_master(rt, (s>>3)&1);
Johannes Bergf3d94782006-06-21 15:42:43 +0200206}
207
David Howellsc4028952006-11-22 14:57:56 +0000208static void ftr_handle_notify(struct work_struct *work)
Johannes Bergf3d94782006-06-21 15:42:43 +0200209{
David Howellsc4028952006-11-22 14:57:56 +0000210 struct gpio_notification *notif =
211 container_of(work, struct gpio_notification, work.work);
Johannes Bergf3d94782006-06-21 15:42:43 +0200212
213 mutex_lock(&notif->mutex);
214 if (notif->notify)
215 notif->notify(notif->data);
216 mutex_unlock(&notif->mutex);
217}
218
Johannes Bergbd66f3b2006-06-28 14:00:58 +0200219static void gpio_enable_dual_edge(int gpio)
220{
221 int v;
222
223 if (gpio == -1)
224 return;
225 v = pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, gpio, 0);
226 v |= 0x80; /* enable dual edge */
227 pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, gpio, v);
228}
229
Johannes Bergf3d94782006-06-21 15:42:43 +0200230static void ftr_gpio_init(struct gpio_runtime *rt)
231{
232 get_gpio("headphone-mute", NULL,
233 &headphone_mute_gpio,
234 &headphone_mute_gpio_activestate);
235 get_gpio("amp-mute", NULL,
236 &amp_mute_gpio,
237 &amp_mute_gpio_activestate);
238 get_gpio("lineout-mute", NULL,
239 &lineout_mute_gpio,
240 &lineout_mute_gpio_activestate);
241 get_gpio("hw-reset", "audio-hw-reset",
242 &hw_reset_gpio,
243 &hw_reset_gpio_activestate);
Johannes Berg5f17e792009-01-15 18:22:31 +0100244 if (get_gpio("master-mute", NULL,
245 &master_mute_gpio,
246 &master_mute_gpio_activestate)) {
247 methods.set_master = ftr_gpio_set_master;
248 methods.get_master = ftr_gpio_get_master;
249 }
Johannes Bergf3d94782006-06-21 15:42:43 +0200250
251 headphone_detect_node = get_gpio("headphone-detect", NULL,
252 &headphone_detect_gpio,
253 &headphone_detect_gpio_activestate);
254 /* go Apple, and thanks for giving these different names
255 * across the board... */
256 lineout_detect_node = get_gpio("lineout-detect", "line-output-detect",
257 &lineout_detect_gpio,
258 &lineout_detect_gpio_activestate);
259 linein_detect_node = get_gpio("linein-detect", "line-input-detect",
260 &linein_detect_gpio,
261 &linein_detect_gpio_activestate);
262
Johannes Bergbd66f3b2006-06-28 14:00:58 +0200263 gpio_enable_dual_edge(headphone_detect_gpio);
264 gpio_enable_dual_edge(lineout_detect_gpio);
265 gpio_enable_dual_edge(linein_detect_gpio);
266
Johannes Bergf3d94782006-06-21 15:42:43 +0200267 get_irq(headphone_detect_node, &headphone_detect_irq);
268 get_irq(lineout_detect_node, &lineout_detect_irq);
269 get_irq(linein_detect_node, &linein_detect_irq);
270
271 ftr_gpio_all_amps_off(rt);
272 rt->implementation_private = 0;
David Howellsc4028952006-11-22 14:57:56 +0000273 INIT_DELAYED_WORK(&rt->headphone_notify.work, ftr_handle_notify);
274 INIT_DELAYED_WORK(&rt->line_in_notify.work, ftr_handle_notify);
275 INIT_DELAYED_WORK(&rt->line_out_notify.work, ftr_handle_notify);
Johannes Bergf3d94782006-06-21 15:42:43 +0200276 mutex_init(&rt->headphone_notify.mutex);
277 mutex_init(&rt->line_in_notify.mutex);
278 mutex_init(&rt->line_out_notify.mutex);
279}
280
281static void ftr_gpio_exit(struct gpio_runtime *rt)
282{
283 ftr_gpio_all_amps_off(rt);
284 rt->implementation_private = 0;
285 if (rt->headphone_notify.notify)
286 free_irq(headphone_detect_irq, &rt->headphone_notify);
287 if (rt->line_in_notify.gpio_private)
288 free_irq(linein_detect_irq, &rt->line_in_notify);
289 if (rt->line_out_notify.gpio_private)
290 free_irq(lineout_detect_irq, &rt->line_out_notify);
Tejun Heo5b84ba22010-12-11 17:51:26 +0100291 cancel_delayed_work_sync(&rt->headphone_notify.work);
292 cancel_delayed_work_sync(&rt->line_in_notify.work);
293 cancel_delayed_work_sync(&rt->line_out_notify.work);
Johannes Bergf3d94782006-06-21 15:42:43 +0200294 mutex_destroy(&rt->headphone_notify.mutex);
295 mutex_destroy(&rt->line_in_notify.mutex);
296 mutex_destroy(&rt->line_out_notify.mutex);
297}
298
David Howells7d12e782006-10-05 14:55:46 +0100299static irqreturn_t ftr_handle_notify_irq(int xx, void *data)
Johannes Bergf3d94782006-06-21 15:42:43 +0200300{
301 struct gpio_notification *notif = data;
302
David Howellsc4028952006-11-22 14:57:56 +0000303 schedule_delayed_work(&notif->work, 0);
Johannes Bergf3d94782006-06-21 15:42:43 +0200304
305 return IRQ_HANDLED;
306}
307
308static int ftr_set_notify(struct gpio_runtime *rt,
309 enum notify_type type,
310 notify_func_t notify,
311 void *data)
312{
313 struct gpio_notification *notif;
314 notify_func_t old;
315 int irq;
316 char *name;
317 int err = -EBUSY;
318
319 switch (type) {
320 case AOA_NOTIFY_HEADPHONE:
321 notif = &rt->headphone_notify;
322 name = "headphone-detect";
323 irq = headphone_detect_irq;
324 break;
325 case AOA_NOTIFY_LINE_IN:
326 notif = &rt->line_in_notify;
327 name = "linein-detect";
328 irq = linein_detect_irq;
329 break;
330 case AOA_NOTIFY_LINE_OUT:
331 notif = &rt->line_out_notify;
332 name = "lineout-detect";
333 irq = lineout_detect_irq;
334 break;
335 default:
336 return -EINVAL;
337 }
338
Michael Ellermanef24ba72016-09-06 21:53:24 +1000339 if (!irq)
Johannes Bergf3d94782006-06-21 15:42:43 +0200340 return -ENODEV;
341
342 mutex_lock(&notif->mutex);
343
344 old = notif->notify;
345
346 if (!old && !notify) {
347 err = 0;
348 goto out_unlock;
349 }
350
351 if (old && notify) {
352 if (old == notify && notif->data == data)
353 err = 0;
354 goto out_unlock;
355 }
356
357 if (old && !notify)
358 free_irq(irq, notif);
359
360 if (!old && notify) {
361 err = request_irq(irq, ftr_handle_notify_irq, 0, name, notif);
362 if (err)
363 goto out_unlock;
364 }
365
366 notif->notify = notify;
367 notif->data = data;
368
369 err = 0;
370 out_unlock:
371 mutex_unlock(&notif->mutex);
372 return err;
373}
374
375static int ftr_get_detect(struct gpio_runtime *rt,
376 enum notify_type type)
377{
378 int gpio, ret, active;
379
380 switch (type) {
381 case AOA_NOTIFY_HEADPHONE:
382 gpio = headphone_detect_gpio;
383 active = headphone_detect_gpio_activestate;
384 break;
385 case AOA_NOTIFY_LINE_IN:
386 gpio = linein_detect_gpio;
387 active = linein_detect_gpio_activestate;
388 break;
389 case AOA_NOTIFY_LINE_OUT:
390 gpio = lineout_detect_gpio;
391 active = lineout_detect_gpio_activestate;
392 break;
393 default:
394 return -EINVAL;
395 }
396
397 if (gpio == -1)
398 return -ENODEV;
399
400 ret = pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, gpio, 0);
401 if (ret < 0)
402 return ret;
403 return ((ret >> 1) & 1) == active;
404}
405
406static struct gpio_methods methods = {
407 .init = ftr_gpio_init,
408 .exit = ftr_gpio_exit,
409 .all_amps_off = ftr_gpio_all_amps_off,
410 .all_amps_restore = ftr_gpio_all_amps_restore,
411 .set_headphone = ftr_gpio_set_headphone,
412 .set_speakers = ftr_gpio_set_amp,
413 .set_lineout = ftr_gpio_set_lineout,
414 .set_hw_reset = ftr_gpio_set_hw_reset,
415 .get_headphone = ftr_gpio_get_headphone,
416 .get_speakers = ftr_gpio_get_amp,
417 .get_lineout = ftr_gpio_get_lineout,
418 .set_notify = ftr_set_notify,
419 .get_detect = ftr_get_detect,
420};
421
422struct gpio_methods *ftr_gpio_methods = &methods;
423EXPORT_SYMBOL_GPL(ftr_gpio_methods);