1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Apple Onboard Audio pmf GPIOs
4 *
5 * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
6 */
7
8 #include <linux/slab.h>
9 #include <asm/pmac_feature.h>
10 #include <asm/pmac_pfunc.h>
11 #include "../aoa.h"
12
13 #define PMF_GPIO(name, bit) \
14 static void pmf_gpio_set_##name(struct gpio_runtime *rt, int on)\
15 { \
16 struct pmf_args args = { .count = 1, .u[0].v = !on }; \
17 int rc; \
18 \
19 if (unlikely(!rt)) return; \
20 rc = pmf_call_function(rt->node, #name "-mute", &args); \
21 if (rc && rc != -ENODEV) \
22 printk(KERN_WARNING "pmf_gpio_set_" #name \
23 " failed, rc: %d\n", rc); \
24 rt->implementation_private &= ~(1<<bit); \
25 rt->implementation_private |= (!!on << bit); \
26 } \
27 static 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
33 PMF_GPIO(headphone, 0);
34 PMF_GPIO(amp, 1);
35 PMF_GPIO(lineout, 2);
36
pmf_gpio_set_hw_reset(struct gpio_runtime * rt,int on)37 static void pmf_gpio_set_hw_reset(struct gpio_runtime *rt, int on)
38 {
39 struct pmf_args args = { .count = 1, .u[0].v = !!on };
40 int rc;
41
42 if (unlikely(!rt)) return;
43 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);
47 }
48
pmf_gpio_all_amps_off(struct gpio_runtime * rt)49 static 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
pmf_gpio_all_amps_restore(struct gpio_runtime * rt)61 static 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
pmf_handle_notify(struct work_struct * work)72 static void pmf_handle_notify(struct work_struct *work)
73 {
74 struct gpio_notification *notif =
75 container_of(work, struct gpio_notification, work.work);
76
77 guard(mutex)(¬if->mutex);
78 if (notif->notify)
79 notif->notify(notif->data);
80 }
81
pmf_gpio_init(struct gpio_runtime * rt)82 static void pmf_gpio_init(struct gpio_runtime *rt)
83 {
84 pmf_gpio_all_amps_off(rt);
85 rt->implementation_private = 0;
86 INIT_DELAYED_WORK(&rt->headphone_notify.work, pmf_handle_notify);
87 INIT_DELAYED_WORK(&rt->line_in_notify.work, pmf_handle_notify);
88 INIT_DELAYED_WORK(&rt->line_out_notify.work, pmf_handle_notify);
89 mutex_init(&rt->headphone_notify.mutex);
90 mutex_init(&rt->line_in_notify.mutex);
91 mutex_init(&rt->line_out_notify.mutex);
92 }
93
pmf_gpio_exit(struct gpio_runtime * rt)94 static void pmf_gpio_exit(struct gpio_runtime *rt)
95 {
96 pmf_gpio_all_amps_off(rt);
97 rt->implementation_private = 0;
98
99 if (rt->headphone_notify.gpio_private)
100 pmf_unregister_irq_client(rt->headphone_notify.gpio_private);
101 if (rt->line_in_notify.gpio_private)
102 pmf_unregister_irq_client(rt->line_in_notify.gpio_private);
103 if (rt->line_out_notify.gpio_private)
104 pmf_unregister_irq_client(rt->line_out_notify.gpio_private);
105
106 /* make sure no work is pending before freeing
107 * all things */
108 cancel_delayed_work_sync(&rt->headphone_notify.work);
109 cancel_delayed_work_sync(&rt->line_in_notify.work);
110 cancel_delayed_work_sync(&rt->line_out_notify.work);
111
112 mutex_destroy(&rt->headphone_notify.mutex);
113 mutex_destroy(&rt->line_in_notify.mutex);
114 mutex_destroy(&rt->line_out_notify.mutex);
115
116 kfree(rt->headphone_notify.gpio_private);
117 kfree(rt->line_in_notify.gpio_private);
118 kfree(rt->line_out_notify.gpio_private);
119 }
120
pmf_handle_notify_irq(void * data)121 static void pmf_handle_notify_irq(void *data)
122 {
123 struct gpio_notification *notif = data;
124
125 schedule_delayed_work(¬if->work, 0);
126 }
127
pmf_set_notify(struct gpio_runtime * rt,enum notify_type type,notify_func_t notify,void * data)128 static int pmf_set_notify(struct gpio_runtime *rt,
129 enum notify_type type,
130 notify_func_t notify,
131 void *data)
132 {
133 struct gpio_notification *notif;
134 notify_func_t old;
135 struct pmf_irq_client *irq_client;
136 char *name;
137 int err = -EBUSY;
138
139 switch (type) {
140 case AOA_NOTIFY_HEADPHONE:
141 notif = &rt->headphone_notify;
142 name = "headphone-detect";
143 break;
144 case AOA_NOTIFY_LINE_IN:
145 notif = &rt->line_in_notify;
146 name = "linein-detect";
147 break;
148 case AOA_NOTIFY_LINE_OUT:
149 notif = &rt->line_out_notify;
150 name = "lineout-detect";
151 break;
152 default:
153 return -EINVAL;
154 }
155
156 guard(mutex)(¬if->mutex);
157
158 old = notif->notify;
159
160 if (!old && !notify)
161 return 0;
162
163 if (old && notify) {
164 if (old == notify && notif->data == data)
165 err = 0;
166 return err;
167 }
168
169 if (old && !notify) {
170 irq_client = notif->gpio_private;
171 pmf_unregister_irq_client(irq_client);
172 kfree(irq_client);
173 notif->gpio_private = NULL;
174 }
175 if (!old && notify) {
176 irq_client = kzalloc_obj(struct pmf_irq_client);
177 if (!irq_client)
178 return -ENOMEM;
179 irq_client->data = notif;
180 irq_client->handler = pmf_handle_notify_irq;
181 irq_client->owner = THIS_MODULE;
182 err = pmf_register_irq_client(rt->node,
183 name,
184 irq_client);
185 if (err) {
186 printk(KERN_ERR "snd-aoa: gpio layer failed to"
187 " register %s irq (%d)\n", name, err);
188 kfree(irq_client);
189 return err;
190 }
191 notif->gpio_private = irq_client;
192 }
193 notif->notify = notify;
194 notif->data = data;
195
196 return 0;
197 }
198
pmf_get_detect(struct gpio_runtime * rt,enum notify_type type)199 static int pmf_get_detect(struct gpio_runtime *rt,
200 enum notify_type type)
201 {
202 char *name;
203 int err = -EBUSY, ret;
204 struct pmf_args args = { .count = 1, .u[0].p = &ret };
205
206 switch (type) {
207 case AOA_NOTIFY_HEADPHONE:
208 name = "headphone-detect";
209 break;
210 case AOA_NOTIFY_LINE_IN:
211 name = "linein-detect";
212 break;
213 case AOA_NOTIFY_LINE_OUT:
214 name = "lineout-detect";
215 break;
216 default:
217 return -EINVAL;
218 }
219
220 err = pmf_call_function(rt->node, name, &args);
221 if (err)
222 return err;
223 return ret;
224 }
225
226 static struct gpio_methods methods = {
227 .init = pmf_gpio_init,
228 .exit = pmf_gpio_exit,
229 .all_amps_off = pmf_gpio_all_amps_off,
230 .all_amps_restore = pmf_gpio_all_amps_restore,
231 .set_headphone = pmf_gpio_set_headphone,
232 .set_speakers = pmf_gpio_set_amp,
233 .set_lineout = pmf_gpio_set_lineout,
234 .set_hw_reset = pmf_gpio_set_hw_reset,
235 .get_headphone = pmf_gpio_get_headphone,
236 .get_speakers = pmf_gpio_get_amp,
237 .get_lineout = pmf_gpio_get_lineout,
238 .set_notify = pmf_set_notify,
239 .get_detect = pmf_get_detect,
240 };
241
242 struct gpio_methods *pmf_gpio_methods = &methods;
243 EXPORT_SYMBOL_GPL(pmf_gpio_methods);
244