blob: 2836c3218391c549156e0b3e53048daf6ec64cd1 [file] [log] [blame]
Johannes Bergf3d94782006-06-21 15:42:43 +02001/*
2 * Apple Onboard Audio pmf GPIOs
3 *
4 * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
5 *
6 * GPL v2, can be found in COPYING.
7 */
8
9#include <asm/pmac_feature.h>
10#include <asm/pmac_pfunc.h>
11#include "../aoa.h"
12
13#define PMF_GPIO(name, bit) \
14static void pmf_gpio_set_##name(struct gpio_runtime *rt, int on)\
15{ \
16 struct pmf_args args = { .count = 1, .u[0].v = !on }; \
Benjamin Herrenschmidte53fcab2006-07-10 04:44:37 -070017 int rc; \
18 \
Johannes Bergf3d94782006-06-21 15:42:43 +020019 if (unlikely(!rt)) return; \
Benjamin Herrenschmidte53fcab2006-07-10 04:44:37 -070020 rc = pmf_call_function(rt->node, #name "-mute", &args); \
Johannes Bergbe6a83d2006-07-25 16:15:07 +020021 if (rc && rc != -ENODEV) \
Benjamin Herrenschmidte53fcab2006-07-10 04:44:37 -070022 printk(KERN_WARNING "pmf_gpio_set_" #name \
23 " failed, rc: %d\n", rc); \
Johannes Bergf3d94782006-06-21 15:42:43 +020024 rt->implementation_private &= ~(1<<bit); \
25 rt->implementation_private |= (!!on << bit); \
26} \
27static int pmf_gpio_get_##name(struct gpio_runtime *rt) \
28{ \
29 if (unlikely(!rt)) return 0; \
30 return (rt->implementation_private>>bit)&1; \
31}
32
33PMF_GPIO(headphone, 0);
34PMF_GPIO(amp, 1);
35PMF_GPIO(lineout, 2);
36
37static void pmf_gpio_set_hw_reset(struct gpio_runtime *rt, int on)
38{
39 struct pmf_args args = { .count = 1, .u[0].v = !!on };
Benjamin Herrenschmidte53fcab2006-07-10 04:44:37 -070040 int rc;
Johannes Bergf3d94782006-06-21 15:42:43 +020041
42 if (unlikely(!rt)) return;
Benjamin Herrenschmidte53fcab2006-07-10 04:44:37 -070043 rc = pmf_call_function(rt->node, "hw-reset", &args);
44 if (rc)
45 printk(KERN_WARNING "pmf_gpio_set_hw_reset"
46 " failed, rc: %d\n", rc);
Johannes Bergf3d94782006-06-21 15:42:43 +020047}
48
49static void pmf_gpio_all_amps_off(struct gpio_runtime *rt)
50{
51 int saved;
52
53 if (unlikely(!rt)) return;
54 saved = rt->implementation_private;
55 pmf_gpio_set_headphone(rt, 0);
56 pmf_gpio_set_amp(rt, 0);
57 pmf_gpio_set_lineout(rt, 0);
58 rt->implementation_private = saved;
59}
60
61static void pmf_gpio_all_amps_restore(struct gpio_runtime *rt)
62{
63 int s;
64
65 if (unlikely(!rt)) return;
66 s = rt->implementation_private;
67 pmf_gpio_set_headphone(rt, (s>>0)&1);
68 pmf_gpio_set_amp(rt, (s>>1)&1);
69 pmf_gpio_set_lineout(rt, (s>>2)&1);
70}
71
72static void pmf_handle_notify(void *data)
73{
74 struct gpio_notification *notif = data;
75
76 mutex_lock(&notif->mutex);
77 if (notif->notify)
78 notif->notify(notif->data);
79 mutex_unlock(&notif->mutex);
80}
81
82static void pmf_gpio_init(struct gpio_runtime *rt)
83{
84 pmf_gpio_all_amps_off(rt);
85 rt->implementation_private = 0;
86 INIT_WORK(&rt->headphone_notify.work, pmf_handle_notify,
87 &rt->headphone_notify);
88 INIT_WORK(&rt->line_in_notify.work, pmf_handle_notify,
89 &rt->line_in_notify);
90 INIT_WORK(&rt->line_out_notify.work, pmf_handle_notify,
91 &rt->line_out_notify);
92 mutex_init(&rt->headphone_notify.mutex);
93 mutex_init(&rt->line_in_notify.mutex);
94 mutex_init(&rt->line_out_notify.mutex);
95}
96
97static void pmf_gpio_exit(struct gpio_runtime *rt)
98{
99 pmf_gpio_all_amps_off(rt);
100 rt->implementation_private = 0;
101
102 if (rt->headphone_notify.gpio_private)
103 pmf_unregister_irq_client(rt->headphone_notify.gpio_private);
104 if (rt->line_in_notify.gpio_private)
105 pmf_unregister_irq_client(rt->line_in_notify.gpio_private);
106 if (rt->line_out_notify.gpio_private)
107 pmf_unregister_irq_client(rt->line_out_notify.gpio_private);
108
109 /* make sure no work is pending before freeing
110 * all things */
111 cancel_delayed_work(&rt->headphone_notify.work);
112 cancel_delayed_work(&rt->line_in_notify.work);
113 cancel_delayed_work(&rt->line_out_notify.work);
114 flush_scheduled_work();
115
116 mutex_destroy(&rt->headphone_notify.mutex);
117 mutex_destroy(&rt->line_in_notify.mutex);
118 mutex_destroy(&rt->line_out_notify.mutex);
119
120 if (rt->headphone_notify.gpio_private)
121 kfree(rt->headphone_notify.gpio_private);
122 if (rt->line_in_notify.gpio_private)
123 kfree(rt->line_in_notify.gpio_private);
124 if (rt->line_out_notify.gpio_private)
125 kfree(rt->line_out_notify.gpio_private);
126}
127
128static void pmf_handle_notify_irq(void *data)
129{
130 struct gpio_notification *notif = data;
131
132 schedule_work(&notif->work);
133}
134
135static int pmf_set_notify(struct gpio_runtime *rt,
136 enum notify_type type,
137 notify_func_t notify,
138 void *data)
139{
140 struct gpio_notification *notif;
141 notify_func_t old;
142 struct pmf_irq_client *irq_client;
143 char *name;
144 int err = -EBUSY;
145
146 switch (type) {
147 case AOA_NOTIFY_HEADPHONE:
148 notif = &rt->headphone_notify;
149 name = "headphone-detect";
150 break;
151 case AOA_NOTIFY_LINE_IN:
152 notif = &rt->line_in_notify;
153 name = "linein-detect";
154 break;
155 case AOA_NOTIFY_LINE_OUT:
156 notif = &rt->line_out_notify;
157 name = "lineout-detect";
158 break;
159 default:
160 return -EINVAL;
161 }
162
163 mutex_lock(&notif->mutex);
164
165 old = notif->notify;
166
167 if (!old && !notify) {
168 err = 0;
169 goto out_unlock;
170 }
171
172 if (old && notify) {
173 if (old == notify && notif->data == data)
174 err = 0;
175 goto out_unlock;
176 }
177
178 if (old && !notify) {
179 irq_client = notif->gpio_private;
180 pmf_unregister_irq_client(irq_client);
181 kfree(irq_client);
182 notif->gpio_private = NULL;
183 }
184 if (!old && notify) {
185 irq_client = kzalloc(sizeof(struct pmf_irq_client),
186 GFP_KERNEL);
187 irq_client->data = notif;
188 irq_client->handler = pmf_handle_notify_irq;
189 irq_client->owner = THIS_MODULE;
190 err = pmf_register_irq_client(rt->node,
191 name,
192 irq_client);
193 if (err) {
194 printk(KERN_ERR "snd-aoa: gpio layer failed to"
195 " register %s irq (%d)\n", name, err);
196 kfree(irq_client);
197 goto out_unlock;
198 }
199 notif->gpio_private = irq_client;
200 }
201 notif->notify = notify;
202 notif->data = data;
203
204 err = 0;
205 out_unlock:
206 mutex_unlock(&notif->mutex);
207 return err;
208}
209
210static int pmf_get_detect(struct gpio_runtime *rt,
211 enum notify_type type)
212{
213 char *name;
214 int err = -EBUSY, ret;
215 struct pmf_args args = { .count = 1, .u[0].p = &ret };
216
217 switch (type) {
218 case AOA_NOTIFY_HEADPHONE:
219 name = "headphone-detect";
220 break;
221 case AOA_NOTIFY_LINE_IN:
222 name = "linein-detect";
223 break;
224 case AOA_NOTIFY_LINE_OUT:
225 name = "lineout-detect";
226 break;
227 default:
228 return -EINVAL;
229 }
230
231 err = pmf_call_function(rt->node, name, &args);
232 if (err)
233 return err;
234 return ret;
235}
236
237static struct gpio_methods methods = {
238 .init = pmf_gpio_init,
239 .exit = pmf_gpio_exit,
240 .all_amps_off = pmf_gpio_all_amps_off,
241 .all_amps_restore = pmf_gpio_all_amps_restore,
242 .set_headphone = pmf_gpio_set_headphone,
243 .set_speakers = pmf_gpio_set_amp,
244 .set_lineout = pmf_gpio_set_lineout,
245 .set_hw_reset = pmf_gpio_set_hw_reset,
246 .get_headphone = pmf_gpio_get_headphone,
247 .get_speakers = pmf_gpio_get_amp,
248 .get_lineout = pmf_gpio_get_lineout,
249 .set_notify = pmf_set_notify,
250 .get_detect = pmf_get_detect,
251};
252
253struct gpio_methods *pmf_gpio_methods = &methods;
254EXPORT_SYMBOL_GPL(pmf_gpio_methods);