blob: 6776d1c12b63a30fafa4b16b52ddc638c1e0dcb2 [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
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/slab.h>
Johannes Bergf3d94782006-06-21 15:42:43 +020010#include <asm/pmac_feature.h>
11#include <asm/pmac_pfunc.h>
12#include "../aoa.h"
13
14#define PMF_GPIO(name, bit) \
15static void pmf_gpio_set_##name(struct gpio_runtime *rt, int on)\
16{ \
17 struct pmf_args args = { .count = 1, .u[0].v = !on }; \
Benjamin Herrenschmidte53fcab2006-07-10 04:44:37 -070018 int rc; \
19 \
Johannes Bergf3d94782006-06-21 15:42:43 +020020 if (unlikely(!rt)) return; \
Benjamin Herrenschmidte53fcab2006-07-10 04:44:37 -070021 rc = pmf_call_function(rt->node, #name "-mute", &args); \
Johannes Bergbe6a83d2006-07-25 16:15:07 +020022 if (rc && rc != -ENODEV) \
Benjamin Herrenschmidte53fcab2006-07-10 04:44:37 -070023 printk(KERN_WARNING "pmf_gpio_set_" #name \
24 " failed, rc: %d\n", rc); \
Johannes Bergf3d94782006-06-21 15:42:43 +020025 rt->implementation_private &= ~(1<<bit); \
26 rt->implementation_private |= (!!on << bit); \
27} \
28static int pmf_gpio_get_##name(struct gpio_runtime *rt) \
29{ \
30 if (unlikely(!rt)) return 0; \
31 return (rt->implementation_private>>bit)&1; \
32}
33
34PMF_GPIO(headphone, 0);
35PMF_GPIO(amp, 1);
36PMF_GPIO(lineout, 2);
37
38static void pmf_gpio_set_hw_reset(struct gpio_runtime *rt, int on)
39{
40 struct pmf_args args = { .count = 1, .u[0].v = !!on };
Benjamin Herrenschmidte53fcab2006-07-10 04:44:37 -070041 int rc;
Johannes Bergf3d94782006-06-21 15:42:43 +020042
43 if (unlikely(!rt)) return;
Benjamin Herrenschmidte53fcab2006-07-10 04:44:37 -070044 rc = pmf_call_function(rt->node, "hw-reset", &args);
45 if (rc)
46 printk(KERN_WARNING "pmf_gpio_set_hw_reset"
47 " failed, rc: %d\n", rc);
Johannes Bergf3d94782006-06-21 15:42:43 +020048}
49
50static void pmf_gpio_all_amps_off(struct gpio_runtime *rt)
51{
52 int saved;
53
54 if (unlikely(!rt)) return;
55 saved = rt->implementation_private;
56 pmf_gpio_set_headphone(rt, 0);
57 pmf_gpio_set_amp(rt, 0);
58 pmf_gpio_set_lineout(rt, 0);
59 rt->implementation_private = saved;
60}
61
62static void pmf_gpio_all_amps_restore(struct gpio_runtime *rt)
63{
64 int s;
65
66 if (unlikely(!rt)) return;
67 s = rt->implementation_private;
68 pmf_gpio_set_headphone(rt, (s>>0)&1);
69 pmf_gpio_set_amp(rt, (s>>1)&1);
70 pmf_gpio_set_lineout(rt, (s>>2)&1);
71}
72
David Howellsc4028952006-11-22 14:57:56 +000073static void pmf_handle_notify(struct work_struct *work)
Johannes Bergf3d94782006-06-21 15:42:43 +020074{
David Howellsc4028952006-11-22 14:57:56 +000075 struct gpio_notification *notif =
76 container_of(work, struct gpio_notification, work.work);
Johannes Bergf3d94782006-06-21 15:42:43 +020077
78 mutex_lock(&notif->mutex);
79 if (notif->notify)
80 notif->notify(notif->data);
81 mutex_unlock(&notif->mutex);
82}
83
84static void pmf_gpio_init(struct gpio_runtime *rt)
85{
86 pmf_gpio_all_amps_off(rt);
87 rt->implementation_private = 0;
David Howellsc4028952006-11-22 14:57:56 +000088 INIT_DELAYED_WORK(&rt->headphone_notify.work, pmf_handle_notify);
89 INIT_DELAYED_WORK(&rt->line_in_notify.work, pmf_handle_notify);
90 INIT_DELAYED_WORK(&rt->line_out_notify.work, pmf_handle_notify);
Johannes Bergf3d94782006-06-21 15:42:43 +020091 mutex_init(&rt->headphone_notify.mutex);
92 mutex_init(&rt->line_in_notify.mutex);
93 mutex_init(&rt->line_out_notify.mutex);
94}
95
96static void pmf_gpio_exit(struct gpio_runtime *rt)
97{
98 pmf_gpio_all_amps_off(rt);
99 rt->implementation_private = 0;
100
101 if (rt->headphone_notify.gpio_private)
102 pmf_unregister_irq_client(rt->headphone_notify.gpio_private);
103 if (rt->line_in_notify.gpio_private)
104 pmf_unregister_irq_client(rt->line_in_notify.gpio_private);
105 if (rt->line_out_notify.gpio_private)
106 pmf_unregister_irq_client(rt->line_out_notify.gpio_private);
107
108 /* make sure no work is pending before freeing
109 * all things */
110 cancel_delayed_work(&rt->headphone_notify.work);
111 cancel_delayed_work(&rt->line_in_notify.work);
112 cancel_delayed_work(&rt->line_out_notify.work);
113 flush_scheduled_work();
114
115 mutex_destroy(&rt->headphone_notify.mutex);
116 mutex_destroy(&rt->line_in_notify.mutex);
117 mutex_destroy(&rt->line_out_notify.mutex);
118
119 if (rt->headphone_notify.gpio_private)
120 kfree(rt->headphone_notify.gpio_private);
121 if (rt->line_in_notify.gpio_private)
122 kfree(rt->line_in_notify.gpio_private);
123 if (rt->line_out_notify.gpio_private)
124 kfree(rt->line_out_notify.gpio_private);
125}
126
127static void pmf_handle_notify_irq(void *data)
128{
129 struct gpio_notification *notif = data;
130
David Howellsc4028952006-11-22 14:57:56 +0000131 schedule_delayed_work(&notif->work, 0);
Johannes Bergf3d94782006-06-21 15:42:43 +0200132}
133
134static int pmf_set_notify(struct gpio_runtime *rt,
135 enum notify_type type,
136 notify_func_t notify,
137 void *data)
138{
139 struct gpio_notification *notif;
140 notify_func_t old;
141 struct pmf_irq_client *irq_client;
142 char *name;
143 int err = -EBUSY;
144
145 switch (type) {
146 case AOA_NOTIFY_HEADPHONE:
147 notif = &rt->headphone_notify;
148 name = "headphone-detect";
149 break;
150 case AOA_NOTIFY_LINE_IN:
151 notif = &rt->line_in_notify;
152 name = "linein-detect";
153 break;
154 case AOA_NOTIFY_LINE_OUT:
155 notif = &rt->line_out_notify;
156 name = "lineout-detect";
157 break;
158 default:
159 return -EINVAL;
160 }
161
162 mutex_lock(&notif->mutex);
163
164 old = notif->notify;
165
166 if (!old && !notify) {
167 err = 0;
168 goto out_unlock;
169 }
170
171 if (old && notify) {
172 if (old == notify && notif->data == data)
173 err = 0;
174 goto out_unlock;
175 }
176
177 if (old && !notify) {
178 irq_client = notif->gpio_private;
179 pmf_unregister_irq_client(irq_client);
180 kfree(irq_client);
181 notif->gpio_private = NULL;
182 }
183 if (!old && notify) {
184 irq_client = kzalloc(sizeof(struct pmf_irq_client),
185 GFP_KERNEL);
Julia Lawallf065fab2009-07-31 08:32:03 +0200186 if (!irq_client) {
187 err = -ENOMEM;
188 goto out_unlock;
189 }
Johannes Bergf3d94782006-06-21 15:42:43 +0200190 irq_client->data = notif;
191 irq_client->handler = pmf_handle_notify_irq;
192 irq_client->owner = THIS_MODULE;
193 err = pmf_register_irq_client(rt->node,
194 name,
195 irq_client);
196 if (err) {
197 printk(KERN_ERR "snd-aoa: gpio layer failed to"
198 " register %s irq (%d)\n", name, err);
199 kfree(irq_client);
200 goto out_unlock;
201 }
202 notif->gpio_private = irq_client;
203 }
204 notif->notify = notify;
205 notif->data = data;
206
207 err = 0;
208 out_unlock:
209 mutex_unlock(&notif->mutex);
210 return err;
211}
212
213static int pmf_get_detect(struct gpio_runtime *rt,
214 enum notify_type type)
215{
216 char *name;
217 int err = -EBUSY, ret;
218 struct pmf_args args = { .count = 1, .u[0].p = &ret };
219
220 switch (type) {
221 case AOA_NOTIFY_HEADPHONE:
222 name = "headphone-detect";
223 break;
224 case AOA_NOTIFY_LINE_IN:
225 name = "linein-detect";
226 break;
227 case AOA_NOTIFY_LINE_OUT:
228 name = "lineout-detect";
229 break;
230 default:
231 return -EINVAL;
232 }
233
234 err = pmf_call_function(rt->node, name, &args);
235 if (err)
236 return err;
237 return ret;
238}
239
240static struct gpio_methods methods = {
241 .init = pmf_gpio_init,
242 .exit = pmf_gpio_exit,
243 .all_amps_off = pmf_gpio_all_amps_off,
244 .all_amps_restore = pmf_gpio_all_amps_restore,
245 .set_headphone = pmf_gpio_set_headphone,
246 .set_speakers = pmf_gpio_set_amp,
247 .set_lineout = pmf_gpio_set_lineout,
248 .set_hw_reset = pmf_gpio_set_hw_reset,
249 .get_headphone = pmf_gpio_get_headphone,
250 .get_speakers = pmf_gpio_get_amp,
251 .get_lineout = pmf_gpio_get_lineout,
252 .set_notify = pmf_set_notify,
253 .get_detect = pmf_get_detect,
254};
255
256struct gpio_methods *pmf_gpio_methods = &methods;
257EXPORT_SYMBOL_GPL(pmf_gpio_methods);