162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Apple Onboard Audio feature call GPIO control
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci * This file contains the GPIO control routines for
862306a36Sopenharmony_ci * direct (through feature calls) access to the GPIO
962306a36Sopenharmony_ci * registers.
1062306a36Sopenharmony_ci */
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci#include <linux/of_irq.h>
1362306a36Sopenharmony_ci#include <linux/interrupt.h>
1462306a36Sopenharmony_ci#include <asm/pmac_feature.h>
1562306a36Sopenharmony_ci#include "../aoa.h"
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci/* TODO: these are lots of global variables
1862306a36Sopenharmony_ci * that aren't used on most machines...
1962306a36Sopenharmony_ci * Move them into a dynamically allocated
2062306a36Sopenharmony_ci * structure and use that.
2162306a36Sopenharmony_ci */
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ci/* these are the GPIO numbers (register addresses as offsets into
2462306a36Sopenharmony_ci * the GPIO space) */
2562306a36Sopenharmony_cistatic int headphone_mute_gpio;
2662306a36Sopenharmony_cistatic int master_mute_gpio;
2762306a36Sopenharmony_cistatic int amp_mute_gpio;
2862306a36Sopenharmony_cistatic int lineout_mute_gpio;
2962306a36Sopenharmony_cistatic int hw_reset_gpio;
3062306a36Sopenharmony_cistatic int lineout_detect_gpio;
3162306a36Sopenharmony_cistatic int headphone_detect_gpio;
3262306a36Sopenharmony_cistatic int linein_detect_gpio;
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci/* see the SWITCH_GPIO macro */
3562306a36Sopenharmony_cistatic int headphone_mute_gpio_activestate;
3662306a36Sopenharmony_cistatic int master_mute_gpio_activestate;
3762306a36Sopenharmony_cistatic int amp_mute_gpio_activestate;
3862306a36Sopenharmony_cistatic int lineout_mute_gpio_activestate;
3962306a36Sopenharmony_cistatic int hw_reset_gpio_activestate;
4062306a36Sopenharmony_cistatic int lineout_detect_gpio_activestate;
4162306a36Sopenharmony_cistatic int headphone_detect_gpio_activestate;
4262306a36Sopenharmony_cistatic int linein_detect_gpio_activestate;
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci/* node pointers that we save when getting the GPIO number
4562306a36Sopenharmony_ci * to get the interrupt later */
4662306a36Sopenharmony_cistatic struct device_node *lineout_detect_node;
4762306a36Sopenharmony_cistatic struct device_node *linein_detect_node;
4862306a36Sopenharmony_cistatic struct device_node *headphone_detect_node;
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_cistatic int lineout_detect_irq;
5162306a36Sopenharmony_cistatic int linein_detect_irq;
5262306a36Sopenharmony_cistatic int headphone_detect_irq;
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_cistatic struct device_node *get_gpio(char *name,
5562306a36Sopenharmony_ci				    char *altname,
5662306a36Sopenharmony_ci				    int *gpioptr,
5762306a36Sopenharmony_ci				    int *gpioactiveptr)
5862306a36Sopenharmony_ci{
5962306a36Sopenharmony_ci	struct device_node *np, *gpio;
6062306a36Sopenharmony_ci	const u32 *reg;
6162306a36Sopenharmony_ci	const char *audio_gpio;
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci	*gpioptr = -1;
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci	/* check if we can get it the easy way ... */
6662306a36Sopenharmony_ci	np = of_find_node_by_name(NULL, name);
6762306a36Sopenharmony_ci	if (!np) {
6862306a36Sopenharmony_ci		/* some machines have only gpioX/extint-gpioX nodes,
6962306a36Sopenharmony_ci		 * and an audio-gpio property saying what it is ...
7062306a36Sopenharmony_ci		 * So what we have to do is enumerate all children
7162306a36Sopenharmony_ci		 * of the gpio node and check them all. */
7262306a36Sopenharmony_ci		gpio = of_find_node_by_name(NULL, "gpio");
7362306a36Sopenharmony_ci		if (!gpio)
7462306a36Sopenharmony_ci			return NULL;
7562306a36Sopenharmony_ci		while ((np = of_get_next_child(gpio, np))) {
7662306a36Sopenharmony_ci			audio_gpio = of_get_property(np, "audio-gpio", NULL);
7762306a36Sopenharmony_ci			if (!audio_gpio)
7862306a36Sopenharmony_ci				continue;
7962306a36Sopenharmony_ci			if (strcmp(audio_gpio, name) == 0)
8062306a36Sopenharmony_ci				break;
8162306a36Sopenharmony_ci			if (altname && (strcmp(audio_gpio, altname) == 0))
8262306a36Sopenharmony_ci				break;
8362306a36Sopenharmony_ci		}
8462306a36Sopenharmony_ci		of_node_put(gpio);
8562306a36Sopenharmony_ci		/* still not found, assume not there */
8662306a36Sopenharmony_ci		if (!np)
8762306a36Sopenharmony_ci			return NULL;
8862306a36Sopenharmony_ci	}
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ci	reg = of_get_property(np, "reg", NULL);
9162306a36Sopenharmony_ci	if (!reg) {
9262306a36Sopenharmony_ci		of_node_put(np);
9362306a36Sopenharmony_ci		return NULL;
9462306a36Sopenharmony_ci	}
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_ci	*gpioptr = *reg;
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ci	/* this is a hack, usually the GPIOs 'reg' property
9962306a36Sopenharmony_ci	 * should have the offset based from the GPIO space
10062306a36Sopenharmony_ci	 * which is at 0x50, but apparently not always... */
10162306a36Sopenharmony_ci	if (*gpioptr < 0x50)
10262306a36Sopenharmony_ci		*gpioptr += 0x50;
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_ci	reg = of_get_property(np, "audio-gpio-active-state", NULL);
10562306a36Sopenharmony_ci	if (!reg)
10662306a36Sopenharmony_ci		/* Apple seems to default to 1, but
10762306a36Sopenharmony_ci		 * that doesn't seem right at least on most
10862306a36Sopenharmony_ci		 * machines. So until proven that the opposite
10962306a36Sopenharmony_ci		 * is necessary, we default to 0
11062306a36Sopenharmony_ci		 * (which, incidentally, snd-powermac also does...) */
11162306a36Sopenharmony_ci		*gpioactiveptr = 0;
11262306a36Sopenharmony_ci	else
11362306a36Sopenharmony_ci		*gpioactiveptr = *reg;
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ci	return np;
11662306a36Sopenharmony_ci}
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_cistatic void get_irq(struct device_node * np, int *irqptr)
11962306a36Sopenharmony_ci{
12062306a36Sopenharmony_ci	if (np)
12162306a36Sopenharmony_ci		*irqptr = irq_of_parse_and_map(np, 0);
12262306a36Sopenharmony_ci	else
12362306a36Sopenharmony_ci		*irqptr = 0;
12462306a36Sopenharmony_ci}
12562306a36Sopenharmony_ci
12662306a36Sopenharmony_ci/* 0x4 is outenable, 0x1 is out, thus 4 or 5 */
12762306a36Sopenharmony_ci#define SWITCH_GPIO(name, v, on)				\
12862306a36Sopenharmony_ci	(((v)&~1) | ((on)?					\
12962306a36Sopenharmony_ci			(name##_gpio_activestate==0?4:5):	\
13062306a36Sopenharmony_ci			(name##_gpio_activestate==0?5:4)))
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_ci#define FTR_GPIO(name, bit)					\
13362306a36Sopenharmony_cistatic void ftr_gpio_set_##name(struct gpio_runtime *rt, int on)\
13462306a36Sopenharmony_ci{								\
13562306a36Sopenharmony_ci	int v;							\
13662306a36Sopenharmony_ci								\
13762306a36Sopenharmony_ci	if (unlikely(!rt)) return;				\
13862306a36Sopenharmony_ci								\
13962306a36Sopenharmony_ci	if (name##_mute_gpio < 0)				\
14062306a36Sopenharmony_ci		return;						\
14162306a36Sopenharmony_ci								\
14262306a36Sopenharmony_ci	v = pmac_call_feature(PMAC_FTR_READ_GPIO, NULL,		\
14362306a36Sopenharmony_ci			      name##_mute_gpio,			\
14462306a36Sopenharmony_ci			      0);				\
14562306a36Sopenharmony_ci								\
14662306a36Sopenharmony_ci	/* muted = !on... */					\
14762306a36Sopenharmony_ci	v = SWITCH_GPIO(name##_mute, v, !on);			\
14862306a36Sopenharmony_ci								\
14962306a36Sopenharmony_ci	pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL,		\
15062306a36Sopenharmony_ci			  name##_mute_gpio, v);			\
15162306a36Sopenharmony_ci								\
15262306a36Sopenharmony_ci	rt->implementation_private &= ~(1<<bit);		\
15362306a36Sopenharmony_ci	rt->implementation_private |= (!!on << bit);		\
15462306a36Sopenharmony_ci}								\
15562306a36Sopenharmony_cistatic int ftr_gpio_get_##name(struct gpio_runtime *rt)		\
15662306a36Sopenharmony_ci{								\
15762306a36Sopenharmony_ci	if (unlikely(!rt)) return 0;				\
15862306a36Sopenharmony_ci	return (rt->implementation_private>>bit)&1;		\
15962306a36Sopenharmony_ci}
16062306a36Sopenharmony_ci
16162306a36Sopenharmony_ciFTR_GPIO(headphone, 0);
16262306a36Sopenharmony_ciFTR_GPIO(amp, 1);
16362306a36Sopenharmony_ciFTR_GPIO(lineout, 2);
16462306a36Sopenharmony_ciFTR_GPIO(master, 3);
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_cistatic void ftr_gpio_set_hw_reset(struct gpio_runtime *rt, int on)
16762306a36Sopenharmony_ci{
16862306a36Sopenharmony_ci	int v;
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_ci	if (unlikely(!rt)) return;
17162306a36Sopenharmony_ci	if (hw_reset_gpio < 0)
17262306a36Sopenharmony_ci		return;
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_ci	v = pmac_call_feature(PMAC_FTR_READ_GPIO, NULL,
17562306a36Sopenharmony_ci			      hw_reset_gpio, 0);
17662306a36Sopenharmony_ci	v = SWITCH_GPIO(hw_reset, v, on);
17762306a36Sopenharmony_ci	pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL,
17862306a36Sopenharmony_ci			  hw_reset_gpio, v);
17962306a36Sopenharmony_ci}
18062306a36Sopenharmony_ci
18162306a36Sopenharmony_cistatic struct gpio_methods methods;
18262306a36Sopenharmony_ci
18362306a36Sopenharmony_cistatic void ftr_gpio_all_amps_off(struct gpio_runtime *rt)
18462306a36Sopenharmony_ci{
18562306a36Sopenharmony_ci	int saved;
18662306a36Sopenharmony_ci
18762306a36Sopenharmony_ci	if (unlikely(!rt)) return;
18862306a36Sopenharmony_ci	saved = rt->implementation_private;
18962306a36Sopenharmony_ci	ftr_gpio_set_headphone(rt, 0);
19062306a36Sopenharmony_ci	ftr_gpio_set_amp(rt, 0);
19162306a36Sopenharmony_ci	ftr_gpio_set_lineout(rt, 0);
19262306a36Sopenharmony_ci	if (methods.set_master)
19362306a36Sopenharmony_ci		ftr_gpio_set_master(rt, 0);
19462306a36Sopenharmony_ci	rt->implementation_private = saved;
19562306a36Sopenharmony_ci}
19662306a36Sopenharmony_ci
19762306a36Sopenharmony_cistatic void ftr_gpio_all_amps_restore(struct gpio_runtime *rt)
19862306a36Sopenharmony_ci{
19962306a36Sopenharmony_ci	int s;
20062306a36Sopenharmony_ci
20162306a36Sopenharmony_ci	if (unlikely(!rt)) return;
20262306a36Sopenharmony_ci	s = rt->implementation_private;
20362306a36Sopenharmony_ci	ftr_gpio_set_headphone(rt, (s>>0)&1);
20462306a36Sopenharmony_ci	ftr_gpio_set_amp(rt, (s>>1)&1);
20562306a36Sopenharmony_ci	ftr_gpio_set_lineout(rt, (s>>2)&1);
20662306a36Sopenharmony_ci	if (methods.set_master)
20762306a36Sopenharmony_ci		ftr_gpio_set_master(rt, (s>>3)&1);
20862306a36Sopenharmony_ci}
20962306a36Sopenharmony_ci
21062306a36Sopenharmony_cistatic void ftr_handle_notify(struct work_struct *work)
21162306a36Sopenharmony_ci{
21262306a36Sopenharmony_ci	struct gpio_notification *notif =
21362306a36Sopenharmony_ci		container_of(work, struct gpio_notification, work.work);
21462306a36Sopenharmony_ci
21562306a36Sopenharmony_ci	mutex_lock(&notif->mutex);
21662306a36Sopenharmony_ci	if (notif->notify)
21762306a36Sopenharmony_ci		notif->notify(notif->data);
21862306a36Sopenharmony_ci	mutex_unlock(&notif->mutex);
21962306a36Sopenharmony_ci}
22062306a36Sopenharmony_ci
22162306a36Sopenharmony_cistatic void gpio_enable_dual_edge(int gpio)
22262306a36Sopenharmony_ci{
22362306a36Sopenharmony_ci	int v;
22462306a36Sopenharmony_ci
22562306a36Sopenharmony_ci	if (gpio == -1)
22662306a36Sopenharmony_ci		return;
22762306a36Sopenharmony_ci	v = pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, gpio, 0);
22862306a36Sopenharmony_ci	v |= 0x80; /* enable dual edge */
22962306a36Sopenharmony_ci	pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, gpio, v);
23062306a36Sopenharmony_ci}
23162306a36Sopenharmony_ci
23262306a36Sopenharmony_cistatic void ftr_gpio_init(struct gpio_runtime *rt)
23362306a36Sopenharmony_ci{
23462306a36Sopenharmony_ci	get_gpio("headphone-mute", NULL,
23562306a36Sopenharmony_ci		 &headphone_mute_gpio,
23662306a36Sopenharmony_ci		 &headphone_mute_gpio_activestate);
23762306a36Sopenharmony_ci	get_gpio("amp-mute", NULL,
23862306a36Sopenharmony_ci		 &amp_mute_gpio,
23962306a36Sopenharmony_ci		 &amp_mute_gpio_activestate);
24062306a36Sopenharmony_ci	get_gpio("lineout-mute", NULL,
24162306a36Sopenharmony_ci		 &lineout_mute_gpio,
24262306a36Sopenharmony_ci		 &lineout_mute_gpio_activestate);
24362306a36Sopenharmony_ci	get_gpio("hw-reset", "audio-hw-reset",
24462306a36Sopenharmony_ci		 &hw_reset_gpio,
24562306a36Sopenharmony_ci		 &hw_reset_gpio_activestate);
24662306a36Sopenharmony_ci	if (get_gpio("master-mute", NULL,
24762306a36Sopenharmony_ci		     &master_mute_gpio,
24862306a36Sopenharmony_ci		     &master_mute_gpio_activestate)) {
24962306a36Sopenharmony_ci		methods.set_master = ftr_gpio_set_master;
25062306a36Sopenharmony_ci		methods.get_master = ftr_gpio_get_master;
25162306a36Sopenharmony_ci	}
25262306a36Sopenharmony_ci
25362306a36Sopenharmony_ci	headphone_detect_node = get_gpio("headphone-detect", NULL,
25462306a36Sopenharmony_ci					 &headphone_detect_gpio,
25562306a36Sopenharmony_ci					 &headphone_detect_gpio_activestate);
25662306a36Sopenharmony_ci	/* go Apple, and thanks for giving these different names
25762306a36Sopenharmony_ci	 * across the board... */
25862306a36Sopenharmony_ci	lineout_detect_node = get_gpio("lineout-detect", "line-output-detect",
25962306a36Sopenharmony_ci				       &lineout_detect_gpio,
26062306a36Sopenharmony_ci				       &lineout_detect_gpio_activestate);
26162306a36Sopenharmony_ci	linein_detect_node = get_gpio("linein-detect", "line-input-detect",
26262306a36Sopenharmony_ci				      &linein_detect_gpio,
26362306a36Sopenharmony_ci				      &linein_detect_gpio_activestate);
26462306a36Sopenharmony_ci
26562306a36Sopenharmony_ci	gpio_enable_dual_edge(headphone_detect_gpio);
26662306a36Sopenharmony_ci	gpio_enable_dual_edge(lineout_detect_gpio);
26762306a36Sopenharmony_ci	gpio_enable_dual_edge(linein_detect_gpio);
26862306a36Sopenharmony_ci
26962306a36Sopenharmony_ci	get_irq(headphone_detect_node, &headphone_detect_irq);
27062306a36Sopenharmony_ci	get_irq(lineout_detect_node, &lineout_detect_irq);
27162306a36Sopenharmony_ci	get_irq(linein_detect_node, &linein_detect_irq);
27262306a36Sopenharmony_ci
27362306a36Sopenharmony_ci	ftr_gpio_all_amps_off(rt);
27462306a36Sopenharmony_ci	rt->implementation_private = 0;
27562306a36Sopenharmony_ci	INIT_DELAYED_WORK(&rt->headphone_notify.work, ftr_handle_notify);
27662306a36Sopenharmony_ci	INIT_DELAYED_WORK(&rt->line_in_notify.work, ftr_handle_notify);
27762306a36Sopenharmony_ci	INIT_DELAYED_WORK(&rt->line_out_notify.work, ftr_handle_notify);
27862306a36Sopenharmony_ci	mutex_init(&rt->headphone_notify.mutex);
27962306a36Sopenharmony_ci	mutex_init(&rt->line_in_notify.mutex);
28062306a36Sopenharmony_ci	mutex_init(&rt->line_out_notify.mutex);
28162306a36Sopenharmony_ci}
28262306a36Sopenharmony_ci
28362306a36Sopenharmony_cistatic void ftr_gpio_exit(struct gpio_runtime *rt)
28462306a36Sopenharmony_ci{
28562306a36Sopenharmony_ci	ftr_gpio_all_amps_off(rt);
28662306a36Sopenharmony_ci	rt->implementation_private = 0;
28762306a36Sopenharmony_ci	if (rt->headphone_notify.notify)
28862306a36Sopenharmony_ci		free_irq(headphone_detect_irq, &rt->headphone_notify);
28962306a36Sopenharmony_ci	if (rt->line_in_notify.gpio_private)
29062306a36Sopenharmony_ci		free_irq(linein_detect_irq, &rt->line_in_notify);
29162306a36Sopenharmony_ci	if (rt->line_out_notify.gpio_private)
29262306a36Sopenharmony_ci		free_irq(lineout_detect_irq, &rt->line_out_notify);
29362306a36Sopenharmony_ci	cancel_delayed_work_sync(&rt->headphone_notify.work);
29462306a36Sopenharmony_ci	cancel_delayed_work_sync(&rt->line_in_notify.work);
29562306a36Sopenharmony_ci	cancel_delayed_work_sync(&rt->line_out_notify.work);
29662306a36Sopenharmony_ci	mutex_destroy(&rt->headphone_notify.mutex);
29762306a36Sopenharmony_ci	mutex_destroy(&rt->line_in_notify.mutex);
29862306a36Sopenharmony_ci	mutex_destroy(&rt->line_out_notify.mutex);
29962306a36Sopenharmony_ci}
30062306a36Sopenharmony_ci
30162306a36Sopenharmony_cistatic irqreturn_t ftr_handle_notify_irq(int xx, void *data)
30262306a36Sopenharmony_ci{
30362306a36Sopenharmony_ci	struct gpio_notification *notif = data;
30462306a36Sopenharmony_ci
30562306a36Sopenharmony_ci	schedule_delayed_work(&notif->work, 0);
30662306a36Sopenharmony_ci
30762306a36Sopenharmony_ci	return IRQ_HANDLED;
30862306a36Sopenharmony_ci}
30962306a36Sopenharmony_ci
31062306a36Sopenharmony_cistatic int ftr_set_notify(struct gpio_runtime *rt,
31162306a36Sopenharmony_ci			  enum notify_type type,
31262306a36Sopenharmony_ci			  notify_func_t notify,
31362306a36Sopenharmony_ci			  void *data)
31462306a36Sopenharmony_ci{
31562306a36Sopenharmony_ci	struct gpio_notification *notif;
31662306a36Sopenharmony_ci	notify_func_t old;
31762306a36Sopenharmony_ci	int irq;
31862306a36Sopenharmony_ci	char *name;
31962306a36Sopenharmony_ci	int err = -EBUSY;
32062306a36Sopenharmony_ci
32162306a36Sopenharmony_ci	switch (type) {
32262306a36Sopenharmony_ci	case AOA_NOTIFY_HEADPHONE:
32362306a36Sopenharmony_ci		notif = &rt->headphone_notify;
32462306a36Sopenharmony_ci		name = "headphone-detect";
32562306a36Sopenharmony_ci		irq = headphone_detect_irq;
32662306a36Sopenharmony_ci		break;
32762306a36Sopenharmony_ci	case AOA_NOTIFY_LINE_IN:
32862306a36Sopenharmony_ci		notif = &rt->line_in_notify;
32962306a36Sopenharmony_ci		name = "linein-detect";
33062306a36Sopenharmony_ci		irq = linein_detect_irq;
33162306a36Sopenharmony_ci		break;
33262306a36Sopenharmony_ci	case AOA_NOTIFY_LINE_OUT:
33362306a36Sopenharmony_ci		notif = &rt->line_out_notify;
33462306a36Sopenharmony_ci		name = "lineout-detect";
33562306a36Sopenharmony_ci		irq = lineout_detect_irq;
33662306a36Sopenharmony_ci		break;
33762306a36Sopenharmony_ci	default:
33862306a36Sopenharmony_ci		return -EINVAL;
33962306a36Sopenharmony_ci	}
34062306a36Sopenharmony_ci
34162306a36Sopenharmony_ci	if (!irq)
34262306a36Sopenharmony_ci		return -ENODEV;
34362306a36Sopenharmony_ci
34462306a36Sopenharmony_ci	mutex_lock(&notif->mutex);
34562306a36Sopenharmony_ci
34662306a36Sopenharmony_ci	old = notif->notify;
34762306a36Sopenharmony_ci
34862306a36Sopenharmony_ci	if (!old && !notify) {
34962306a36Sopenharmony_ci		err = 0;
35062306a36Sopenharmony_ci		goto out_unlock;
35162306a36Sopenharmony_ci	}
35262306a36Sopenharmony_ci
35362306a36Sopenharmony_ci	if (old && notify) {
35462306a36Sopenharmony_ci		if (old == notify && notif->data == data)
35562306a36Sopenharmony_ci			err = 0;
35662306a36Sopenharmony_ci		goto out_unlock;
35762306a36Sopenharmony_ci	}
35862306a36Sopenharmony_ci
35962306a36Sopenharmony_ci	if (old && !notify)
36062306a36Sopenharmony_ci		free_irq(irq, notif);
36162306a36Sopenharmony_ci
36262306a36Sopenharmony_ci	if (!old && notify) {
36362306a36Sopenharmony_ci		err = request_irq(irq, ftr_handle_notify_irq, 0, name, notif);
36462306a36Sopenharmony_ci		if (err)
36562306a36Sopenharmony_ci			goto out_unlock;
36662306a36Sopenharmony_ci	}
36762306a36Sopenharmony_ci
36862306a36Sopenharmony_ci	notif->notify = notify;
36962306a36Sopenharmony_ci	notif->data = data;
37062306a36Sopenharmony_ci
37162306a36Sopenharmony_ci	err = 0;
37262306a36Sopenharmony_ci out_unlock:
37362306a36Sopenharmony_ci	mutex_unlock(&notif->mutex);
37462306a36Sopenharmony_ci	return err;
37562306a36Sopenharmony_ci}
37662306a36Sopenharmony_ci
37762306a36Sopenharmony_cistatic int ftr_get_detect(struct gpio_runtime *rt,
37862306a36Sopenharmony_ci			  enum notify_type type)
37962306a36Sopenharmony_ci{
38062306a36Sopenharmony_ci	int gpio, ret, active;
38162306a36Sopenharmony_ci
38262306a36Sopenharmony_ci	switch (type) {
38362306a36Sopenharmony_ci	case AOA_NOTIFY_HEADPHONE:
38462306a36Sopenharmony_ci		gpio = headphone_detect_gpio;
38562306a36Sopenharmony_ci		active = headphone_detect_gpio_activestate;
38662306a36Sopenharmony_ci		break;
38762306a36Sopenharmony_ci	case AOA_NOTIFY_LINE_IN:
38862306a36Sopenharmony_ci		gpio = linein_detect_gpio;
38962306a36Sopenharmony_ci		active = linein_detect_gpio_activestate;
39062306a36Sopenharmony_ci		break;
39162306a36Sopenharmony_ci	case AOA_NOTIFY_LINE_OUT:
39262306a36Sopenharmony_ci		gpio = lineout_detect_gpio;
39362306a36Sopenharmony_ci		active = lineout_detect_gpio_activestate;
39462306a36Sopenharmony_ci		break;
39562306a36Sopenharmony_ci	default:
39662306a36Sopenharmony_ci		return -EINVAL;
39762306a36Sopenharmony_ci	}
39862306a36Sopenharmony_ci
39962306a36Sopenharmony_ci	if (gpio == -1)
40062306a36Sopenharmony_ci		return -ENODEV;
40162306a36Sopenharmony_ci
40262306a36Sopenharmony_ci	ret = pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, gpio, 0);
40362306a36Sopenharmony_ci	if (ret < 0)
40462306a36Sopenharmony_ci		return ret;
40562306a36Sopenharmony_ci	return ((ret >> 1) & 1) == active;
40662306a36Sopenharmony_ci}
40762306a36Sopenharmony_ci
40862306a36Sopenharmony_cistatic struct gpio_methods methods = {
40962306a36Sopenharmony_ci	.init			= ftr_gpio_init,
41062306a36Sopenharmony_ci	.exit			= ftr_gpio_exit,
41162306a36Sopenharmony_ci	.all_amps_off		= ftr_gpio_all_amps_off,
41262306a36Sopenharmony_ci	.all_amps_restore	= ftr_gpio_all_amps_restore,
41362306a36Sopenharmony_ci	.set_headphone		= ftr_gpio_set_headphone,
41462306a36Sopenharmony_ci	.set_speakers		= ftr_gpio_set_amp,
41562306a36Sopenharmony_ci	.set_lineout		= ftr_gpio_set_lineout,
41662306a36Sopenharmony_ci	.set_hw_reset		= ftr_gpio_set_hw_reset,
41762306a36Sopenharmony_ci	.get_headphone		= ftr_gpio_get_headphone,
41862306a36Sopenharmony_ci	.get_speakers		= ftr_gpio_get_amp,
41962306a36Sopenharmony_ci	.get_lineout		= ftr_gpio_get_lineout,
42062306a36Sopenharmony_ci	.set_notify		= ftr_set_notify,
42162306a36Sopenharmony_ci	.get_detect		= ftr_get_detect,
42262306a36Sopenharmony_ci};
42362306a36Sopenharmony_ci
42462306a36Sopenharmony_cistruct gpio_methods *ftr_gpio_methods = &methods;
42562306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(ftr_gpio_methods);
426