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